Educational Codeforces Round 21(A.暴力,B.前缀和,C.贪心)
A. Lucky Year
time limit per test:1 second
memory limit per test:256 megabytes
input:standard input
output:standard output
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.
Input
The first line contains integer number n (1 ≤ n ≤ 109) — current year in Berland.
Output
Output amount of years from the current year to the next lucky one.
Examples
Input
4
Output
1
Input
201
Output
99
Input
4000
Output
1000
Note
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
time limit per test:1 second
memory limit per test:256 megabytes
input:standard input
output:standard output
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.
Input
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
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.
Examples
Input
3 2
3 4 7
Output
9.0000000000
Input
1 1
10
Output
10.0000000000
Input
8 2
1 2 4 100000 123 456 789 1
Output
28964.2857142857
Note
In the third example there are n - k + 1 = 7 weeks, so the answer is sums of all weeks divided by 7.
题目链接:http://codeforces.com/contest/808/problem/B
分析:计算上升和下降的序列数之和,再除以n-k+1即可,相当于求其中每k个数的和的平均值!
下面给出AC代码:
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
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.
题目链接: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 }
- CentOS 7 安装Maven
- CentOS 7 安装Java 1.8
- Ocelot 集成Butterfly 实现分布式跟踪
- 如何从零开始学一门程序语言?
- 玩得一手好注入之order by排序篇
- [Cake] 1. CI中的Cake
- Docker镜像导致centos-root根分区容量爆满
- 分布式文件系统FastDFS动态扩容
- 高可用高性能分布式文件系统FastDFS实践Java程序
- MobileNet教程:用TensorFlow搭建在手机上运行的图像分类器
- Vagrant share浅析
- 空间金字塔池化(Spatial Pyramid Pooling, SPP)原理和代码实现(Pytorch)
- 批量下载Coursera及其他场景上的文件
- 深度学习动手入门:GitHub上四个超棒的TensorFlow开源项目
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法