Educational Codeforces Round 6 620C Pearls in a Row
来源:互联网 发布:后期制作软件 编辑:程序博客网 时间:2024/05/15 06:08
题意:从一大长串中截取子串,要求每一个子串中必须要有两个相同的数字,当存在子串的时候输出最多可以存在几个子串(贪心算法),并且输出子串的首尾端点,不存在这种子串的时候就输出-1.
解题策略:贪心算法+STL,每当有两个相同数字出现时则将这一部分变为一个子串,可以获得最大数量。
本人采用了数组的方式来记录下子串的首尾端点,不过要注意开足够大的数组,第一次数组没开够结果runtime error/(ㄒoㄒ)/~~
#include <iostream>#include <set>#include <cstdlib>#include <cstring>#include <cstdio>using namespace std;int a[200000][2];int main(){ set<int> s; int n,num,ans,i; while(cin>>n) { s.clear(); ans = 0; memset(a,0,sizeof(a)); a[0][0] = 1; for(i=1;i<=n;i++) { cin >> num; if (s.count(num)) { a[ans++][1] = i; a[ans][0] = i+1; s.clear(); } else s.insert(num); } if(a[ans-1][1]<n) a[ans-1][1] = n; if(ans) { printf("%d\n",ans); for(i=0;i<ans;i++) { printf("%d %d\n",a[i][0],a[i][1]); } } else printf("-1\n"); } return 0;}
0 0
- Educational Codeforces Round 6 620C Pearls in a Row
- cf Educational Codeforces Round 6 C Pearls in a Row
- Educational Codeforces Round 6 C. Pearls in a Row
- Educational Codeforces Round 6 C. Pearls in a Row
- Educational Codeforces Round 6 C. Pearls in a Row(贪心)
- Educational Codeforces Round 6 C. Pearls in a Row
- Educational Codeforces Round 6 620C Pearls in a Row(stl)
- Codeforces - Educational Codeforces Round 6C - Pearls in a Row(练习)
- Educational Codeforces Round 6-C. Pearls in a Row(贪心)
- Educational Codeforces Round 6 C. Pearls in a Row (分离序列+set)
- codeforces 620C Pearls in a Row
- 【Educational Codeforces Round 6C】【DP or 贪心】Pearls in a Row n个数分最多区间使得每个区间都有重复数
- Codeforces-Edu6-C.Pearls in a Row
- CodeForces 620 C. Pearls in a Row(贪心)
- Codeforces 620C Pearls in a Row(贪心)
- 【32.26%】【codeforces 620C】Pearls in a Row
- Pearls in a Row CodeForces
- Educational Codeforces Round 25 B. Five-In-a-Row
- linux type
- Andriod源码下载与编译
- 压力测试的思路和方法小结
- 我需要把数据库的数据以图表的形式(饼状,线状,柱状)显示出来,不知道django下面有没有比较好用的一些插件工具或者自带的类等等
- cocos2d-x截屏
- Educational Codeforces Round 6 620C Pearls in a Row
- Add Two Number
- 实现对象的复用——享元模式(五):单纯、复合享元模式,享元模式总结
- 8VC Venture Cup 2016 -A - Robot Sequence/B - Cards
- Android 自定义控件实现手势密码
- WDM PON(无源光网络技术)
- MyEclipse配置svn
- 解决MAC下svn run cleanup interrupted错误
- 性能分析与调优的原理