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. POJ 2411 状压dp

    F - Mondriaan's Dream Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I6 ...

  2. Laravel 视图中的url

    <a href="{{ url('url') }}">url</a> <a href="{{ action('StudentControll ...

  3. CodeForces - 813C The Tag Game (树的dfs遍历)

    [传送门]http://codeforces.com/problemset/problem/813/C [题目大意]两个人玩游戏,一个人跑一个人追,轮流决策,可以走也可以不走.给你一棵树,想要从某个结 ...

  4. 2018 ICPC 徐州网络预赛 Features Track (STL map pair)

    [传送门]https://nanti.jisuanke.com/t/31458 [题目大意]有N个帧,每帧有K个动作特征,每个特征用一个向量表示(x,y).两个特征相同当且仅当他们在不同的帧中出现且向 ...

  5. All you need to know about SYN floods

    http://blog.dubbelboer.com/ Date: 09 Apr 2012Author: Erik Dubbelboer SYN cookies So one day I notice ...

  6. 【PostgreSQL】安装使用步骤

    1.下载地址 https://www.postgresql.org/download/windows/ 下载按照较新版本,和平台相一致就好 2.安装 选择安装地址 数据存放地址 密码设置 端口使用默认 ...

  7. Linux常用C函数-接口处理篇(网络通信函数)

    接口处理篇accept,bind,connect,endprotoent,endservent,getsockopt,htonl,htons,inet_addr,inet_aton,inet_ntoa ...

  8. awk批量处理文件,对第一列去重并,累加第二列数值,打印一二列存入新文件

    awk '{if(NR>1)a[$1]+=$2}END{for(i in a)printf "%s\t %d\n",i,a[i]}' querylog* > total ...

  9. Sentinel实现Redis高可用

    实现目标: 一主两从,集群起始VIP在master上边,如果当前master挂了,sentinel自动选出一个slave当选master,并把VIP漂移到这台机器,然后把另一台slave指向的mast ...

  10. android 获取屏幕宽高 和 获取控件坐标

    一.获取屏幕宽高: (1). WindowManager wm = (WindowManager)getSystemService(Context.WINDOW_SERVICE); int width ...