Codeforces 297C. Splitting the Uniqueness
Polar bears like unique arrays — that is, arrays without repeated elements.
You have got a unique array s with length n containing non-negative integers. Since you are good friends with Alice and Bob, you decide to split the array in two. Precisely, you need to construct two arrays a and b that are also of length n, with the following conditions for all i(1 ≤ i ≤ n):
- ai, bi are non-negative integers;
- si = ai + bi .
Ideally, a and b should also be unique arrays. However, life in the Arctic is hard and this is not always possible. Fortunately, Alice and Bob are still happy if their arrays are almost unique. We define an array of length n to be almost unique, if and only if it can be turned into a unique array by removing no more than entries.
For example, the array [1, 2, 1, 3, 2] is almost unique because after removing the first two entries, it becomes [1, 3, 2]. The array [1, 2, 1, 3, 1, 2] is not almost unique because we need to remove at least 3 entries to turn it into a unique array.
So, your task is to split the given unique array s into two almost unique arrays a and b.
The first line of the input contains integer n (1 ≤ n ≤ 105).
The second line contains n distinct integers s1, s2, ... sn (0 ≤ si ≤ 109).
If it is possible to make Alice and Bob happy (if you can split the given array), print "YES" (without quotes) in the first line. In the second line, print the array a. In the third line, print the array b. There may be more than one solution. Any of them will be accepted.
If it is impossible to split s into almost unique arrays a and b, print "NO" (without quotes) in the first line.
6
12 5 8 3 11 9
YES
6 2 6 0 2 4
6 3 2 3 9 5
In the sample, we can remove the first two entries from a and the second entry from b to make them both unique.
分析:
因为一个序列如果是近似不同的的序列,那么它有至少 ⌊ 2n / 3⌋的元素是不同的...所以我们把s序列分成三个部分,第一个部分保证a互不相同,第二个部分保证b互不相同,第三个部分保证ab都互不相同...如下图:
设x=n/3(上取整)
i∈[1,x] a=i-1
i∈[x+1,n-x] b=i-1
i∈[n-x+1,n] b=n-i+1
代码:
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
//by NeighThorn
using namespace std;
//眉眼如初,岁月如故 const int maxn=100000+5; int n,x; struct M{
int a,b,s,id;
friend bool operator < (M a,M b){
return a.s<b.s;
}
}sxy[maxn]; inline bool cmp(M a,M b){
return a.id<b.id;
} signed main(void){
scanf("%d",&n);x=(n+2)/3;
for(int i=1;i<=n;i++)
scanf("%d",&sxy[i].s),sxy[i].id=i;
sort(sxy+1,sxy+n+1);
for(int i=1;i<=x;i++)
sxy[i].a=i-1,sxy[i].b=sxy[i].s-i+1;
for(int i=x+1;i<=n-x;i++)
sxy[i].b=i-1,sxy[i].a=sxy[i].s-i+1;
for(int i=n-x+1;i<=n;i++)
sxy[i].b=n-i,sxy[i].a=sxy[i].s-sxy[i].b;
sort(sxy+1,sxy+n+1,cmp);puts("YES");
for(int i=1;i<=n;i++)
printf("%d ",sxy[i].a);
puts("");
for(int i=1;i<=n;i++)
printf("%d ",sxy[i].b);
puts("");
return 0;
}//Cap ou pas cap. Pas cap.
By NeighThorn
Codeforces 297C. Splitting the Uniqueness的更多相关文章
- CodeForces 297C Splitting the Uniqueness (脑补构造题)
题意 Split a unique array into two almost unique arrays. unique arrays指数组各个数均不相同,almost unique arrays指 ...
- Codeforces.297C.Splitting the Uniqueness(构造)
题目链接 \(Description\) 给定一个长为n的序列A,求两个长为n的序列B,C,对任意的i满足B[i]+C[i]=A[i],且B,C序列分别至少有\(\lfloor\frac{2*n}{3 ...
- 【CodeForces 297C】Splitting the Uniqueness
题意 序列s有n个数,每个数都是不同的,把它每个数分成两个数,组成两个序列a和b,使ab序列各自去掉个数后各自的其它数字都不同. 如果存在一个划分,就输出YES,并且输出两个序列,否则输出NO. 分析 ...
- Codeforces Round #180 (Div. 1 + Div. 2)
A. Snow Footprints 如果只有L或者只有R,那么起点和终点都在边界上,否则在两者的边界. B. Sail 每次根据移动后的曼哈顿距离来判断是否移动. C. Parity Game 如果 ...
- Educational Codeforces Round 4 A. The Text Splitting 水题
A. The Text Splitting 题目连接: http://www.codeforces.com/contest/612/problem/A Description You are give ...
- Codeforces 754A Lesha and array splitting(简单贪心)
A. Lesha and array splitting time limit per test:2 seconds memory limit per test:256 megabytes input ...
- Codeforces Round #452 (Div. 2)-899A.Splitting in Teams 899B.Months and Years 899C.Dividing the numbers(规律题)
A. Splitting in Teams time limit per test 1 second memory limit per test 256 megabytes input standar ...
- Codeforces Round #440 (Div. 2, based on Technocup 2018 Elimination Round 2) C. Maximum splitting
地址: 题目: C. Maximum splitting time limit per test 2 seconds memory limit per test 256 megabytes input ...
- Codeforces Round #390 (Div. 2) A. Lesha and array splitting
http://codeforces.com/contest/754/problem/A 题意: 给出一串序列,现在要把这串序列分成多个序列,使得每一个序列的sum都不为0. 思路: 先统计一下不为0的 ...
随机推荐
- Mysql 查询出某列字段 被包含于 条件数据中
我们通常是使用 某条件 是否包含于 某列中 ,简单点 就是:select * from 表名 where 字段名 like '%条件数据%'; 现在说下 某列 被包含于 条件数据中 接下 ...
- k8s资源配置清单的书写格式(yaml文件)
yaml文件书写格式:5大类:apiVersion: 选择kubectl api-versions里面存在的版本kind: 选择kubectl api-resources结果中的对象资源metadat ...
- IDEA整合Mybatis+Struts2+Spring(一)--新建项目
1.IDEA新建Maven项目: (1)依次点击File->New->Project,弹出如下对话框: (2)在弹出的New Project页面上,①选择Maven,② 勾选Create ...
- php中处理字符串的常见函数
编写程序的时候,经常要处理字符串,最基本就是字符串的查找,在php检测字符串中是否包含指定字符串可以使用正则,如果你对正则不了解,那么有几个函数可以为您提供方便. 1. strstr strstr() ...
- ubuntu crontab设置定时任务
ubuntu 设置定时任务 crontab -l #查看详情crontab -e #设置定时任务 * * * * * command 分 时 日 月 周 命令 第1列表示分钟1-59 每分钟用* ...
- 遗传算法 | C++版GA_TSP
嗯哼,时隔半年,再次有时间整理关于组合优化问题——旅行商问题(Traveling Salesman Problem, TSP),这次采用的是经典遗传算法(Genetic Algorithm, GA)进 ...
- ZOJ 3231 Apple Transportation 树DP
一.前言 红书上面推荐的题目,在138页,提到了关键部分的题解,但是实际上他没提到的还有若干不太好实现的地方.尤其是在这道题是大家都拿网络流玩弄的大背景下,这个代码打不出来就相当的揪心了..最后在牛客 ...
- Kali 网络配置
一.配置IP 编辑/etc/network/interfaces # This file describes the network interfaces available on your syst ...
- vba中ListBox控件的使用
给ListBox添加内容 If CheckBox8 = True Then---------------------------checkbox控件被选中 For i = 0 To ListBox1. ...
- Python 内置函数isinstance
isinstance 用来判断对象的类型 isinstance(对象,类型/类型集合) 如果属于 返回True 不属于返回 False >>> a = 1 >>> ...