Codeforce 931 C. Laboratory Work
2018-03-10 11:53
369 查看
C. Laboratory Worktime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputAnya and Kirill are doing a physics laboratory work. In one of the tasks they have to measure some value n times, and then compute the average value to lower the error.Kirill has already made his measurements, and has got the following integer values: x1, x2, ..., xn. It is important that the values are close to each other, namely, the difference between the maximum value and the minimum value is at most 2.Anya does not want to make the measurements, however, she can't just copy the values from Kirill's work, because the error of each measurement is a random value, and this coincidence will be noted by the teacher. Anya wants to write such integer values y1, y2, ..., yn in her work, that the following conditions are met:the average value of x1, x2, ..., xn is equal to the average value of y1, y2, ..., yn;
all Anya's measurements are in the same bounds as all Kirill's measurements, that is, the maximum value among Anya's values is not greater than the maximum value among Kirill's values, and the minimum value among Anya's values is not less than the minimum value among Kirill's values;
the number of equal measurements in Anya's work and Kirill's work is as small as possible among options with the previous conditions met. Formally, the teacher goes through all Anya's values one by one, if there is equal value in Kirill's work and it is not strike off yet, he strikes off this Anya's value and one of equal values in Kirill's work. The number of equal measurements is then the total number of strike off values in Anya's work.
Help Anya to write such a set of measurements that the conditions above are met.InputThe first line contains a single integer n (1 ≤ n ≤ 100 000) — the numeber of measurements made by Kirill.The second line contains a sequence of integers x1, x2, ..., xn ( - 100 000 ≤ xi ≤ 100 000) — the measurements made by Kirill. It is guaranteed that the difference between the maximum and minimum values among values x1, x2, ..., xn does not exceed 2.OutputIn the first line print the minimum possible number of equal measurements.In the second line print n integers y1, y2, ..., yn — the values Anya should write. You can print the integers in arbitrary order. Keep in mind that the minimum value among Anya's values should be not less that the minimum among Kirill's values, and the maximum among Anya's values should be not greater than the maximum among Kirill's values.If there are multiple answers, print any of them.ExamplesinputCopy
一串数字,每个数字之间相差不超过2,要求再写出一串数字,与原来那串数字的和相同,个数相同,与原串尽可能不同。
思路
将一个最大数和一个最小数变为2个中间数,将两个中间数变为一个最大数和一个最小数取最优
#include<bits/stdc++.h>
#define maxn 100010
#define ll long long
using namespace std;
bool cmp(int a,int b)
{
return a>b;
}
int main()
{
int n;
int a[maxn],b[maxn],c[maxn];
while(scanf("%d",&n)!=EOF)
{
ll sum=0;
int Max=-maxn,Min=maxn;
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
Max=max(Max,a[i]);
Min=min(Min,a[i]);
sum+=a[i];
}
if(Max-Min==0||Max-Min==1)
{
printf("%d\n",n);
sort(a,a+n,cmp);
for(int i=0;i<n-1;i++)
printf("%d ",a[i]);
printf("%d\n",a[n-1]);
}
else
{
int Mid=Max-1;
sort(a,a+n,cmp);
for(int i=0;i<n;i++)
b[i]=a[i];
for(int i=0;i<n/2;i++)
{
if(b[i]==Max&&b[n-1-i]==Min)
{
b[i]=Mid;
b[n-1-i]=Mid;
}
}
sort(b,b+n,cmp);
int ans1=0;
for(int i=0;i<n;i++)
{
if(a[i]==b[i])
ans1++;
}
for(int i=0;i<n;i++)
c[i]=a[i];
for(int i=0;i<n;i++)
{
if(c[i]==Mid&&c[i+1]==Mid)
{
c[i]=Max;
c[i+1]=Min;
}
}
sort(c,c+n,cmp);
int ans2=0;
for(int i=0;i<n;i++)
{
if(a[i]==c[i])
ans2++;
}
if(ans1<ans2)
{
printf("%d\n",ans1);
for(int i=0;i<n-1;i++)
printf("%d ",b[i]);
printf("%d\n",b[n-1]);
}
else
{
printf("%d\n",ans2);
for(int i=0;i<n-1;i++)
printf("%d ",c[i]);
printf("%d\n",c[n-1]);
}
}
}
return 0;
}
all Anya's measurements are in the same bounds as all Kirill's measurements, that is, the maximum value among Anya's values is not greater than the maximum value among Kirill's values, and the minimum value among Anya's values is not less than the minimum value among Kirill's values;
the number of equal measurements in Anya's work and Kirill's work is as small as possible among options with the previous conditions met. Formally, the teacher goes through all Anya's values one by one, if there is equal value in Kirill's work and it is not strike off yet, he strikes off this Anya's value and one of equal values in Kirill's work. The number of equal measurements is then the total number of strike off values in Anya's work.
Help Anya to write such a set of measurements that the conditions above are met.InputThe first line contains a single integer n (1 ≤ n ≤ 100 000) — the numeber of measurements made by Kirill.The second line contains a sequence of integers x1, x2, ..., xn ( - 100 000 ≤ xi ≤ 100 000) — the measurements made by Kirill. It is guaranteed that the difference between the maximum and minimum values among values x1, x2, ..., xn does not exceed 2.OutputIn the first line print the minimum possible number of equal measurements.In the second line print n integers y1, y2, ..., yn — the values Anya should write. You can print the integers in arbitrary order. Keep in mind that the minimum value among Anya's values should be not less that the minimum among Kirill's values, and the maximum among Anya's values should be not greater than the maximum among Kirill's values.If there are multiple answers, print any of them.ExamplesinputCopy
6 -1 1 1 0 0 -1output
2 0 0 0 0 0 0inputCopy
3 100 100 101output
3 101 100 100inputCopy
7 -10 -9 -10 -8 -10 -9 -9output
5 -10 -10 -9 -9 -9 -9 -9NoteIn the first example Anya can write zeros as here measurements results. The average value is then equal to the average value of Kirill's values, and there are only two equal measurements.In the second example Anya should write two values 100 and one value 101 (in any order), because it is the only possibility to make the average be the equal to the average of Kirill's values. Thus, all three measurements are equal.In the third example the number of equal measurements is 5.题意
一串数字,每个数字之间相差不超过2,要求再写出一串数字,与原来那串数字的和相同,个数相同,与原串尽可能不同。
思路
将一个最大数和一个最小数变为2个中间数,将两个中间数变为一个最大数和一个最小数取最优
#include<bits/stdc++.h>
#define maxn 100010
#define ll long long
using namespace std;
bool cmp(int a,int b)
{
return a>b;
}
int main()
{
int n;
int a[maxn],b[maxn],c[maxn];
while(scanf("%d",&n)!=EOF)
{
ll sum=0;
int Max=-maxn,Min=maxn;
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
Max=max(Max,a[i]);
Min=min(Min,a[i]);
sum+=a[i];
}
if(Max-Min==0||Max-Min==1)
{
printf("%d\n",n);
sort(a,a+n,cmp);
for(int i=0;i<n-1;i++)
printf("%d ",a[i]);
printf("%d\n",a[n-1]);
}
else
{
int Mid=Max-1;
sort(a,a+n,cmp);
for(int i=0;i<n;i++)
b[i]=a[i];
for(int i=0;i<n/2;i++)
{
if(b[i]==Max&&b[n-1-i]==Min)
{
b[i]=Mid;
b[n-1-i]=Mid;
}
}
sort(b,b+n,cmp);
int ans1=0;
for(int i=0;i<n;i++)
{
if(a[i]==b[i])
ans1++;
}
for(int i=0;i<n;i++)
c[i]=a[i];
for(int i=0;i<n;i++)
{
if(c[i]==Mid&&c[i+1]==Mid)
{
c[i]=Max;
c[i+1]=Min;
}
}
sort(c,c+n,cmp);
int ans2=0;
for(int i=0;i<n;i++)
{
if(a[i]==c[i])
ans2++;
}
if(ans1<ans2)
{
printf("%d\n",ans1);
for(int i=0;i<n-1;i++)
printf("%d ",b[i]);
printf("%d\n",b[n-1]);
}
else
{
printf("%d\n",ans2);
for(int i=0;i<n-1;i++)
printf("%d ",c[i]);
printf("%d\n",c[n-1]);
}
}
}
return 0;
}
相关文章推荐
- Codeforces 931 C. Laboratory Work
- CodeForce 363C Fixing Typos
- CodeForce#190 Div1
- codeforce 113div2 20130726CF训练
- codeforce C. Success Rate
- codeforce Gym 100570B ShortestPath Query (最短路SPFA)
- codeforce round 399# E
- codeforce 371C
- Codeforce Round #438 C.Bus(贪心)
- Codeforce 332C
- codeforce C - Vasya and Robot
- CodeForce 236B Easy Number Challenge(筛法求素数 + 整数因式分解)
- codeforce 617E XOR and Favorite Number 莫队算法
- Codeforce 215 div1
- Codeforce Round #222 Div2
- code force A. Bear and Five Cards&B. Bear and Finding Criminals
- Codeforce 373 D Counting Rectangles is Fun (统计全0子矩阵)
- Codeforce 149D(区间dp+括号匹配)
- CodeForce 825B Five-In-a-Row
- 【CodeForce #239 Div2】408A——Line to Cashier