CF 620 C. Pearls in a Row 贪心
来源:互联网 发布:qt ros 显示界面编程 编辑:程序博客网 时间:2024/05/22 22:02
题目大意:给出一个数组,要求将数组分成若干段,每个数都必须属于一段,使得每一段都至少有两个数相同。
为何不贪心呢?从左往右扫描,只要满足形成一段的条件,立马形成一段,注意最后一段结尾必须是n。
#include<cstdio>#include<cstring>#include<string>#include<iostream>#include<sstream>#include<algorithm>#include<utility>#include<vector>#include<set>#include<map>#include<queue>#include<cmath>#include<iterator>#include<stack>using namespace std;typedef __int64 LL;const int INF=1e9+7;const double eps=1e-7;const int maxn=3*100000;int n,a[maxn+10];map<int,int >mp;vector<int >ve;void pre()//离散化,其实这题不用map离散化,可用set代替。{ mp.clear(); for(int i=1;i<=n;i++) { if(!mp.count(a[i])) { int k=mp.size(); mp[a[i]]=k; a[i]=k; } else { int num=mp[a[i] ]; a[i]=num; } }}int vis[maxn+10];//可用set代替标记vis+离散化+mapvoid work(){ memset(vis,-1,(n+1)*sizeof vis[0] ); ve.clear(); int ans=0; for(int i=1;i<=n;i++) { int x=a[i]; if(vis[x]==ans) { ans++; ve.push_back(i); vis[x]=-1; } else { vis[x]=ans; } } if(ve.size()==0) {puts("-1");return;} printf("%d\n",ve.size()); int last=1; for(int i=0;i<ve.size();i++) { int x=ve[i]; if(i!=ve.size()-1) printf("%d %d\n",last,x); else printf("%d %d\n",last,n); last=x+1; }}int main(){ while(~scanf("%d",&n)) { for(int i=1;i<=n;i++) { scanf("%d",&a[i]); } pre(); work(); } return 0;}
0 0
- CF 620 C. Pearls in a Row 贪心
- CodeForces 620 C. Pearls in a Row(贪心)
- Codeforces 620C Pearls in a Row(贪心)
- cf Educational Codeforces Round 6 C Pearls in a Row
- codeforces 620C Pearls in a Row
- Educational Codeforces Round 6 C. Pearls in a Row(贪心)
- C. Pearls in a Row
- Educational Codeforces Round 6 620C Pearls in a Row
- 【32.26%】【codeforces 620C】Pearls in a Row
- Codeforces-Edu6-C.Pearls in a Row
- Educational Codeforces Round 6-C. Pearls in a Row(贪心)
- CF620C - Pearls in a Row
- codeforces_620C. Pearls in a Row
- codeforces620C Pearls in a Row
- Pearls in a Row CodeForces
- 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
- Linux高性能服务器编程笔记1
- 让你尖叫的游戏开发工具清单
- FZU 2125 简单的等式
- IOS开发之OC学习笔记(下)
- mFC + ffmpeg
- CF 620 C. Pearls in a Row 贪心
- n x n矩阵的转置矩阵,并计算对角线元素之和
- 4.6日学习记录
- SDAU 搜索专题 15 Knight Moves
- [IOS]tableView的操作。
- 项目:信息交流平台系统
- Android后台定时提醒功能实现
- angularjs 前端页面之用户修改密码
- JAVA语言规范(官方文档)一