原题面:https://codeforces.com/contest/1283
A.Minutes Before the New Year
题目大意:给定时间,问距离零点零分还有多久?
分析:注意一下特判0,0就好了。
代码:
t = input() t = int(t) for i in range(t): h, m = input().split() h = int(h) m = int(m) if h == 0 and m == 0: print(0) continue ret = 24 * 60 ret -= h * 60 ret -= m print(ret)
B.Candies Division
题目大意:分糖果,得到糖果数最多的人的糖果数与得到糖果数最少的人的数量之差不能大于一。并且得到糖果数量最多的人不得多于人数的一半。
分析:简单的数学题,比较一下得到糖果数最多与最少相等的情况和得到糖果数最多比最少多一的情况即可。
代码:
#include<bits/stdc++.h> using namespace std; typedef long long ll; int main() { int t; ll n, k; cin >> t; while (t--) { cin >> n >> k; // aver * k + k / 2 <= n (aver + 1 / 2) * k <= n aver + 1/2 <= n/k aver <= n/k - 1/2 ll average = n / k; ll remain = n - average * k; cout << average * k + min(remain, k / 2) << endl; } return 0; }
C.Friends and Gifts
题目大意:每个人都要给其他一个人一个礼物,并且每个人都要收到一个礼物。请你构造一种合法的赠送礼物的方法,使得每个人都赠送非自己的人一个礼物,并且从其他人那里收到一个礼物。
分析:贪心的构造题?不知道啊,反正乱搞就过了。先把没收到礼物的找出来,再把没赠送礼物的找出来,题目保证这两类人数相等,再按合法的方法构造就行了。
代码:
#include<bits/stdc++.h> using namespace std; const int maxn=2e5+7; int f[maxn],in[maxn],out[maxn]; vector<int> need_out; vector<int> need_in; int main() { int n; cin >> n; for (int i = 1; i <= n; i++) cin >> f[i]; for (int i = 1; i <= n; i++) { if (!f[i]) out[i] = 0; else out[i]++, in[f[i]]++; } for (int i = 1; i <= n; i++) { if (!in[i]) { need_in.push_back(i); } } for (int i = 1; i <= n; i++) { if (!out[i]) { need_out.push_back(i); } } sort(need_in.begin(), need_in.end(), less<int>()); sort(need_out.begin(), need_out.end(), less<int>()); /*for (int i = 0; i < need_in.size(); i++) { cout << need_in[i] << " " << need_out[i] << endl; }*/ bool flag = true; while (flag) { flag = false; int sz = need_in.size(); for (int i = 0; i < sz; i++) { if (need_in[i] == need_out[i]) { flag = true; swap(need_out[need_out.size() - 1], need_out[i]); } } for (int i = sz - 1; i >= 0; i--) { if (need_in[i] == need_out[i]) { flag = true; swap(need_out[0], need_out[i]); } } } for (int i = 0; i < need_in.size(); i++) { f[need_out[i]] = need_in[i]; } for (int i = 1; i <= n; i++) { cout << f[i] << (i == n ? '\n' : ' '); } return 0; }