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. net8:文本文件的创建及其读写

    原文发布时间为:2008-08-06 -- 来源于本人的百度文章 [由搬家工具导入] using System;using System.Data;using System.Configuration ...

  2. App后台运行通知函数

    
[[UIApplicationsharedApplication] beginBackgroundTaskWithExpirationHandler: ^() { //程序在10分钟内未被系统关闭或 ...

  3. PHP html_entity_decode() 函数

    html_entity_decode(string,flags,character-set) 把 HTML 实体转换为字符. html_entity_decode() 函数是 htmlentities ...

  4. 服务器Out of Memory

    之前有次把图片存储在数据库,结果读取时候报错了:Out of Memory.. 图片本来不应该存储在数据库中的,消耗内存太大,既然已经这样,那就先解决问题,不改存储方式. 如果一个应用程序为了提高性能 ...

  5. widows 2008 同步时间命令

    由于windows2008没有提供类似XP的自动同步功能,因此需要使用windows 2008计划任务来运行一行命令进行同步.   首先查看与想要同步时间的internet时间服务器的时差: w32t ...

  6. 常见的哈希Hash算法 & MD5 & 对称非对称加密 & 海明码

    参考 Link 另外,这篇文章也提到了利用Hash碰撞而产生DOS攻击的案例: http://www.cnblogs.com/charlesblc/p/5990475.html DJB的算法实现核心是 ...

  7. IE浏览器打不开解决的方法

    windows 7和windows 8上的IE浏览器打不开.非常可能是权限问题,解决的方法: 点击"開始"-"执行",输入"regedit" ...

  8. Qt在线技术交流之OpenGL、Quick以及所经历项目开发心得分享

    时间:3月25日晚上7:30 主题:Qt在线技术交流之OpenGL.Quick以及所经历项目开发心得分享 直播:http://qtdream.com 主页.全民TV,可能会加上其他的直播平台进行转播 ...

  9. poj 3105 Expectation 按位统计

    题意: 给n,求sum(i^j)/(n^2),0<=i,j<n.n<10^9 分析: 暴力n^2算法肯定超时.这是logn按位统计算法:按位先算出0出现的个数x,则1出现的个数为n- ...

  10. 设计模式学习笔记——Decorator装饰模式

    装饰模式的作用或动机就是,尽量避免继承,而使用关联.原因是层层继承下来,内容会越来越多,有失控的危险.就扩展性而言,用关联比用继承好.所谓的关联,A使用了B,就叫A关联了B. Component 抽象 ...