C. Inhabitant of the Deep Sea
原创 于 2024-04-24 21:17:13 发布 · 粉丝可见 · 496 阅读 · 7 · 1 · 本内容遵循CC 4.0 BY-SA版权协议 版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。 GEO检测 · 编辑
文章链接:https://blog.csdn.net/hacker_51/article/details/138169094
本题链接: Problem - C - Codeforces
题目:

样例:
cpp<br/>6<br/>4 5<br/>1 2 4 3<br/>4 6<br/>1 2 4 3<br/>5 20<br/>2 7 1 8 2<br/>2 2<br/>3 2<br/>2 15<br/>1 5<br/>2 7<br/>5 2<br/> |
思路:
数学+模拟。
根据题意,一前一后的攻击,攻击k次后,总共击落舰队多少个。
如果单纯模拟,肯定会TLE,所以要加上数学推导一下。
代码详解如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
| #include <iostream> #include <vector> #include <queue> #include <cstring> #include <algorithm> #include <unordered_map> #define endl '\n' #define int long long #define YES puts("YES") #define NO puts("NO") #define umap unordered_map #define All(x) x.begin(),x.end() #pragma GCC optimize(3,"Ofast","inline") #define IOS std::ios::sync_with_stdio(false),cin.tie(0), cout.tie(0) using namespace std; const int N = 2e6 + 10; inline void solve(); signed main() {
IOS; int _t = 1; cin >> _t; while (_t--) { solve(); } return 0; } int n,x,k; inline void solve() { cin >> n >> k; deque<int>q; for(int i = 0;i < n;++i) { cin >> x; q.emplace_back(x); } while(q.size() > 1 and k) { int mins = min(q.front(),q.back()); if(k < (mins << 1)) { q.front() -= (k >> 1) + (k % 2); q.back() -= (k >> 1); k = 0; }else { q.front() -= mins; q.back() -= mins; k -= (mins << 1); } if(!q.front()) q.pop_front(); if(!q.back()) q.pop_back(); } int ans = n - q.size(); cout << ans + (q.size() and q.front() <= k) << endl; }
|
最后提交:
