Codeforces Round #756 (Div. 3) 題解
阿新 • • 發佈:2021-11-28
A. Make Even
題目大意:給你一個數n,現在你可以選擇一個長度l,讓這個數的前l位數交換。請你用最少的交換次數(可以為0),使n變成偶數。如果無法達到,輸出-1。
解題思路:答案只可能是-1,0,1,2,根據偶數位所出現的位置決定。
#include <iostream> #include <algorithm> #include <cstring> #include <string> #include <queue> using namespace std; int work() { int n, m = 0, flag = 0; cin >> n; if (n % 2 == 0) return 0; while(n > 0) { m = m * 10 + n % 10; if (n % 10 % 2 == 0) flag = 1; n /= 10; } if (flag == 0) return -1; if (m % 2 == 0) return 1; return 2; } int main() { int T; cin >> T; while(T--) { cout << work() << endl; } return 0; }
B. Team Composition: Programmers and Mathematicians
題目大意:有a個電腦科學家和b個數學家,現在你需要給這些人組隊。組隊條件:1. 每個隊伍必須有4人;2.每個隊伍至少有1個數學家和1個電腦科學家。問最多能組多少個隊伍。
解題思路:實在懶得去想貪心了,直接二分答案。每個隊伍先安排一個數學家和一個電腦科學家,然後剩下的隊員隨意安排即可。
#include <iostream> #include <algorithm> #include <cstring> #include <string> #include <queue> using namespace std; int a, b; bool check(int x) { int c = a - x, d = b - x; if (c >= 0 && d >= 0 && (c+d)/2 >= x) return 1; else return 0; } void work() { cin >> a >> b; int l = 0, r = (a+b)/4; while(l <= r) { int mid = (l+r) / 2; if (check(mid)) l = mid + 1; else r = mid - 1; } cout << l-1 << endl; } int main() { int T; cin >> T; while(T--) { work(); } return 0; }