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(贪心)的更多相关文章

  1. 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 ...

  2. CodeForces 620C Pearls in a Row

    水题,每当出现重复就分割开来,最后留下的尾巴给最后一段 #include<cstdio> #include<cstring> #include<cmath> #in ...

  3. 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 ...

  4. 【32.26%】【codeforces 620C】Pearls in a Row

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  5. CF620C Pearls in a Row

    CF620C Pearls in a Row 洛谷评测传送门 题目描述 There are nn pearls in a row. Let's enumerate them with integers ...

  6. 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 ...

  7. Educational Codeforces Round 6 C. Pearls in a Row

    Educational Codeforces Round 6 C. Pearls in a Row 题意:一个3e5范围的序列:要你分成最多数量的子序列,其中子序列必须是只有两个数相同, 其余的数只能 ...

  8. 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 ...

  9. C. Pearls in a Row

    C. Pearls in a Row time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

随机推荐

  1. javascript事件委托和jQuery事件绑定on、off 和one以及on绑定多个事件(重要)

    一. 事件委托什么是事件委托?用现实中的理解就是:有100 个学生同时在某天中午收到快递,但这100 个学生不可能同时站在学校门口等,那么都会委托门卫去收取,然后再逐个交给学生.而在jQuery 中, ...

  2. 标准C程序设计七---12

    Linux应用             编程深入            语言编程 标准C程序设计七---经典C11程序设计    以下内容为阅读:    <标准C程序设计>(第7版) 作者 ...

  3. 强大的stringstream

    [本文来自]http://www.builder.com.cn/2003/0304/83250.shtmlhttp://www.cppblog.com/alantop/archive/2007/07/ ...

  4. Python入门--7--处理数据时学习到的东西

    一.数据导入(这里使用的是pands包) import pands as pd wenjian = pd.read_csv('路径') 二.数据变换 print wenjian.head()    # ...

  5. 【WEB基础】HTML & CSS 基础入门(6)超链接

    超链接--文字链接 超链接[hyperlink]是网页中最为常见的元素之一,我们几乎可以在所有的网站页面中找到超链接.每个网站都不止一个页面,这些页面就是利用超链接进行串接.超链接帮我们实现了网页与网 ...

  6. 洛谷——P3576 [POI2014]MRO-Ant colony

    P3576 [POI2014]MRO-Ant colony 题目描述 The ants are scavenging an abandoned ant hill in search of food. ...

  7. rdb转为rdf

    dump-rdf -f N-TRIPLE -b http://localhost:2020/  -o iswc.nt terrsearch.ttl

  8. linux驱动开发流程

    嵌入式linux驱动开发流程嵌入式系统中,操作系统是通过各种驱动程序来驾驭硬件设备的.设备驱动程序是操作系统内核和硬件设备之间的接口,它为应用程序屏蔽了硬件的细节,这样在应用程序看来,硬件设备只是一个 ...

  9. 零基础学python-5.9 集合set

    今天我们来说说set 集合:是一些唯一的.不可变的对象(数值和字符串等)的一个无序的集合(collection).而且这些对象支持与数学集合理论相相应的操作. 特点: 1.一个项仅仅可以出现一次 2. ...

  10. 关于OC的内存管理-01

    1.什么是内存管理? 大家都知道手机的内存是有限的,app应用的内存也应该是受限制的,随着app应用的使用会导致内存的占用率增大.当内存占用率达到一种程度时.系统会发出内存警告.这时我们须要把一些不用 ...