题目链接: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的更多相关文章

  1. Pearls in a Row CodeForces 620C 水题

    题目:http://codeforces.com/problemset/problem/620/C 文章末有一些测试数据仅供参考 题目大意 给你一个数字串,然后将分成几个部分,要求每个部分中必须有一对 ...

  2. CodeForces 620C Pearls in a Row

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

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

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

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

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

  6. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  7. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  8. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  9. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

随机推荐

  1. Codeforces Gym 101630J Travelling from Petersburg to Moscow (最短路)

    题目链接 http://codeforces.com/gym/101630/attachments 题解 zyb学长的题. 先枚举第\(k\)大的边权,设其边权为\(x\),然后把每条边边权减掉\(x ...

  2. Java集合框架之TreeSet

    简述 TreeSet是基于TreeMap作为存储的可排序.可去重的有序集合 继承于AbstractSet,AbstractSet实现了equals和hashcode方法 实现了NavigableSet ...

  3. selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document

    抓取网页代码后,由于是在同一个li标签下,所以使用一次性抓取,所有的a标签,然后循环做不同的操作,但是抛出找不到元素异常. def office_page(_chrome: Chrome): sn = ...

  4. [CSP-S模拟测试]:B(DP+数学)

    题目传送门(内部题45) 输入格式 第一行$3$个整数$n,m,P$.第二行$m$个整数,表示$m$次询问. 输出格式 一行$m$个整数表示答案. 样例 样例输入1: 2 4 40 1 2 3 样例输 ...

  5. Inter IPP 处理图像数据的方法

    Inter IPP没有读取图片和保存图片的函数,需要结合opencv完成这个功能. opencv读到图片以后逐个像素点赋值给IPP显然是不可取的,方法如下: int main(int argc, ch ...

  6. HearthBuddy卡组

    偶数萨 手打两天已上传说,各位加油  欧洲牧羊人 ### 火元素换艾雅# 职业:萨满祭司# 模式:狂野模式## 2x (2) 图腾魔像        # 2x (2) 大漩涡传送门   # 2x (2 ...

  7. 往Angular应用程序中添加DevExtreme

    To start this tutorial, you need an Angular 5+ application created using the Angular CLI. Refer to t ...

  8. 大牛们是如何开发 WordPress 主题的?

    自己算是写过一个主题,目前也在用( 这里 -> http://udonmai.com/ ),所以多少想说两句. 当初走上web开发的路之后最想干的事情就是写个自己的WP主题...所以网上搜罗了很 ...

  9. eslint 检查单个文件的错误

    问题: 批量检查时,没有针对性,想针对单个文件进行检查 解决办法:./node_modules/.bin/eslint  your file

  10. [go]从os.Stdin探究文件类源码

    咋一看go的标准输入输出函数有一箩筐. 细究了一下. - 从标准输入获取输入 fmt.Scan 以空白(空格或换行)分割,值满后无结束 fmt.Scanln 以空格作为分割,遇到换行结束 fmt.Sc ...