第一行输入两个正整数和
。
第二行输入个正整数
一个整数,代表游游最多能刷题的天数。
5 3 1 2 3 4 5
3
第一天上午刷1号试卷,下午刷5号试卷,总共刷6题。
第二天上午摸鱼,下午刷3号试卷,总共刷3题。
第三天上午刷4号试卷,下午刷2号试卷,总共刷6题。
5 3 1 1 1 1 1
0
显然,游游没有任何一个方案可以开始刷题。
让a[i]对k取模,计为。如果
,那么当天只做这一套卷子,ans++。否则(tmp!=0),如果
在之前出现过,就匹配。ans++;
时间复杂度,空间复杂度
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; int ans = 0; unordered_map<int, int> rd; for (int i = 0; i < n; i++) { int tmp; cin >> tmp; tmp %= k; if (tmp == 0) ans++; else { if (rd.count(k - tmp)) { ans++; rd[k - tmp]--; if (rd[k - tmp] == 0) rd.erase(k - tmp); } else rd[tmp]++; } } cout<<ans; }