Educational Codeforces Round 6(C)尺取法+贪心
来源:互联网 发布:光网络计划 verizon 编辑:程序博客网 时间:2024/04/29 03:50
题意:给你n个数字,问 最多有多少个区间满足区间内恰好有2个相同的数字,将这些区间的下标输出。
题解:很明显,使用尺取法枚举区间,;这里使用SET去维护区间的个数,如果一个区间的数字出出现2次,那么这个区间就是符合题意的。这里其实有一个很坑的地方,举个例子
4
1 1 3 2
这组的样例应该输出
1
1 4
很多同学错应该是输出
1
1 2
那么我们可以稍微处理一下,我们只要稍微思考一下就知道,只有最后一个区间的数字有可能会扫描不到所有的数字,根据题意最后一个区间的右区间一定是n。所以输出的时候将最后的数字特殊处理一下
#include<iostream>#include<cstdio>#include<algorithm>#include<cmath>#include<cstring>#include<map>#include<set>#include<vector>#define N 3000000using namespace std;#define LL unsigned long longLL a[N];struct point{int x,y;point(int _x,int _y):x(_x),y(_y){}point(){}};map<unsigned long long,LL>q;vector<point>eg;int main(){#ifdef CDZSCfreopen("i.txt","r",stdin);#endifint n;while(~scanf("%d",&n)){ eg.clear();q.clear();for(int i=0;i<n;i++){scanf("%lld",&a[i]);}int L=0,R=0;while(R<n){if(!q.count(a[R])){q[a[R++]]++;}else{eg.push_back(point(L+1,R+1));L=++R;q.clear();}}if(eg.size()<=0){puts("-1");}else{printf("%d\n",eg.size());for(int i=0;i<eg.size();i++){if(i==eg.size()-1){eg[i].y=n;}printf("%d %d\n",eg[i].x,eg[i].y);}}}return 0;}
0 0
- Educational Codeforces Round 6(C)尺取法+贪心
- Educational Codeforces Round 12(C)贪心
- Educational Codeforces Round 11 C. Hard Process 尺取法
- Educational Codeforces Round 6 (A)贪心
- Codeforces Educational Codeforces Round 3 C. Load Balancing(贪心)
- Educational Codeforces Round 6 C. Pearls in a Row(贪心)
- Educational Codeforces Round 2C. Make Palindrome(贪心构造)
- coderforce Educational Codeforces Round 10 C. Foe Pairs(贪心)
- Educational Codeforces Round 6-C. Pearls in a Row(贪心)
- Educational Codeforces Round 5(B)贪心
- Educational Codeforces Round 5 (D. Longest k-Good Segment)(尺取法)
- Educational Codeforces Round 2 C.Make Palindrome(贪心)
- Educational Codeforces Round 12 C. Simple Strings 贪心
- Educational Codeforces Round 21 808C Tea Party 【贪心算法】
- Educational Codeforces Round 21 C. Tea Party 贪心
- Educational Codeforces Round 22-C. The Tag Game-搜索,贪心
- Educational Codeforces Round 25 C Multi-judge Solving 贪心
- Educational Codeforces Round 11(C)尺取+前缀和
- C语言判断一个字符串是否是另一个字符串的子串
- Educational Codeforces Round 6 (A)贪心
- 【20160122】深夜难眠,觉晓此生
- 使用java测试工具编写案例(一)
- Educational Codeforces Round 6 (B)模拟
- Educational Codeforces Round 6(C)尺取法+贪心
- qt 容器类
- 使用 Dojo 显示表格数据,并带筛选(filter)功能(2) - filter 插件
- 16初出茅庐A题
- 一箭多星: 如何syn flood 一个网段服务器
- Codeforces-Edu6-A. Professor GukiZ's Robot
- 前端零基础入门
- 什么样的人当不好程序员?
- 5-11 UVA 12504 Updating a dictionary更新字典