Educational Codeforces Round 6 C
2 seconds
256 megabytes
standard input
standard output
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.
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.
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".
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 题意 n 给一段长为n的数字 问最多有多少段 每段要求:有两个相同的数字
题解 贪心处理 直接贪心会超时 set 处理 注意最后一段的右区间一定为n 2000ms 超时代码
#include<bits/stdc++.h>
using namespace std;
int n;
struct node
{
int l;
int r;
}m[300005];
int a[300005];
int main()
{
scanf("%d",&n);
int st=0;
int jishu=0;
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
for(int j=st+1;j<i;j++)
{
if(a[j]==a[i])
{
m[jishu].l=st+1;
m[jishu].r=i;
jishu++;
st=i;
}
}
}
if(jishu==0)
printf("-1\n");
else
{
printf("%d\n",jishu);
for(int i=0;i<jishu-1;i++)
{
printf("%d %d\n",m[i].l,m[i].r);
}
printf("%d %d\n",m[jishu-1].l,n);
}
return 0;
}
set 处理 218ms
#include<bits/stdc++.h>
using namespace std;
struct node
{
int l;
int r;
}m[300005];
int exm;
set<int>s;
set<int>::iterator it;
int n;
int main()
{
scanf("%d",&n);
int jishu=0;
for(int i=1;i<=n;)
{
m[jishu].l=i;
while(i<=n)
{
scanf("%d",&exm);
i++;
it=s.find(exm);
if(it!=s.end())
{
s.clear();
m[jishu].r=i-1;
jishu++;
break;
}
else
s.insert(exm);
}
}
if(jishu==0)
printf("-1\n");
else
{
printf("%d\n",jishu);
for(int i=0;i<jishu-1;i++)
{
printf("%d %d\n",m[i].l,m[i].r);
}
printf("%d %d\n",m[jishu-1].l,n);
}
return 0;
}
Educational Codeforces Round 6 C的更多相关文章
- [Educational Codeforces Round 16]E. Generate a String
[Educational Codeforces Round 16]E. Generate a String 试题描述 zscoder wants to generate an input file f ...
- [Educational Codeforces Round 16]D. Two Arithmetic Progressions
[Educational Codeforces Round 16]D. Two Arithmetic Progressions 试题描述 You are given two arithmetic pr ...
- [Educational Codeforces Round 16]C. Magic Odd Square
[Educational Codeforces Round 16]C. Magic Odd Square 试题描述 Find an n × n matrix with different number ...
- [Educational Codeforces Round 16]B. Optimal Point on a Line
[Educational Codeforces Round 16]B. Optimal Point on a Line 试题描述 You are given n points on a line wi ...
- [Educational Codeforces Round 16]A. King Moves
[Educational Codeforces Round 16]A. King Moves 试题描述 The only king stands on the standard chess board ...
- Educational Codeforces Round 6 C. Pearls in a Row
Educational Codeforces Round 6 C. Pearls in a Row 题意:一个3e5范围的序列:要你分成最多数量的子序列,其中子序列必须是只有两个数相同, 其余的数只能 ...
- Educational Codeforces Round 9
Educational Codeforces Round 9 Longest Subsequence 题目描述:给出一个序列,从中抽出若干个数,使它们的公倍数小于等于\(m\),问最多能抽出多少个数, ...
- Educational Codeforces Round 37
Educational Codeforces Round 37 这场有点炸,题目比较水,但只做了3题QAQ.还是实力不够啊! 写下题解算了--(写的比较粗糙,细节或者bug可以私聊2333) A. W ...
- Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship
Problem Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship Time Limit: 2000 mSec P ...
- Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems(动态规划+矩阵快速幂)
Problem Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems Time Limit: 3000 mSec P ...
随机推荐
- JVM--Java类加载机制
一.什么是类的加载 类的加载指的是将类的.class文件中的二进制数据读入到内存中,将其存放在运行时数据区的方法区内,然后在java堆区创建一个java.lang.Class对象,用来封装类在方法区内 ...
- 天平 (Not so Mobile UVA - 839)
题目描述: 题目思路: 1.DFS建树 2.只有每个树的左右子树都平衡整颗树才平衡 #include <iostream> using namespace std; bool solve( ...
- Vuejs 实现简易 todoList 功能 与 组件
todoList 结合之前 Vuejs 基础与语法 使用 v-model 双向绑定 input 输入内容与数据 data 使用 @click 和 methods 关联事件 使用 v-for 进行数据循 ...
- 【zabbix 监控】第一章 zabbix的安装配置
安装前准备 一.下载网络yum源: http://mirrors.163.com/.help/centos.html https://opsx.alibaba.com/mirror 1.首先备份/et ...
- LogisticRegression Algorithm——机器学习(西瓜书)读书笔记
import numpy as np from sklearn.datasets import load_breast_cancer import sklearn.linear_model from ...
- Java 单例模式探讨
以下是我再次研究单例(Java 单例模式缺点)时在网上收集的资料,相信你们看完就对单例完全掌握了 Java单例模式应该是看起来以及用起来简单的一种设计模式,但是就实现方式以及原理来说,也并不浅显哦. ...
- centos配置iptables
第一次配置前消除默认的规则 #这个一定要先做,不然清空后可能会悲剧 iptables -P INPUT ACCEPT #清空默认所有规则 iptables -F #清空自定义的所有规则 iptable ...
- 第二次作业(1001.A+B Format (20))
代码文件及题目描写已放至此 一开始看题目的时候有点没看懂,要求把数分组是什么意思.如果只是单纯的a+b的话是不可能的,所以关于这一点犹豫了很久.本来以为是指把a,b,以及它们的和c各建一个数组,但只输 ...
- week1 四人小组项目
小组名称:nice! 项目组长:李权 组员:于淼 刘芳芳 杨柳 项目选题:东北师范大学论坛 作为东北师范大学同学间的信息交流平台,要满足的需求如下: 1.校内信息及公告 2.毕业生招聘信息 3.课程查 ...
- Jenkins系列-Jenkins邮件通知
一.安装邮件插件 由于Jenkins自带的邮件功能比较鸡肋,因此这里推荐安装专门的邮件插件,不过下面也会顺带介绍如何配置Jenkins自带的邮件功能作用. 可以通过系统管理→管理插件→可选插件,选择E ...