cf 620C Pearls in a Row(贪心)
d.有一串数字,要把这些数字分成若干连续的段,每段必须至少包含2个相同的数字,怎么分才能分的段数最多?
比如 是1 2 1 3 1 2 1
那么 答案是
2
1 3
4 7
即最多分在2段,第一段是1~3,第二段是4~7。
即分成这2段:1 2 1,3 1 2 1
s.很不错的一道贪心的题。当时没怎么细想,后来看了tourist的代码后得知。
可以证明,满足贪心选择性质和最优子结构性质。
贪心策略是:从前向后遍历,每次选择最小长度的符合条件的段。
c.
#include<iostream>
#include<stdio.h>
#include<set>
using namespace std; #define MAXN 312345 int a[MAXN]; int _start[MAXN];
int _end[MAXN]; int main(){ int n;
set<int> existed;
int cnt;
int start; while(~scanf("%d",&n)){
existed.clear(); for(int i=;i<n;++i){
scanf("%d",&a[i]);
} cnt=;
start=;
for(int i=;i<n;++i){
if(existed.find(a[i])!=existed.end()){
_start[cnt]=start;
_end[cnt]=i;
++cnt; existed.clear();
start=i+;
}
else{
existed.insert(a[i]);
}
} if(cnt==){
printf("-1\n");
}
else{
_end[cnt-]=n-;
printf("%d\n",cnt);
for(int i=;i<cnt;++i){
printf("%d %d\n",_start[i]+,_end[i]+);
}
}
} return ;
}
cf 620C Pearls in a Row(贪心)的更多相关文章
- 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 Pearls in a Row
水题,每当出现重复就分割开来,最后留下的尾巴给最后一段 #include<cstdio> #include<cstring> #include<cmath> #in ...
- 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 ...
- CF620C Pearls in a Row
CF620C Pearls in a Row 洛谷评测传送门 题目描述 There are nn pearls in a row. Let's enumerate them with integers ...
- Codeforce 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
Educational Codeforces Round 6 C. Pearls in a Row 题意:一个3e5范围的序列:要你分成最多数量的子序列,其中子序列必须是只有两个数相同, 其余的数只能 ...
- 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 ...
随机推荐
- Perl语言--一些关于赋值、引用的东西
引用详解 一.定义引用有两种: 1.用斜线“\” 2.匿名引用 1.用反斜线的引用:数组.哈希.标量 数组的引用 my @array = (1,2,3); my $aref = \@array; 哈希 ...
- 使用Reachability检测网格
#pragma mark - 网络连接检查 - (void) currentReach { // 网络检测 Reachability *curReach = [Reachability reacha ...
- Unable to locate Attribute with the the given name [] on this ManagedType
最近在写Springboot+hibernate的项目,遇到这个问题 好坑,查了半天没发现我哪里配置错了,后来发现实体类声明字段大小写敏感,把声明字段改成小写就行了
- codevs——1503 愚蠢的宠物
1503 愚蠢的宠物 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 查看运行结果 题目描述 Description 大家都知道,sheep有两 ...
- http状态码的分类
状态码分类 转自http://www.cnblogs.com/TankXiao/archive/2013/01/08/2818542.html HTTP状态码被分为五大类, 目前我们使用的HTTP协议 ...
- 【.Net 学习系列】-- 利用Aspose转换Excel为PDF文件
功能: 从数据库中查询出数据 利用Aspose.cell + Excel模板绑定数据源生成Excel文件 通过Aspose.pdf + 生成好的Excel生成PDF文件 实现: 查询数据,根据Exce ...
- js获取table的值,js获取td里input的值
1.如果想让table具有可以编辑的功能,可以在table里嵌入input标签 写法{{ list_one[1] or '' }}的作用是,当list_one[1]取值为None时,前端web界面不至 ...
- PPT中的图像失真
现象:Office PowerPoint 保存出来的PPT文件在WPS下播放的时候会出现图像失真的显现. 解决方法:Office PowerPoint打开PPT将里面的图像另存为BMP格式的图像文件, ...
- leetcode题解||Container With Most Water问题
problem: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate ...
- 【转载】C#扫盲之:==/Equals /ReferenceEquals 异同的总结,相等性你真的知道吗?
1.前言 == Equals ReferenceEquals 三个相等性测试,是.NET提供给程序员使用的三个方法,他们之间有什么联系和区别,你真的仔细研究过?虽然之前也多多少少知道一点,但是有时候又 ...