CodeForces - 808C C. Tea Party (贪心)
2017-09-09 20:09
281 查看
C. Tea Party
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Polycarp invited all his friends to the tea party to celebrate the holiday. He has n cups, one for each of his n friends,
with volumes a1, a2, ..., an.
His teapot stores w milliliters of tea (w ≤ a1 + a2 + ... + an).
Polycarp wants to pour tea in cups in such a way that:
Every cup will contain tea for at least half of its volume
Every cup will contain integer number of milliliters of tea
All the tea from the teapot will be poured into cups
All friends will be satisfied.
Friend with cup i won't be satisfied, if there exists such cup j that
cup i contains less tea than cup j but ai > aj.
For each cup output how many milliliters of tea should be poured in it. If it's impossible to pour all the tea and satisfy all conditions then output -1.
Input
The first line contains two integer numbers n and w (1 ≤ n ≤ 100,
).
The second line contains n numbers a1, a2, ..., an (1 ≤ ai ≤ 100).
Output
Output how many milliliters of tea every cup should contain. If there are multiple answers, print any of them.
If it's impossible to pour all the tea and satisfy all conditions then output -1.
Examples
input
output
input
output
input
output
Note
In the third example you should pour to the first cup at least 5 milliliters, to the second one at least 4, to the third one at least 5. It sums up to 14, which is greater than 10 milliliters available.
题意:有n个杯子,有w毫升茶,之后有每个杯子的容积,要求每个杯子里的茶至少有一半,且容积小的杯子里的茶不能超过容积大的杯子里的茶。
思路:先每个杯子倒一半,剩余的茶按容积从大到小倒。
#include<stdio.h>
#include<algorithm>
using namespace std;
struct cup
{
int t;
int id;
int ans;
}c[110];
bool cmp(cup a,cup b)
{
return a.t>b.t;
}
bool cmp1(cup a,cup b)
{
return a.id<b.id;
}
int main()
{
int n,w,a[110];
while(scanf("%d%d",&n,&w)!=EOF)
{
int t=0;
for(int i=0;i<n;i++)
{
scanf("%d",&c[i].t);
c[i].id=i;
c[i].ans=c[i].t%2?c[i].t/2+1:c[i].t/2;
t+=c[i].ans;
}
if(t>w)
{
printf("-1\n");
continue;
}
w=w-t;
sort(c,c+n,cmp);
for(int i=0;i<n;i++)
{
int p=c[i].t-c[i].ans;
if(w>=p)
{
c[i].ans+=p;
w-=p;
}
else
{
c[i].ans+=w;
w-=w;
}
if(w==0)
break;
}
if(w!=0)
printf("-1\n");
else
{
sort(c,c+n,cmp1);
for(int i=0;i<n;i++)
{
if(i==n-1)
printf("%d\n",c[i].ans);
else
printf("%d ",c[i].ans);
}
}
}
}
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Polycarp invited all his friends to the tea party to celebrate the holiday. He has n cups, one for each of his n friends,
with volumes a1, a2, ..., an.
His teapot stores w milliliters of tea (w ≤ a1 + a2 + ... + an).
Polycarp wants to pour tea in cups in such a way that:
Every cup will contain tea for at least half of its volume
Every cup will contain integer number of milliliters of tea
All the tea from the teapot will be poured into cups
All friends will be satisfied.
Friend with cup i won't be satisfied, if there exists such cup j that
cup i contains less tea than cup j but ai > aj.
For each cup output how many milliliters of tea should be poured in it. If it's impossible to pour all the tea and satisfy all conditions then output -1.
Input
The first line contains two integer numbers n and w (1 ≤ n ≤ 100,
).
The second line contains n numbers a1, a2, ..., an (1 ≤ ai ≤ 100).
Output
Output how many milliliters of tea every cup should contain. If there are multiple answers, print any of them.
If it's impossible to pour all the tea and satisfy all conditions then output -1.
Examples
input
2 10 8 7
output
6 4
input
4 4 1 1 1 1
output
1 1 1 1
input
3 10 9 8 10
output
-1
Note
In the third example you should pour to the first cup at least 5 milliliters, to the second one at least 4, to the third one at least 5. It sums up to 14, which is greater than 10 milliliters available.
题意:有n个杯子,有w毫升茶,之后有每个杯子的容积,要求每个杯子里的茶至少有一半,且容积小的杯子里的茶不能超过容积大的杯子里的茶。
思路:先每个杯子倒一半,剩余的茶按容积从大到小倒。
#include<stdio.h>
#include<algorithm>
using namespace std;
struct cup
{
int t;
int id;
int ans;
}c[110];
bool cmp(cup a,cup b)
{
return a.t>b.t;
}
bool cmp1(cup a,cup b)
{
return a.id<b.id;
}
int main()
{
int n,w,a[110];
while(scanf("%d%d",&n,&w)!=EOF)
{
int t=0;
for(int i=0;i<n;i++)
{
scanf("%d",&c[i].t);
c[i].id=i;
c[i].ans=c[i].t%2?c[i].t/2+1:c[i].t/2;
t+=c[i].ans;
}
if(t>w)
{
printf("-1\n");
continue;
}
w=w-t;
sort(c,c+n,cmp);
for(int i=0;i<n;i++)
{
int p=c[i].t-c[i].ans;
if(w>=p)
{
c[i].ans+=p;
w-=p;
}
else
{
c[i].ans+=w;
w-=w;
}
if(w==0)
break;
}
if(w!=0)
printf("-1\n");
else
{
sort(c,c+n,cmp1);
for(int i=0;i<n;i++)
{
if(i==n-1)
printf("%d\n",c[i].ans);
else
printf("%d ",c[i].ans);
}
}
}
}
相关文章推荐
- 【Codeforces 808C】【贪心】Tea Party题解
- codeforces 808C——Tea Party(贪心)
- 【codeforces 808】C.Tea Party(贪心)
- Codeforces 389A Fox and Number Game(贪心)
- CodeForces 416C Booking System(贪心)
- Codeforces 355C Vasya and Robot【贪心+前缀和】
- codeforces 232A Cycles (构建图,贪心+模拟)
- Codeforces 734B Anton and Digits【贪心】水题
- Codeforces 825D 二分贪心
- Codeforces 373C:Counting Kangaroos is Fun(二分+贪心)
- codeforces 732B The Best Friend Of a Man 贪心
- CodeForces - 459E Pashmak and Graph[贪心优化dp]
- CodeForces 702 A Maximum Increase (贪心,高效算法)
- CodeForces 605 A.Sorting Railway Cars(贪心)
- Codeforces 486C Palindrome Transformation(贪心)
- Codeforces 651B Beautiful Paintings【贪心】
- CodeForces 500C New Year Book Reading(贪心+思维)
- codeforces 231 C To Add or Not to Add 贪心
- CodeForces 707B Bakery(思维+贪心)
- codeforces 798 D. Mike and distribution 二维贪心