【codeforces 808】C.Tea Party(贪心)
来源:互联网 发布:java源代码流程图 编辑:程序博客网 时间:2024/06/15 00:06
题目链接:http://codeforces.com/contest/808/problem/C
解题思路:应该算贪心的思想吧,首先给每个杯子倒茶(a[i]+1)/2毫升,如果茶壶水不够,输出-1.
否则,把当前茶壶剩下水,倒给其他水杯中,从容量最大的开始,直接倒满,如果还有剩余,就给第二大的水杯倒满,依次类推,直到茶壶为空。
#include <iostream>#include <cstdio>using namespace std;int a[102];int b[102];int f[102];//标记当前水杯是否已经处理过,倒满水了。int n,w;int findNext(){ //求没有被遍历过的,当前剩下酒杯之中最大的那个 int maxx=-1,t=0; for(int i=0;i<n;i++){ if(f[i]==0){ if(a[i]>maxx){ maxx=a[i]; t=i; } } } f[t]=1; return t;}int main(){ scanf("%d %d",&n,&w); int minn=0; for(int i=0;i<n;i++){ scanf("%d",&a[i]); minn+=(a[i]+1)/2; b[i]=(a[i]+1)/2; } //printf("%d \n",minn); if(w<minn){ printf("-1"); }else{ int left=w-minn; while(left>0){ int t=findNext(); if(left>a[t]/2){ left-=a[t]/2; b[t]=a[t]; }else{ b[t]+=left; left=0; } } for(int i=0;i<n-1;i++){ printf("%d ",b[i]); } printf("%d\n",b[n-1]); } return 0;}
阅读全文
0 0
- 【codeforces 808】C.Tea Party(贪心)
- Codeforces 808C Tea Party【贪心】
- 【Codeforces 808C】【贪心】Tea Party题解
- codeforces 808C——Tea Party(贪心)
- CF#808 C. Tea Party(贪心)
- Educational Codeforces Round 21 808C Tea Party 【贪心算法】
- Codeforces 808C Tea Party
- Codeforces 808C Tea Party
- Codeforces 808C Tea Party 题解
- 【Codeforces 808 C. Tea Party】+ sort
- Educational Codeforces Round 21 C. Tea Party 贪心
- Educational Codeforces Round 21 C. Tea Party
- Educational Codeforces Round 21 C. Tea Party
- Educational Codeforces Round 21 C. Tea Party
- Educational Codeforces Round 21-----C. Tea Party
- Educational Codeforces Round 21 C Tea Party
- codeforces Educational Codeforces Round 21 C. Tea Party
- Crazy tea party(UVALive 2756)
- JQ 获取控件值或设置值
- build static lib
- Stars
- POJ 1170 Shopping Offers 笔记
- js闭包
- 【codeforces 808】C.Tea Party(贪心)
- MyBatis中的参数传递
- 解锁Oracle system/scott/sys用户
- Shell逐行读取文件的4种方法
- LeetCode:Same Tree
- Eclipse Jetty插件安装
- Sass入门教程
- c++作业五
- LInux学习笔记 --https