Educational Codeforces Round 21(A.暴力,B.前缀和,C.贪心)
A. Lucky Year
Apart from having lots of holidays throughout the year, residents of Berland also have whole lucky years. Year is considered lucky if it has no more than 1 non-zero digit in its number. So years 100, 40000, 5 are lucky and 12, 3001 and 12345 are not.
You are given current year in Berland. Your task is to find how long will residents of Berland wait till the next lucky year.
The first line contains integer number n (1 ≤ n ≤ 109) — current year in Berland.
Output amount of years from the current year to the next lucky one.
4
1
201
99
4000
1000
In the first example next lucky year is 5. In the second one — 300. In the third — 5000.
题目链接:http://codeforces.com/contest/808/problem/A
分析:题目意思是要将下一个数字变为只有1个非0的数,打表即可,要注意开ll,否则会WA!
下面给出AC代码:
1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 int main() 5 { 6 ll n; 7 while(scanf("%lld",&n)!=EOF) 8 { 9 if(n>=0&&n<=9) 10 cout<<1<<endl; 11 else if(n>=10&&n<=99) 12 cout<<(n/10+1)*10-n<<endl; 13 else if(n>=100&&n<=999) 14 cout<<(n/100+1)*100-n<<endl; 15 else if(n>=1000&&n<=9999) 16 cout<<(n/1000+1)*1000-n<<endl; 17 else if(n>=10000&&n<=99999) 18 cout<<(n/10000+1)*10000-n<<endl; 19 else if(n>=100000&&n<=999999) 20 cout<<(n/100000+1)*100000-n<<endl; 21 else if(n>=1000000&&n<=9999999) 22 cout<<(n/1000000+1)*1000000-n<<endl; 23 else if(n>=10000000&&n<=99999999) 24 cout<<(n/10000000+1)*10000000-n<<endl; 25 else if(n>=100000000&&n<=999999999) 26 cout<<(n/100000000+1)*100000000-n<<endl; 27 else if(n>=1000000000&&n<=9999999999) 28 cout<<(n/1000000000+1)*1000000000-n<<endl; 29 } 30 return 0; 31 }
B. Average Sleep Time
It's been almost a week since Polycarp couldn't get rid of insomnia. And as you may already know, one week in Berland lasts k days!
When Polycarp went to a doctor with his problem, the doctor asked him about his sleeping schedule (more specifically, the average amount of hours of sleep per week). Luckily, Polycarp kept records of sleep times for the last n days. So now he has a sequence a1, a2, ..., an, where ai is the sleep time on the i-th day.
The number of records is so large that Polycarp is unable to calculate the average value by himself. Thus he is asking you to help him with the calculations. To get the average Polycarp is going to consider k consecutive days as a week. So there will be n - k + 1 weeks to take into consideration. For example, if k = 2, n = 3 and a = [3, 4, 7], then the result is .
You should write a program which will calculate average sleep times of Polycarp over all weeks.
The first line contains two integer numbers n and k (1 ≤ k ≤ n ≤ 2·105).
The second line contains n integer numbers a1, a2, ..., an (1 ≤ ai ≤ 105).
Output average sleeping time over all weeks.
The answer is considered to be correct if its absolute or relative error does not exceed 10 - 6. In particular, it is enough to output real number with at least 6 digits after the decimal point.
3 2
3 4 7
9.0000000000
1 1
10
10.0000000000
8 2
1 2 4 100000 123 456 789 1
28964.2857142857
In the third example there are n - k + 1 = 7 weeks, so the answer is sums of all weeks divided by 7.
1 #include <bits/stdc++.h> 2 typedef long long ll; 3 using namespace std; 4 ll n,k; 5 double avg; 6 ll ans; 7 ll s[200005]; 8 int main() 9 { 10 cin>>n>>k; 11 for(int i=1;i<=n;i++) 12 scanf("%I64d",s+i); 13 ll c=0; 14 for(int i=1;i<=n;i++) 15 { 16 if(i<=n-k+1) 17 { 18 if(c<k) 19 c++; 20 ans+=c*s[i]; 21 } 22 else 23 { 24 if(c>n-i+1) 25 c--; 26 ans+=c*s[i]; 27 } 28 } 29 avg=ans*1.0/(n-k+1); 30 printf("%0.12lf",avg); 31 return 0; 32 }
C. Tea Party
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.
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 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.
2 10
8 7
6 4
4 4
1 1 1 1
1 1 1 1
3 10
9 8 10
-1
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.
题目链接:http://codeforces.com/contest/808/problem/C
题意:n个茶杯,每个茶杯有容量。现在给一壶茶,总量为w。希望倒茶满足条件:每杯茶要超过容量的一半,并且w被倒光,茶杯内的茶水为整数,容量大的杯子内的茶不允许比容量小的杯子内的茶水少,特判不满足情况,然后将茶水给每一杯倒至一半以上。然后按照容量从大到小每次倒一个单位,直到倒完为止。
下面给出AC代码:
1 #include <bits/stdc++.h> 2 using namespace std; 3 class Tea 4 { 5 public: 6 int a,w,id; 7 }; 8 Tea t[105]; 9 int n,w; 10 int cmp1(Tea a,Tea b) 11 { 12 return a.a>b.a; 13 } 14 int cmp2(Tea a,Tea b) 15 { 16 return a.id<b.id; 17 } 18 int main() 19 { 20 cin>>n>>w; 21 for(int i=0;i<n;i++) 22 { 23 cin>>t[i].a; 24 t[i].id=i; 25 } 26 sort(t,t+n,cmp1); //杯子大到小排序 27 for(int i=0;i<n;i++) 28 { 29 w-=(t[i].a+1)/2; //每个杯子至少装一半 30 t[i].w=(t[i].a+1)/2; //保存已经装了多少 31 } 32 for(int i=0;i<n;i++) 33 { 34 int x=t[i].a-t[i].w; 35 if(w>=x) 36 { 37 t[i].w+=x; 38 w-=x; 39 } 40 else if(w>0) 41 { 42 t[i].w+=w; 43 w=0; 44 } 45 else 46 break; 47 } 48 sort(t,t+n,cmp2); //按照id 排序 排回原位 49 if(w<0) 50 cout<<-1; 51 else 52 for(int i=0;i<n;i++) 53 cout<<t[i].w<<" "; 54 return 0; 55 }
作 者:Angel_Kitty
出 处:https://www.cnblogs.com/ECJTUACM-873284962/
关于作者:阿里云ACE,目前主要研究方向是Web安全漏洞以及反序列化。如有问题或建议,请多多赐教!
版权声明:本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接。
特此声明:所有评论和私信都会在第一时间回复。也欢迎园子的大大们指正错误,共同进步。或者直接私信我
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角【推荐】一下。您的鼓励是作者坚持原创和持续写作的最大动力!
欢迎大家关注我的微信公众号IT老实人(IThonest),如果您觉得文章对您有很大的帮助,您可以考虑赏博主一杯咖啡以资鼓励,您的肯定将是我最大的动力。thx.
我的公众号是IT老实人(IThonest),一个有故事的公众号,欢迎大家来这里讨论,共同进步,不断学习才能不断进步。扫下面的二维码或者收藏下面的二维码关注吧(长按下面的二维码图片、并选择识别图中的二维码),个人QQ和微信的二维码也已给出,扫描下面👇的二维码一起来讨论吧!!!
欢迎大家关注我的Github,一些文章的备份和平常做的一些项目会存放在这里。