Educational Codeforces Round 6 620C Pearls in a Row(stl)

本文介绍了一种将连续排列的珍珠分成尽可能多的相同类型的连续段落的方法,并提供了实现该策略的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C. Pearls in a Row
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
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/printfinstead 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".

Sample test(s)
input
5
1 2 3 4 1
output
1
1 5
input
5
1 2 3 4 5
output
-1
input
7
1 2 1 3 1 2 1
output
2
1 3
4 7


题目链接:点击打开链接

给出一个序列, 一段子序列含有两个相同的数字称为happy segment, 要求分为最多的happy segment, 输出每段序列起点终点.

考虑存储数据的数据结构, 使用set解决此问题, 遇到重复的元素则存储位置且清空set, 同时要满足第一段序列的起点为1且最后一段序

列的终点为n 即可.

AC 代码:

#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"
#include "queue"
#include "stack"
#include "cmath"
#include "utility"
#include "map"
#include "set"
#include "vector"
#include "list"
#include "string"
#include "cstdlib"
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const int MAXN = 3e5 + 5;
int n, pos[MAXN], k;
set<int> s;
int main(int argc, char const *argv[])
{
	scanf("%d", &n);
	for(int i = 1; i <= n; ++i) {
		int x;
		scanf("%d", &x);
		if(s.count(x) == 0) s.insert(x);
		else {
			pos[k++] = i;
			s.clear();
		}
	}
	if(k == 0) printf("-1\n");
	else {
		pos[k - 1] = n;
		printf("%d\n", k);
		printf("1 %d\n", pos[0]);
		for(int i = 0; i < k - 1; ++i)
			printf("%d %d\n", pos[i] + 1, pos[i + 1]);
	}
	return 0;
}


### 关于Codeforces Educational Round 171 #### A. The Contest 题目描述指出,在决定举办一场比赛之后,参赛者需要准备一系列材料,包括但不限于问题陈述、解决方案等。然而,协调员可能会突然要求更改某些测试案例的形式[^1]。 对于A题《The Contest》,时间限制为每组测试2秒,内存限制为256兆字节。输入来自标准输入流,输出至标准输出流[^3]。此题目的核心在于处理多组测试数据的情况,这通常意味着程序应当能够高效读取并解析多个独立的测试实例,并分别给出解答。 #### 输入格式说明 根据以往的比赛经验,输入的第一行可能包含两个整数`n`和`m`(1 ≤ n, m ≤ 5·10^5),表示用户的数量以及朋友关系集合的数量[^2]。不过需要注意的是,具体到Educational Round 171中的实际数值范围和其他细节会有所不同,请参照官方发布的最新信息为准。 #### 示例代码实现(Python) 下面是一个简单的模板用于解决涉及多组测试的数据结构化编程挑战: ```python def solve_problem(input_data): results = [] t = int(input()) # 获取总的测试次数t for _ in range(t): # 解析每一组测试的具体参数 params = list(map(int, input().split())) result = process(params) # 假设process函数实现了具体的逻辑运算 results.append(result) return "\n".join(str(x) for x in results) if __name__ == "__main__": from sys import stdin, stdout print(solve_problem(stdin)) ``` 该脚本展示了如何接收多轮次的标准输入并将它们作为单独的任务来执行,最后统一输出所有的计算结果。请注意调整`params`变量的内容以适应特定问题的需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值