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 ...
随机推荐
- Cucumber Vs RobotFramework
I asked this same question a little over a year ago on this list when we were at your stage. Before ...
- Spring 定时器 定时访问数据库并发送邮件
我这里有两个案例的方法: 第一种:使用Spring quartz: 我这里使用的jar:spring-context-support.jar.quartz-1.6.5.jar ============ ...
- .net core 使用 codegenerator 创建默认CRUD代码
dotnet.exe aspnet-codegenerator controller --force --controllerName [controller-name] --relativeFold ...
- Jetson TK1 四:重新安装系统(刷机)
转载:http://blog.sina.com.cn/s/blog_bab3fa030102vk21.html Jetson TK1是NVIDIA基于Tegra K1开发的一块低成本开发板,板载一块T ...
- Spring 详解(三)------- SpringMVC拦截器使用
目录 不拦截静态资源 使用拦截器 拦截器使用测试 SimpleMappingExceptionResolver 拦截异常 不拦截静态资源 如果配置拦截类似于*.do格式的拦截规则,则对静态资源的访问是 ...
- Spring实战Day5
3.3自动装配bean的歧义性 产生歧义的原因 找到多个符合条件的组件,如下注入talent时会有两个满足条件的组件 解决方法 标示首选的bean,但是同时标示两个或多个同样会存在歧义 自动装配标示P ...
- BUPT复试专题—科学计算器(2009)
题目描述 给你一个不带括号的表达式,这个表达式只包含加.减.乘.除,请求出这个表 达式的最后结果,最后结果一定是整数: 输入 一个数学表达式,只包括数字,数字保证是非负整数,以及五种运算符 " ...
- Linux学习系列之Iptables
iptables命令是Linux上常用的防火墙软件,是netfilter项目的一部分.可以直接配置,也可以通过许多前端和图形界面配置. 语法 iptables(选项)(参数) 选项 -t<表&g ...
- python 学习笔记 13 -- 经常使用的时间模块之time
Python 没有包括相应日期和时间的内置类型.只是提供了3个相应的模块,能够採用多种表示管理日期和时间值: * time 模块由底层C库提供与时间相关的函数.它包括一些函数用于获取时钟时间和处 ...
- c 链表之 快慢指针 查找循环节点(转)
上面分析了 根据这张图 推倒出 数学公式. 刚接触 不能一下弄明白.下面结合上面文章的分析.仔细推倒一下 , 一般设置 快指针 速度是 慢指针的2倍.及 快指针每次遍历两个指针, 慢指针每次遍历1个指 ...