Educational Codeforces Round 21(A-D)(水题 + 尺取 + 构造 + multiset优化)

这是昨天的训练赛,在场上A出四题,场下看能不能再补出一题。

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.

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
Copy
4
output
1

A题:在年份中有且只有一个非零字符的是lucky year。给定今年,问最近的lucky year在几年后。

一开始使用了pow()函数结果WA了很多发,应该是因为pow的返回值类型是double的缘故,后来手写了一个幂函数就过了

namespace Solver {
    int pp(int x)
    {
        int ans =1;
        for(int i=1; i<=x; i++)
            ans*=10;
        return ans;
    }

    void solve(){
        int n;
        cin>>n;
        int p=1;

        if(n<10) printf("1\n");
        else {
            while(n/(pp(p))>=10) p++;
            int maxx = (int)(n/(pp(p))+1)*pp(p);
            cout<<maxx - n<<endl;
        }
    }
};
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 = 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
Copy
3 2
3 4 7
output
9.0000000000

B题:求给定n个数中,所有连续k长度中所有数的和,求平均值。

用尺取法在O(n)的复杂度内求出所有连续k长度的和。

void solve(){
        int n, k;
        scanf("%d%d", &n, &k);
        for(int i=0; i<n; i++)
            scanf("%d", &a[i]);
        long long sum = 0, num = 0;
        int l=0, r=k-1;
        for(int i=0; i<k; i++)
            sum += a[i];
        //printf("sum: %d\n", sum);
        num += sum;
        while(r!=n-1)
        {
            sum = sum + a[++r] - a[l++];
            //printf("sum: %d\n", sum);
            num += sum;
        }

        double ans = (double)(num)/(double)(n-k+1);
        printf("%.8f", ans);
    }
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.

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
Copy
2 10
8 7
output
6 4 

C题:给n个杯子m单位的水。要求每个杯子中的水是整数,且至少是该杯子容量的一半,容量大的杯子里的水不能比容量小的杯子里的水少,m单位的水要倒完。给出任意一种倒法。

因为题目要求给出任意一种解法,那么直接构造就可以了。首先保证每个杯子里的水都是容量的一半,然后从容量大的杯子开始倒水,把m单位的水倒完。

struct Node{
    int id;
    int w;
    int h;
}a[105];
bool cmp1(Node a, Node b){return a.w>b.w;}
bool cmp2(Node a, Node b){return a.id<b.id;}
namespace Solver {
    void solve(){
        int n, m, sum=0;
        scanf("%d%d", &n, &m);
        for(int i=0; i<n; i++){
            scanf("%d", &a[i].w);
            a[i].id = i;
            a[i].h = (a[i].w+1)/2;
            sum += a[i].h;
        }
        if(sum > m) printf("-1\n");
        else {
            sort(a, a+n, cmp1);
            int y = m - sum;
            int p=0;
            while(y>0){
                //printf("y:%d %d\n", p, y);
                if(y > a[p].w - a[p].h){
                    y -= a[p].w - a[p].h;
                    a[p].h = a[p].w;
                    p++;
                }
                else {
                    a[p].h += y;
                    y=0;
                    p++;
                }
            }
            sort(a, a+n, cmp2);
            for(int i=0; i<n; i++){
                printf("%d%c", a[i].h, i==n-1?'\n':' ');
            }
        }
    }
};
D. Array Division

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
Copy
3
1 3 2
output
YES

D题:定义一种操作,可以将数列中任意位置的数删去后插入到数列任意位置。问能否通过一次操作使得新的数列中存在前一部分和等于后一部分和的情况。

因为只挪动了一个数,所以可以看成在选取了一部分前缀和后,从后一部分找出一个数挪到前面,或者是从前一部分找出一个数挪到后面。用set来查找。

multiset<LL > s, d;
LL a[maxn];


namespace Solver {

    void solve(){
        int n;
        LL sum=0, num=0;
        s.clear();
        d.clear();
        scanf("%d", &n);
        for(int i=0; i<n; i++){
            scanf("%I64d", &a[i]);
            s.insert(a[i]);
            sum += a[i];
        }
        //printf("sum: %I64d\n", sum);
        if(sum&1) puts("NO");
        else {
            bool can = false;
            sum /= 2;
            for(int i=0; i<n; i++)
            {
                num += a[i];
                multiset <LL>::iterator pos = s.find(a[i]);
                s.erase(pos);
                d.insert(a[i]);
                //printf("%d: %I64d %d\n", i, num, s.count(1));
                if(num == sum){
                    can = true;
                    break;
                }
                else if(num < sum){
                    if(s.count(sum - num)){
                        can = true;
                        break;
                    }
                }
                else if(num > sum){
                    if(d.count(num - sum)){
                        can = true;
                        break;
                    }
                }
            }

            if(can) puts("YES");
            else puts("NO");
        }
    }
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值