Educational Codeforces Round 6 C. Pearls in a Row
来源:互联网 发布:失眠症医院网络咨询 编辑:程序博客网 时间:2024/05/05 22:29
题目链接~~
题意:求一个数列中最多的“good segments” 的数量,只要一个区间有且仅有两个相同的元素就是“good segments”,还有就是区间长度尽可能长。
这个是贪心思想,只要从”good segments”最后元素的下一个开始到正好符合要求的元素结束就会得到最多的”good segments”。只是最后的”good segments”有可能可以取到整个数列最后的元素,所以要特判最后的”good segments”。
#include <cstdio>#include <cstring>#include <cmath>#include <iostream>#include <algorithm>#include <stack>#include <vector>#include <string>#include <map>#define inf 0x7fffffff;using namespace std;typedef long long ll;vector<pair<int,int> >v;//保存区间l,r边界map<int,int>cnt;//记录当前区间中元素出现的个数int a[300005];int main(){ int n; scanf("%d",&n); for(int i=0;i<n;i++){ scanf("%d",&a[i]); cnt[a[i]]=0; } int l=0,r; for(int i=0;i<n;i++){ if(++cnt[a[i]]==2){ cnt.clear(); r=i; v.push_back(make_pair(l+1,r+1)); l=i+1; } } if(v.size()==0){ puts("-1"); return 0; } int i=v[v.size()-1].second; while(i<n){ if(++cnt[a[i]]>2){ break; } i++; } if(i>=n) v[v.size()-1].second=n; printf("%d\n",v.size()); for(int i=0;i<v.size();i++){ printf("%d %d\n",v[i].first,v[i].second); } return 0;}
0 0
- 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 620C Pearls in a Row
- Educational Codeforces Round 6 C. Pearls in a Row
- Codeforces - Educational Codeforces Round 6C - Pearls in a Row(练习)
- Educational Codeforces Round 6-C. Pearls in a Row(贪心)
- Educational Codeforces Round 6 620C Pearls in a Row(stl)
- Educational Codeforces Round 6 C. Pearls in a Row (分离序列+set)
- 【Educational Codeforces Round 6C】【DP or 贪心】Pearls in a Row n个数分最多区间使得每个区间都有重复数
- Codeforces-Edu6-C.Pearls in a Row
- codeforces 620C Pearls in a Row
- Pearls in a Row CodeForces
- Educational Codeforces Round 25 B. Five-In-a-Row
- Educational Codeforces Round 25 B. Five-In-a-Row
- Educational Codeforces Round 25 B. Five-In-a-Row
- C. Pearls in a Row
- js中变量作用域问题
- 大数据(十五) - JStorm
- Spring-web源码解析之MediaTypeFileExtensionResolver
- Android小白的学习小节(1)
- Mysql安装
- Educational Codeforces Round 6 C. Pearls in a Row
- android之ListView上拉加载更多和下拉刷新
- matlab格式化
- IE8背景图片不显示
- Go GOPATH设置
- AngularJS展示数据的ng-bind指令和{{}} 区别
- @Deprecated注解
- POJ 3270 Cow Sorting
- 算法训练 安慰奶牛