Codeforces #353 (Div. 2)C 贪心脑洞题
来源:互联网 发布:淘宝粉哥香水是正品吗 编辑:程序博客网 时间:2024/05/10 02:20
太弱了。。还是笨。。
原题
这题脑洞题,说一下贪心思路。
首先我们注意到 如果连续一段和为0,其他部分耿直的转移,那么就能少移动一次(中间不考虑子段,只让这一段变成了0),
如果 这一段后面还有连续的一段和为0 ,那么就能少移动两次。。
以此类推。。。
例如 xxxxxxxxxxx1 2 -2 -1 1 -1xxxxxxxx
如果中间那些耿直的移动一定是要移动6次才能都是0的,如果分成两段的话,明显只要4次。。
ps。为什么断开不行?因为断开中间的那些是要跨过两段的0 的,这样和中间断开至少有一个,那么好不容易节约下来的又要被移动中间那个花费掉了(甚至更多)。
所以就有了贪心策略,找到最多的连续0的方法。。这里就要考虑维护前缀和了。。
PPS。是个环你没有考虑呀?
其实不然,当我们取出最长的若干段的时候,剩下的一定是和为 0 。那么就很愉快了,答案就是 N - ANS 了。。
写这么多可能还是没讲清楚,请大家见谅,确实自己理解也不深刻,蒟蒻加油吧~
#include <bits/stdc++.h>using namespace std;map<long long,long long> mp;int main(){ int n ,x ; long long ans=0; long long sum=0; scanf("%d",&n); for(int i=0;i<n;i++){ scanf("%d",&x); sum+=x; ans=max(ans,++mp[sum]); } cout<<n-ans<<endl; return 0;}
1 0
- Codeforces #353 (Div. 2)C 贪心脑洞题
- Codeforces Round #205 (Div. 2) / 353C Find Maximum (贪心)
- Codeforces Round #353 (Div. 2) C. Money Transfers (贪心)
- Codeforces Round #311 (Div. 2) C 贪心
- Codeforces Round #363 (Div. 2)-C--贪心
- Codeforces Round #370 (Div. 2) C 贪心
- Codeforces Round #375 (Div. 2) C (贪心)
- Codeforces Round #217 (Div. 2) / 370C Mittens (构造&贪心)
- Codeforces #262 (Div. 2) C. Present(二分+贪心)
- Codeforces Round #276 (Div. 2)(C贪心,D)
- Codeforces Round #277 (Div. 2)---C. Palindrome Transformation (贪心)
- Codeforces Round #273 (Div. 2) C.Table Decorations 贪心
- Codeforces Round #277 (Div. 2) C. Palindrome Transformations 贪心
- Codeforces Round #282 (Div. 2) C. Treasure 贪心
- Codeforces Round #288 (Div. 2) C. Anya and Ghosts 贪心
- Codeforces Round #334 (Div. 2)C. Alternative Thinking(贪心)
- Codeforces Round #340 (Div. 2)(C)贪心,模拟
- Codeforces Round #294 (Div. 2)(C)贪心
- 在项目中部署maven
- Zigzag Iterator
- Spring Session管理
- 一套解决问题的方法
- Android手机网络NetWorkType对应的常量值
- Codeforces #353 (Div. 2)C 贪心脑洞题
- Win7 nodejs express不是内部或外部指令
- HTML第一天入门
- containsKey方法——判断是否包含指定的键名
- 关于navigationBar影响view的frame问题
- THINKPHP 清空数据缓存方法
- jsp限定文件上传为图片格式
- SharedPreferences详解
- 竟立下如此Flag(SQLi系列)