版本比较
3.27
正文
增加 21 行删除 21 行
15 行未改动16171819202021222324252627282829303132333435353637383915 行未改动55565758595960616263646566676868697070717273742 行未改动777879808181828384848586878889909191929394953 行未改动9910010110210310310410510510610710810911011111111211311411511611711811811912012112218 行未改动1411421431441451451461471481481491501511521531531541551561571581591591601611611621631641651661661671681691701711721731731741751761771781781791801811822 行未改动15 行未改动int main(){ ll n; cin >> n;
// 从后往前构造 for(ll i = n;i >= 1;i --) { q.push_front(i); // 将当前数放在队首 q.push_front(q.back()); // 将队尾元素移到队首 q.pop_back(); // 删除队尾 }
// 输出结果 while(q.size()) { cout << q.front() << " "; q.pop_front(); }
return 0;}```
15 行未改动int main(){ ll n; cin >> n;
// 读入卡片序列 for(ll i = 1;i <= n;i ++) { ll x; cin >> x; a[i] = x; q.push(x); }
ll mx = 0; // 记录最大能赢的球票数
// 枚举从每个位置开始 for(ll i = 1;i <= n;i ++) { // 将队列旋转到以第i个位置开头2 行未改动 ll x = q.front(); q.pop(); q.push(x); }
ll idx = 0; // 当前数到的数字 ll sum = 0; // 当前赢得的球票和
// 模拟游戏过程 while(idx <= n && q.size()) { ll x = q.front(); q.pop(); idx ++; // 数数加1
// 如果数到的数字等于卡片数字 if(idx == x) { idx = 0; // 重新从1开始数3 行未改动 { q.push(x); // 没拿到,放回队尾 } }
mx = max(sum, mx); // 更新最大值
// 清空队列,准备下一轮 while(q.size()) { q.pop(); }
// 重新放入原序列 for(ll i = 1;i <= n;i ++) { q.push(a[i]); } }
cout << mx; return 0;}```18 行未改动int main(){ ios::sync_with_stdio(0); cin.tie(0),cout.tie(0);
ll n,p; cin >> n >> p;
for(ll i = 1;i <= n;i ++) { cin >> a[i]; }
ll s = 0; // 当前选中稻草人的防御力之和 for(ll i = 1;i <= n;i ++) { q.push(a[i]); // 加入当前稻草人 s += a[i];
// 如果当前和还不够P,无法停止 if(s < p) if(s < p) { cout << -1 << " "; continue; }
// 贪心:在保证和>=P的情况下,尽量去掉防御力小的稻草人 while(s - q.top() >= p && q.size()) { s -= q.top(); q.pop(); }
// 如果和>=P,输出当前数量;否则输出-1 if(s >= p) cout << q.size() << " "; else cout << -1 << " "; }
return 0;}```
2 行未改动