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 outp...

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 }

 

           
       

           

              
       

           

目录
相关文章
|
6月前
【洛谷 P1219】[USACO1.5]八皇后 Checker Challenge 题解(深度优先搜索+回溯法)
**USACO1.5八皇后挑战**是关于在$n\times n$棋盘上放置棋子的,确保每行、每列及两条主对角线上各有一个且仅有一个棋子。给定$6$作为输入,输出前$3$个解及解的总数。例如,对于$6\times6$棋盘,正确输出应包括解的序列和总数。代码使用DFS解决,通过跟踪对角线占用状态避免冲突。当找到所有解时,显示前三个并计数。样例输入$6$产生输出为解的前三个排列和总数$4$。
41 0
|
机器学习/深度学习 C++
【PAT甲级 - C++题解】1059 Prime Factors
【PAT甲级 - C++题解】1059 Prime Factors
83 0
51nod 1189 阶乘分数(数论)
51nod 1189 阶乘分数(数论)
60 0
|
机器学习/深度学习 人工智能 算法
CF1446D Frequency Problem(思维 前缀和 根号分治 双指针)
CF1446D Frequency Problem(思维 前缀和 根号分治 双指针)
95 0
POJ 2689 Prime Distance (埃氏筛 区间筛)
POJ 2689 Prime Distance (埃氏筛 区间筛)
114 0
|
C语言
HDOJ/HDU Tempter of the Bone(深搜+奇偶性剪枝)
HDOJ/HDU Tempter of the Bone(深搜+奇偶性剪枝)
99 0
|
人工智能 BI 机器学习/深度学习