Educational Codeforces Round 21

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.

题意:给出一个数,询问下一个除首位外其他位数都为0的数与给出数字的差值。

题解:将给出数字首位加一,其他数位都为0,做差然后输出就可以了,(例如:201,就变成300,然后减去201,得99)注意,过程中可能爆int,所以用longlong。

代码:

//Wud
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <stack>
#include <cstring>
#include <vector>
#include <bitset>
#include <string>
#include <cmath>
#include <set>
#include <map>
#include <queue>
using namespace std;
typedef long long ll;
const int maxn = 3e5+7;
const int INF = 0x3f3f3f3f;
int m;
int main()
{
    while (scanf("%d",&m)!=EOF){
        int t;
        int cnt = 0;
        int n  = m;
        while (n){
            t = n%10;
            n/=10;
            cnt++;
        }
        ll ans = t+1;
        for(int i = 0;i < cnt-1;i++){
            ans*=10;
        }
        ans = ans-m;
        cout<<ans<<endl;
    }
    return 0;
}
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 sequencea1, 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 = 2n = 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.

题意:给出n,和k,然后是n个数字。然后每k个连续的数字算一段,求每一段数字和除以(n-k+1)的值之和(即求所有段的平均值)。

题解:先计算最前面的一段,然后不断向前移动,加上下一个数字,减去上一个数字,得到ans,输出即可。(不可以先预处理出每个数出现次数,然后将所有数字加起来除以(n-k+1),这样会有误差)。

代码:

//Wud
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <stack>
#include <cstring>
#include <vector>
#include <bitset>
#include <string>
#include <cmath>
#include <set>
#include <map>
#include <queue>
using namespace std;
typedef long long ll;
const int maxn = 3e5+7;
const int INF = 0x3f3f3f3f;
int n,k;
int s[maxn];
int main()
{
    while (scanf("%d %d",&n,&k)!=EOF){
        for(int i = 1;i <= n;i++){
            scanf("%d",&s[i]);
        }
        double ans = 0;
        ll sum = 0;
        for(int i = 1;i <= k;i++){
            sum+=s[i];
        }
        ans+=sum*1.0/(n-k+1);
        for(int i = k+1;i <= n;i++){
            sum+=s[i]-s[i-k];
            ans+=sum*1.0/(n-k+1);
        }
        printf("%.10f\n",ans);
    }
    return 0;
}
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 volumesa1, 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个杯子,m单位的茶水。现在每个杯子有不同的容量。要求茶水要全部倒完,且每个人获得的水必须大于等于杯子容量的1/2,还有就是容量大的杯子里的水必须多于容量小的杯子里的水。

题解:先判断是否茶水倒不完(所有杯子容量和小于m),以及不够倒(杯子容量的1/2的和大于m)。然后先让所有杯子的1/2倒满,接着对杯子排序,从高到低把杯子倒满直到茶水完了为止。

代码:

