CoderFocers-620C
There are n pearls in a row. Let's enumerate them with integers from 1 to n from the left to the right. The pearl number i has the type ai.
Let's call a sequence of consecutive pearls a segment. Let's call a segment good if it contains two pearls of the same type.
Split the row of the pearls to the maximal number of good segments. Note that each pearl should appear in exactly one segment of the partition.
As input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use scanf/printf instead of cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java.
Input
The first line contains integer n (1 ≤ n ≤ 3·105) — the number of pearls in a row.
The second line contains n integers ai (1 ≤ ai ≤ 109) – the type of the i-th pearl.
Output
On the first line print integer k — the maximal number of segments in a partition of the row.
Each of the next k lines should contain two integers lj, rj (1 ≤ lj ≤ rj ≤ n) — the number of the leftmost and the rightmost pearls in the j-th segment.
Note you should print the correct partition of the row of the pearls, so each pearl should be in exactly one segment and all segments should contain two pearls of the same type.
If there are several optimal solutions print any of them. You can print the segments in any order.
If there are no correct partitions of the row print the number "-1".
Example
5
1 2 3 4 1
1
1 5
5
1 2 3 4 5
-1
7
1 2 1 3 1 2 1
2
1 3
4 7
题解:简单的贪心题目,看代码就懂了,很简单。
AC代码为:
#include<iostream>
#include<cstdio>
#include<map>
#include<algorithm>
using namespace std;
const int maxn=3e5+10;
int a[maxn];
map<int ,int> mp;
struct node{
int l;
int r;
}s[maxn];
int main()
{
int k,li,ri,f=0,cnt=0;
li=ri=1;
scanf("%d",&k);
for(int i=1;i<=k;i++)
{
scanf("%d",&a[i]);
if(!mp.count(a[i]))
{
mp[a[i]]=i;
}
else
{
ri=i;
s[f].l =li;
s[f].r =ri;
f++;
li=i+1;
cnt++;
mp.clear();
}
}
s[f-1].r = k;
if(!cnt)
printf("-1");
else
{
printf("%d\n",cnt);
for(int i=0;i<f;i++)
{
printf("%d %d\n",s[i].l,s[i].r );
}
}
return 0;
}
CoderFocers-620C的更多相关文章
- 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 ...
- Pearls in a Row CodeForces 620C 水题
题目:http://codeforces.com/problemset/problem/620/C 文章末有一些测试数据仅供参考 题目大意 给你一个数字串,然后将分成几个部分,要求每个部分中必须有一对 ...
- 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 ...
- cf 620C Pearls in a Row(贪心)
d.有一串数字,要把这些数字分成若干连续的段,每段必须至少包含2个相同的数字,怎么分才能分的段数最多? 比如 是1 2 1 3 1 2 1 那么 答案是 21 34 7 即最多分在2段,第一段是1~3 ...
- 【32.26%】【codeforces 620C】Pearls in a Row
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- codeforces 620C
题目链接:https://codeforces.com/problemset/problem/620/C 题目分析 题意:给你一串珍珠,每个珍珠都有一个对应值,需要分割这n个珍珠(必须连续),使得每一 ...
- utf-8 汉字对照表
之前从redis中取出一些数据,utf8 16进制编码,想转成字符,没有找到现成的转化工具,先用这个表直接查找对照吧. UTF8编码表大全Code code# Code (coded in UTF-8 ...
- JS base64 加密和 后台 base64解密(防止中文乱码)
直接上代码 1,js(2个文件,网上找的) 不要觉的长,直接复制下来就OK //UnicodeAnsi.js文件 //把Unicode转成Ansi和把Ansi转换成Unicode function ...
- 基于nodejs实现js后端化处理
今天D哥给我提了个问题,"用php执行过js没"?咋一听,没戏~~毕竟常规情况下,js是依赖浏览器运行的.想在php后端采集的同时利用js运行结果并传递给php使用,没戏! 然后回 ...
随机推荐
- Win7无法远程桌面
Win7在设置里开启允许其他计算机远程连接,但局域网计算机还是连不上: 然后点击上面的为远程桌面启用windows防火墙例外,发现远程桌面是允许的: 实际上这个不是,问题的真正原因在于用于远程的338 ...
- PHP 在Swoole中使用双IoC容器实现无污染的依赖注入
简介: 容器(container)技术(可以理解为全局的工厂方法), 已经是现代项目的标配. 基于容器, 可以进一步实现控制反转, 依赖注入. Laravel 的巨大成功就是构建在它非常强大的IoC容 ...
- 100天搞定机器学习|Day56 随机森林工作原理及调参实战(信用卡欺诈预测)
本文是对100天搞定机器学习|Day33-34 随机森林的补充 前文对随机森林的概念.工作原理.使用方法做了简单介绍,并提供了分类和回归的实例. 本期我们重点讲一下: 1.集成学习.Bagging和随 ...
- bert+seq2seq 周公解梦,看AI如何解析你的梦境?【转】
介绍 在参与的项目和产品中,涉及到模型和算法的需求,主要以自然语言处理(NLP)和知识图谱(KG)为主.NLP涉及面太广,而聚焦在具体场景下,想要生产落地的还需要花很多功夫. 作为NLP的主要方向,情 ...
- 在阿里云服务器中配置JDK、tomcat、mysql
阿里云服务器搭建配置 linux命令:参考:https://www.cnblogs.com/itdansan/p/8545187.html cat 文件名: 查看文件内容 ctrl+D : 退出查看 ...
- [ch02-02] 非线性反向传播
系列博客,原文在笔者所维护的github上:https://aka.ms/beginnerAI, 点击star加星不要吝啬,星越多笔者越努力. 2.2 非线性反向传播 2.2.1 提出问题 在上面的线 ...
- 异步任务AsyncTask使用解析
在Android中实现异步任务机制有两种方式,Handler和AsyncTask. Handler模式需要为每一个任务创建一个新的线程,任务完成后通过Handler实例向UI线程发送消息,完成界面的更 ...
- Java描述设计模式(24):备忘录模式
本文源码:GitHub·点这里 || GitEE·点这里 一.生活场景 1.场景描述 常见的视频播放软件都具备这样一个功能:假设在播放视频西游记,如果这时候切换播放视频红楼梦,当再次切回播放西游记时, ...
- 【绝对有收获】看看?必须告诉你为什么要使用MQ消息中间件(图解版)
欢迎关注文章系列 ,关注我 <提升能力,涨薪可待> <面试知识,工作可待> <实战演练,拒绝996> 也欢迎关注微信公众号[Ccww笔记],原创技术文章第一时间推出 ...
- Github上传大文件(超过100M)
上传大文件(超过100M)到Github 笔者上传操作100M的文件到Github,结果在push的时候会自动终止.然后提示无法上传大文件,就算删除再提交也是报错. 于是,本人写这篇博客就是为了解决这 ...