Codeforce C. Pearls in a Row
2 seconds
256 megabytes
standard input
standard output
There are n pearls in a row. Let's enumerate them with integers from 1 to n from the left to the right. The pearl number i has the type ai.
Let's call a sequence of consecutive pearls a segment. Let's call a segment good if it contains two pearls of the same type.
Split the row of the pearls to the maximal number of good segments. Note that each pearl should appear in exactly one segment of the partition.
As input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use scanf/printfinstead of cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java.
The first line contains integer n (1 ≤ n ≤ 3·105) — the number of pearls in a row.
The second line contains n integers ai (1 ≤ ai ≤ 109) – the type of the i-th pearl.
On the first line print integer k — the maximal number of segments in a partition of the row.
Each of the next k lines should contain two integers lj, rj (1 ≤ lj ≤ rj ≤ n) — the number of the leftmost and the rightmost pearls in the j-th segment.
Note you should print the correct partition of the row of the pearls, so each pearl should be in exactly one segment and all segments should contain two pearls of the same type.
If there are several optimal solutions print any of them. You can print the segments in any order.
If there are no correct partitions of the row print the number "-1".
5
1 2 3 4 1
1
1 5
5
1 2 3 4 5
-1
7
1 2 1 3 1 2 1
2
1 3
4 7
题意:给你一串数字,要你分段,要求每个段中至少有两个相同的数字,要你求最多能分多少段。
思路:贪心,从前面开始循环将出现的数字记录,然后遇到相同的就跳出,这就是一段,然后将前面记录的数字清空,重复前面的操作
这样符合最优原则,遇到有两个了相同的就分为一段。要注意最后一段。
1 #include<stdio.h>
2 #include<algorithm>
3 #include<iostream>
4 #include<string.h>
5 #include<cstdio>
6 #include<climits>
7 #include<math.h>
8 #include<queue>
9 #include<string.h>
10 #include<stack>
11 #include<vector>
12 #include<map>
13 #include<ext/hash_map>
14 #define sc(x) scanf("%I64d",&x)
15 #define pr(x) printf("%I64d",x);
16 #define prr(x) printf("%I64d\n",x);
17 #define prrr(x) printf(" %I64d",x);
18 #define FOR(i,p,q) for(int i=p;i<=q;i++)
19 int cmp(const void*p,const void*q);
20 using namespace std;
21 typedef long long ll;
22 typedef struct pp
23 {
24 int x;
25 int y;
26 int flag;
27 } ss;
28 ss bb[3*100005];
29 ss aa[6*100005];
30 ss dd[3*100005];
31 map<int,int>my;//用map记录是否前面出现了某个数字
32 ss rk[3*100005];
33 int main(void)
34 {
35 int n,i,j,k,p,q;
36 while(scanf("%d",&k)!=EOF)
37 {my.clear();
38 for(i=0; i<k; i++)
39 {
40 scanf("%d",&bb[i].x);
41 bb[i].x;
42 bb[i].y=i;
43 }
44 qsort(bb,k,sizeof(ss),cmp);//这里的离散化可以不需要,直接映射就可以了。
45 int cnt=0;
46 dd[bb[0].y].x=cnt;
47 dd[bb[0].y].y=bb[0].y;
48 for(i=1; i<k; i++)
49 {
50 if(bb[i].x!=bb[i-1].x)
51 {
52 cnt++;
53 }
54 dd[bb[i].y].x=cnt;
55 dd[bb[i].y].y=bb[i].y;
56 }
57 int fr=0;
58 int sum=0;
59 for(i=0; i<k; i++)
60 {
61 if(my[dd[i].x]==0)
62 {
63 my[dd[i].x]=1;
64 }
65 else if(my[dd[i].x])
66 {
67 sum++;
68 rk[sum].x=fr;
69 rk[sum].y=i;
70 fr=i+1;
71 my.clear();//有一段后清空映射,因为段不能相交
72 }
73 }
74 if(sum==0)
75 {
76 printf("-1\n");
77 }
78 else
79 {if(rk[sum].y!=k-1)//最后一段要特别注意,如果没到端尾,要到端尾
80 {
81 rk[sum].y=k-1;
82 }
83 printf("%d\n",sum);
84 for(i=1; i<=sum; i++)
85 {
86 printf("%d %d\n",rk[i].x+1,rk[i].y+1);
87 }
88 }
89 }
90 return 0;
91 }
92 int cmp(const void*p,const void*q)
93 {
94 ss*n=(ss*)p;
95 ss*m=(ss*)q;
96 return n->x-m->x;
97 }
Codeforce C. Pearls in a Row的更多相关文章
- Educational Codeforces Round 6 C. Pearls in a Row
Educational Codeforces Round 6 C. Pearls in a Row 题意:一个3e5范围的序列:要你分成最多数量的子序列,其中子序列必须是只有两个数相同, 其余的数只能 ...
- CodeForces - 620C Pearls in a Row 贪心 STL
C. Pearls in a Row time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Codeforces 620C EDU C.Pearls in a Row ( set + greed )
C. Pearls in a Row There are n pearls in a row. Let's enumerate them with integers from 1 to n from ...
- codeforces C. Pearls in a Row map的应用
C. Pearls in a Row time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- C. Pearls in a Row
C. Pearls in a Row time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Educational Codeforces Round 6 C. Pearls in a Row set
C. Pearls in a Row There are n pearls in a row. Let's enumerate them with integers from 1 to n from ...
- 【32.26%】【codeforces 620C】Pearls in a Row
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- CF620C Pearls in a Row
CF620C Pearls in a Row 洛谷评测传送门 题目描述 There are nn pearls in a row. Let's enumerate them with integers ...
- CodeForces 620C Pearls in a Row
水题,每当出现重复就分割开来,最后留下的尾巴给最后一段 #include<cstdio> #include<cstring> #include<cmath> #in ...
随机推荐
- tensorboard No dashboards are active for the current data set.
修改一下启动命令时的路径 位置示例: 命令为 E:\PYTHON_PROJECT\testTF\inceptionV1_net\log>tensorboard --logdir=TEC4FN ...
- 系列好文 | Kubernetes 弃用 Docker,我们该何去何从?
作者 | 张攀(豫哲) 来源 | 尔达 Erda 公众号 导读:Erda 作为一站式云原生 PaaS 平台,现已面向广大开发者完成 70w+ 核心代码全部开源!**在 Erda 开源的同时,我们计划编 ...
- RTSP, RTP, RTCP, RTMP傻傻分不清?
RTSP基于TCP传输请求和响应报文,RTP基于UDP传输流媒体数据,RTCP基于UDP传送传输质量信息(如丢包和延迟). 比如喀什一个局域网内10个人同时点播广州的同一个源,喀什和广州之间就要传10 ...
- 对于HTML和XML的理解
1.什么是HTML??? HTML就是 超文本标记语言(超文本含义:超过文本 --图片 .视频.音频. 超链接) 2.HTML作用 把网页的信息格式化的展现,对网页信息进行规范化展示 连接(https ...
- 解决PLSQL查不到带中文条件的记录
原因: PLSQL乱码问题皆是ORACLE服务端字符集编码与PLSQL端字符集编码不一致引起.类似乱码问题都可以从编码是否一致上面去考虑. 解决: 1. 查询Oracle服务端字符集编码,获取NLS_ ...
- 按照eslint的规则格式化代码
1.下载eslint. 2.首选项->设置,然后搜索eslint,点击在setting.json中设置.设置内容如下: "editor.codeActionsOnSave": ...
- Android App加固原理与技术历程
App为什么会被破解入侵 随着黑客技术的普及化平民化,App,这个承载我们移动数字工作和生活的重要工具,不仅是黑客眼中的肥肉,也获得更多网友的关注.百度一下"App破解"就有529 ...
- ciscn_2019_ne_5
首先checksec和查看多少位的程序 可以看到是32位的程序,放入ida中 进入getflag 可以看到strcpy存在栈溢出,所以大体思路就是输入密码进入选择1造成溢出然后进入选择4获取shell ...
- [BUUCTF]REVERSE——[WUSTCTF2020]level3
[WUSTCTF2020]level3 附件 步骤: 例行检查,64位程序,无壳 64位ida载入,找到关键函数 看样子是个base64加密,但又感觉没那么简单,再翻翻左边的函数,找到了base64加 ...
- 合并项目(Project)
<Project2016 企业项目管理实践>张会斌 董方好 编著 在熬肥的世界里,有个大难题,就是多文件合并-- 好吧,以前是大难题,现在,早就不是了,Word有主控文档,Excel有&q ...