//Wud
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <stack>
#include <cstring>
#include <vector>
#include <bitset>
#include <string>
#include <cmath>
#include <set>
#include <map>
#include <queue>
using namespace std;
typedef long long ll;
const int maxn = 100+7;
const int INF = 0x3f3f3f3f;
int n,k;
struct Node{
    int v,id,ans;
} s[maxn];
bool cmp(Node a,Node b){
    return a.v < b.v;
}
bool cmp1(Node a,Node b){
    return a.id<b.id;
}
int main()
{
    while(scanf("%d %d",&n,&k)!=EOF){
        for(int i = 1;i <= n;i++){
            scanf("%d",&s[i].v);
            s[i].id = i;
        }
        int sum1 = 0,sum2 = 0;
        for(int i = 1;i <= n;i++){
            sum1+=ceil(s[i].v*1.0/2);
            sum2+=s[i].v;
            s[i].ans = ceil(s[i].v*1.0/2);
        }
        if(k>sum2||k < sum1){
            cout<<-1<<endl;
            continue;
        }
        sort(s+1,s+n+1,cmp);
        k -= sum1;
        for(int i = n;i >= 1;i--){
            if(k-s[i].v+s[i].ans>0){
                k-=s[i].v-s[i].ans;
                s[i].ans = s[i].v;
            }
            else {
                s[i].ans = s[i].ans+k;
                break;
            }
        }
        sort(s+1,s+n+1,cmp1);
        for(int i = 1;i <= n;i++)
        {
            cout<<s[i].ans<<" ";
        }
        cout<<endl;
    }
    return 0;

D. Array Division
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Vasya has an array a consisting of positive integer numbers. Vasya wants to divide this array into two non-empty consecutive parts (the prefix and the suffix) so that the sum of all elements in the first part equals to the sum of elements in the second part. It is not always possible, so Vasya will move some element before dividing the array (Vasya will erase some element and insert it into an arbitrary position).

Inserting an element in the same position he was erased from is also considered moving.

Can Vasya divide the array after choosing the right element to move and its new position?

Input

The first line contains single integer n (1 ≤ n ≤ 100000) — the size of the array.

The second line contains n integers a1, a2... an (1 ≤ ai ≤ 109) — the elements of the array.

Output

Print YES if Vasya can divide the array after moving one element. Otherwise print NO.

Examples
input
3
1 3 2
output
YES
input
5
1 2 3 4 5
output
NO
input
5
2 2 3 4 5
output
YES
Note

In the first example Vasya can move the second element to the end of the array.

In the second example no move can make the division possible.

In the third example Vasya can move the fourth element by one position to the left.


题意:给出一数列,要求移动一个数字,使得数列可以分成两段,前一段的数字和等于后一段的数字和。

题解:第一步,先把特判的解决掉,如果原本数列的和就是奇数,那么输出“NO”。第二步,先处理出前缀和,然后枚举每个前段。如果该段的和a大于后段的和b,就可以得到两段差c=a-b,然后在前半段中查找是否存在c/2,如果存在就输出“YES”;如果该段的和a小于后段的b,就在后段中查找c/2,然后同理。如果遍历完所有前缀和都不存在的话,就输出“NO”。而在前段中找c/2的方法是先将整个数列copy一份,然后sort,在新的那一分上二分查找c/2,找到后判断其原来的下标是否大于或者小于现在遍历的i。

代码:

//Wud
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <stack>
#include <cstring>
#include <vector>
#include <bitset>
#include <string>
#include <cmath>
#include <set>
#include <map>
#include <queue>
using namespace std;
typedef long long ll;
const int maxn = 1e5+7;
const int INF = 0x3f3f3f3f;
int n;
struct node{
    ll v,id;
}s[maxn];
ll u[maxn];
bool cmp(node a,node b){
    return a.v<b.v;
}
int lower(int l,int r,ll v){
    while(l + 1 < r){
        int mid = l+r>>1;
        if(s[mid].v>=v){
            r = mid;
        }
        else {
            l = mid;
        }
    }
    if(s[l].v==v) return l;
    else if(s[r].v==v) return r;
    else return -1;
}
int main()
{
    while(scanf("%d",&n)!=EOF){
        u[0] = 0;
        ll sum = 0;
        for(int i = 1;i <= n;i++){
            scanf("%lld",&u[i]);
            s[i].v = u[i];
            sum+=u[i];
            s[i].id = i;
            u[i]+=u[i-1];
        }
        if(sum%2!=0){
            cout<<"NO"<<endl;
            continue;
        }
        sort(s+1,s+1+n,cmp);
        bool flag = 0;
        for(int i = 1;i <= n;i++){
            if(u[i]>sum-u[i]){
                ll t = (u[i]-sum+u[i])/2;
                int pos = lower(1,n,t);
                //cout<<pos<<endl;
                if(pos==-1) continue;
                for(int j = pos;j <= n&&s[j].v==t;j++){
                    if(s[j].id<=i) {flag = 1;break;}
                }
            }
            else if(u[i]<sum-u[i]){
                ll t = (sum-u[i]-u[i])/2;
                int pos = lower(1,n,t);
                //cout<<pos<<endl;
                if(pos==-1) continue;
                for(int j = pos;j <= n&&s[j].v==t;j++){
                    if(s[j].id>i) {flag = 1;break;}
                }
            }
            else {flag = 1;break;}
            if(flag) break;
        }
        if(flag) cout<<"YES"<<endl;
        else cout<<"NO"<<endl;
    }
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值