CodeForces - 620C Pearls in a Row 贪心 STL
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/printf instead 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
这题找规律,从第一个数开始向后搜索,当有一个数字重复时则记录该位置,再从下一个数字开始向后重新搜索。可使用STL中的set函数
AC代码
#include<stdio.h>
#include<set>
using namespace std;
int pos[300005];
set<int>a;
int main()
{
int n,m=0,b;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&b);
if(a.count(b)==0)//检查b是否出现过
{
a.insert(b);//在末尾插入b
}
else
{
pos[m]=i;
m++;
a.clear();//清除a中数据
} }
if(m==0)
{
printf("-1\n");
}
else
{
pos[m-1]=n;
printf("%d\n",m);
printf("1 %d\n",pos[0]);
for(int i=0;i<m-1;i++)
{
printf("%d %d\n",pos[i]+1,pos[i+1]);
}
}
return 0;
}
最近刚学的STL,本来想用循环来搜索重复数字的,结果超时,不过可作为思路
#include<stdio.h>
int a[300005],pos[300005];
int main()
{
int n,m=0;
pos[m]=0;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
for(int j=pos[m]+1;j<i;j++)
{
if(a[j]==a[i])
{
m++;
pos[m]=i;
}
}
}
if(m==0)
{
printf("-1\n");
}
else
{
pos[m]=n;
printf("%d\n",m);
printf("1 %d\n",pos[1]);
for(int i=1;i<m;i++)
{
printf("%d %d\n",pos[i]+1,pos[i+1]);
}
}
return 0;
}
CodeForces - 620C Pearls in a Row 贪心 STL的更多相关文章
- CodeForces 620C Pearls in a Row
水题,每当出现重复就分割开来,最后留下的尾巴给最后一段 #include<cstdio> #include<cstring> #include<cmath> #in ...
- 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 ...
- cf 620C Pearls in a Row(贪心)
d.有一串数字,要把这些数字分成若干连续的段,每段必须至少包含2个相同的数字,怎么分才能分的段数最多? 比如 是1 2 1 3 1 2 1 那么 答案是 21 34 7 即最多分在2段,第一段是1~3 ...
- Codeforces Round #595 (Div. 3)D1D2 贪心 STL
一道用STL的贪心,正好可以用来学习使用STL库 题目大意:给出n条可以内含,相交,分离的线段,如果重叠条数超过k次则为坏点,n,k<2e5 所以我们贪心的想我们从左往右遍历,如果重合部分条数超 ...
- 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 ...
- 【32.26%】【codeforces 620C】Pearls in a Row
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- Educational Codeforces Round 6 C. Pearls in a Row
Educational Codeforces Round 6 C. Pearls in a Row 题意:一个3e5范围的序列:要你分成最多数量的子序列,其中子序列必须是只有两个数相同, 其余的数只能 ...
- 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 ...
- CF620C Pearls in a Row
CF620C Pearls in a Row 洛谷评测传送门 题目描述 There are nn pearls in a row. Let's enumerate them with integers ...
随机推荐
- 20155227 2016-2017-2 《Java程序设计》第四周学习总结
20155227 2016-2017-2 <Java程序设计>第四周学习总结 教材学习内容总结 继承 继承 继承是Java程序设计语言面向对象的又一重要体现,允许子类继承父类,避免重复的行 ...
- python Linux flask uwsgi nginx 在centos7.3部署
0.直接上uwsgi和nginx安装命令 linux 安装uwsgi yum groupinstall "Development tools" yum install zlib-d ...
- MySQL 5.6 GTID Replication【转】
一. MySQL 5.6引入了GTID的概念,那么GTID是何方神圣?其实也不复杂,就是一个全局事务标示符.使用GTID时,每次事务提交都会在binlog里生成1个唯一的标示符,它由UUID和事务ID ...
- C#中HttpWebRequest的GetRequestStream执行的效率太低,甚至偶尔死掉
为了提高httpwebrequest的执行效率,查到了一些如下设置 request.ServicePoint.Expect100Continue = false; request.ServicePoi ...
- 005_MAC下的VMware fushion快捷键(折中)
由于MAC和VMware Fushion虚拟机之间有一些快捷键的映射,所以Windows虚拟机就找了一个折中的方案.现总结MAC下的win常用快捷键==> <1>最小化窗口(Alt ...
- 九、springcloud之服务网关zuul(二)
一.路由熔断 当我们的后端服务出现异常的时候,我们不希望将异常抛出给最外层,期望服务可以自动进行一降级.Zuul给我们提供了这样的支持.当某个服务出现异常时,直接返回我们预设的信息. 我们通过自定义的 ...
- Runtime.getRuntime().exec 类 防止阻塞
import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.IOException; impor ...
- Flask:静态文件&模板(0.1)
Windows 10家庭中文版,Python 3.6.4,Flask 1.0.2 前面看了Flask的Quickstart文档,可是,一直没有练习里面的内容,这不,刚刚练习完毕,来写篇博文记录一下! ...
- webpack3学习笔记
地址:https://segmentfault.com/a/1190000006843916 地址:https://www.chungold.com/my/course/32 地址:http://js ...
- 使用Appium 测试微信小程序和微信公众号方法
由于腾讯系QQ.微信等都是基于腾讯自研X5内核,不是google原生webview,需要打开TBS内核Inspector调试功能才能用Chrome浏览器查看页面元素,并实现Appium自动化测试微信小 ...