您的位置:首页 > 其它

C - Valera and Contest题解

2015-08-06 21:26 459 查看
Description

Valera loves to participate in competitions. Especially in programming contests. Today he has participated in the contest with his team, consisting of n students (including Valera). This contest
was an individual competition, so each student in the team solved problems individually.

After the contest was over, Valera was interested in results. He found out that:

each student in the team scored at least l points and at most r points;
in total, all members of the team scored exactly sall points;
the total score of the k members of the team who scored the most points is equal to exactly sk;
more formally, if a1, a2, ..., an is the sequence of points earned by the
team of students in the non-increasing order (a1 ≥ a2 ≥ ... ≥ an), then sk = a1 + a2 + ... + ak.

However, Valera did not find out exactly how many points each of n students scored. Valera asked you to recover any distribution of scores between the students of the team, such that all the
conditions above are met.

Input

The first line of the input contains exactly six integers n, k, l, r, sall, sk (1 ≤ n, k, l, r ≤ 1000; l ≤ r; k ≤ n; 1 ≤ sk ≤ sall ≤ 106).

It's guaranteed that the input is such that the answer exists.

Output

Print exactly n integers a1, a2, ..., an —
the number of points each student scored. If there are multiple solutions, you can print any of them. You can print the distribution of points in any order.

Sample Input

Input
5 3 1 3 13 9


Output
2 3 2 3 3


Input
5 3 1 3 15 9


Output
3 3 3 3 3


题目如上

AC代码

#include<iostream>
using namespace std;
int a[1010],b[1010];

int main()
{
int n,k,l,r,s1,s2;
int i,j;
cin>>n>>k>>l>>r>>s1>>s2;

int s3;
s3=s1-s2;	//s3为n-k个数的和,比赛后n-k名的和,即s3中最大的数应小于s2中最小的数

j=k;
for(i=1;i<=k;i++)
{
a[i]=s2/j;
j--;
s2-=a[i];
}

j=n-k;
for(i=k+1;i<=n;i++)
{
a[i]=s3/j;
j--;
s3-=a[i];
}

for(i=1;i<=n;i++)
cout<<a[i]<<" ";
cout<<endl;
return 0;
}


解题思路

题意可总结为:将这n个人的成绩按从大到小的顺序排列后前k个人的总分为s1,后n-k个人的总分为s2.且每个人的分数应在l和r之间。因此此题重点是求出和为s1的k个数,和和为s2的n-k个数。我首先求的是k个数,方法是a0=s1/k;a0应小于等于这k个数的平均数,剩余部分s1-=a0*k;此时的s1为k-1个数的和,求这k-1个数的平均数a1,a1满足a1>=a0,因为(s1-a)/(k-1)>=a0*(k-1)/(k-1);即s1>=a0*k;从前面可知这是必然的。所以a1大于等于a0,同理可知,后面依照此法所求出的a2,a3,....ak也满足ai>=a(i-1).即可求得一个递增的序列。

同理求出后n-k个数。

还需要证明两点。

第一,证名求出的每个数ai都在l和r之间。因为每个人的分数都在l和r之间,所以前k名的分数必然也在。、

第二,证明前k个人的分数大于等于后n-k个人的分数。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: