codeforces 620C
题目链接:https://codeforces.com/problemset/problem/620/C
题目分析
题意:给你一串珍珠,每个珍珠都有一个对应值,需要分割这n个珍珠(必须连续),使得每一串珍珠中含有对应值相同的两个珍珠,并让这个样的珍珠串数目达到最多。
首先看到这个题目的时候想用数组保存各个数出现的位置的,但是由于题目的要求,数组太大,故改用map来保存各个数出现的位置。
具体实现,用 s 代表当前珍珠串的起点, e 代表当前搜索的位置, 开始时 s == e ,实现用map通过第e个珍珠的对应值,来访问为这个珍珠的位置 e 。 如果当前位置的珍珠 e 满足
map[e] != 0 ,说明在区间[s,e-1] 含有和珍珠 e 相同的珍珠,也就是说 [ s,e] 是可以分割的最小珍珠串,分割了当前串后,将 s = e +1 ,e = s ,分割位置后移,准备分割下一串珍珠。
最后,由于题目要求n个珍珠必须分割完毕,所以最后一个珍珠串需要包含到最后一个珍珠(前提是至少有一个满足条件的珍珠串)
代码区
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include <set>
#include <map>
typedef long long ll;
using namespace std;
const int Max = 1e9;
const int Max2 = 3e5 + ;
const int inf = 0x3f3f3f3f; int v[Max2];
map<int, int>m; //前一个数为数值,后一个为位置
pair<int, int>p[Max2]; int main()
{
int n;
while (scanf("%d", &n) != EOF)
{
int sum = ;
for (int i = ; i <= n; i++)
{
scanf("%d", v + i);
}
int s = , e = ;
m.clear();
while (e <= n)
{
if (m[v[e]]) //出现过
{
p[++sum] = { s,e };
s = e + ;
e = s;
m.clear();
}
else
{
m[v[e]] = e;
e++;
}
}
p[sum].second = n; //wa了一次,发现所以的珍珠都需要被使用
if (sum == )
{
printf("-1\n");
continue;
}
printf("%d\n", sum);
for (int i = ; i <= sum; i++)
{
printf("%d %d\n", p[i].first, p[i].second);
} }
return ;
}
codeforces 620C的更多相关文章
- Pearls in a Row CodeForces 620C 水题
题目:http://codeforces.com/problemset/problem/620/C 文章末有一些测试数据仅供参考 题目大意 给你一个数字串,然后将分成几个部分,要求每个部分中必须有一对 ...
- CodeForces 620C Pearls in a Row
水题,每当出现重复就分割开来,最后留下的尾巴给最后一段 #include<cstdio> #include<cstring> #include<cmath> #in ...
- 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 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 ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
随机推荐
- Java线程之如何分析死锁及避免死锁
什么是死锁 java中的死锁是一种编程情况,其中两个或多个线程被永久阻塞,Java死锁情况出现至少两个线程和两个或更多资源. 在这里,我们将写了一个简单的程序,它将导致java死锁场景,然后我们将分析 ...
- 0.spring cloud目录
1. 微服务架构概述 1.0. 单体架构是什么 1.1. 单体应用架构存在的问题 1.2. 如何解决单体应用架构存在的问题 1.3. 什么是微服务 1.4. 微服务架构的优点与挑战 1.4.1. 微服 ...
- 《SVG精髓》笔记(二)
3. 文档结构 在SVG中使用样式(四种方式,可以联想对照HTML样式方法) 内联样式, 直接在标签里设置style属性 <circle cx='20' cy='20' r='10' style ...
- Laravel 中如何区别 Model 或者是 Builder?
User::where('id',1)->update([]) 和 User::find(1)->update([]) 有异曲同工之效. 额? 当你通过 Laravel 与数据库交 ...
- Break 和 Continue 标签 kotlin(12)
Break 和 Continue 标签 在 Kotlin 中任何表达式都可以用标签(label ) 来标记. 标签的格式为标识符后跟 @ 符 号,例如: abc@ . fooBar@ 都是有效的标签( ...
- Android SDK更新后Eclipse无法正常工作问题
一,问题描述 更新完Android SDK后,如果你的ADT版本低于其设定的最新版本,你更新完后立马会报错 这个时候你的项目出现各种红线,反正看着让人很不爽 二,原因 每次你更新完SDK后,Andro ...
- Delphi之TClientSocket和TServerSocket使用tcp keepalive心跳机制实现“断网”、"断电"检测
开发环境:Delphi7 测试环境:WinXP,Win7 32bit,Win7 64bit 使用TClientSocket和TServerSocket实现TCP长连接通讯,经常因为断电断网等原因导致 ...
- Fragment的生命周期和activity如何的一个关系
- python汉字编解码问题
http://www.cnblogs.com/rollenholt/archive/2011/08/01/2123889.html
- JMeter使用plugins插件进行服务器性能监控
JMeter使用plugins插件进行服务器性能监控 性能测试时,我们的关注点有两部分 1 服务本身:并发响应时间 QPS 2 服务器的资源使用情况:cpu memory I/O disk等 JMet ...