CF1913C Game with Multiset
寄,会写题不会讲
题目传送门
题面
In this problem, you are initially given an empty multiset. You have to process two types of queries:- ADD
— add an element equal to to the multiset; - GET
— say whether it is possible to take the sum of some subset of the current multiset and get a value equal to .
Input
The first line contains one integer
Then
Output
For each GET query, print YES if it is possible to choose a subset with sum equal to
题目大意
在这个问题中,最初会给你一个空的多集。您必须处理两种类型的查询:
- ADD
- 在多集合中添加一个等于 的元素; - GET
- 询问是否可以取当前多集的某个子集之和,并得到等于 的值。
思路
观察到所添加的元素都是
好好好,越讲越乱
例:
假设多集中此时有 101
,从低到高第一位开始,此时多集中有 0
, 1
,有 YES
.
代码
#include <bits/stdc++.h> #define int long long #define endl '\n' using namespace std; int a[30]; // 记录多集中2^i的数量 void ADD(int x) { a[x]++; } bool GET(int w) { int k = 0; // 用于记录多集中2^i的数量 for (int i = 0; w; i++) { k += a[i]; // 加上此时的多集中2^i的数量 if (k < (w & 1)) // 如果多集中2^i的数量不能满足w的第i位(w&1 == w%2) { // 肯定不能满足条件了,因为2^(i+1)可不能拆成2^i return false; } k -= w & 1; w >>= 1; // w右移一位,用于之后取w二进制的下一位(相当于w/=2;) k >>= 1; // 将剩余的2^i合成2^(i+1) } return true; } signed main() { ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); int m, t, v; cin >> m; while (m--) { cin >> t >> v; if (t == 1) { ADD(v); } else { if (GET(v)) { cout << "YES\n"; } else { cout << "NO\n"; } } } return 0; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】