Codeforces Round #468 (Div. 2,)(模拟 + 二进制位运算 + 贪心模拟 + dfs树)

数学建模这门课没选上,sad。所以在胖胖他们上课的时候有空开了一场,在开之前大概看了一眼过的人数,当时就想好了这场要过四个题。flag果然是立早了呢。

A. Friends Meeting

Two friends are on the coordinate axis Ox in points with integer coordinates. One of them is in the point x1 = a, another one is in the point x2 = b.

Each of the friends can move by one along the line in any direction unlimited number of times. When a friend moves, the tiredness of a friend changes according to the following rules: the first move increases the tiredness by 1, the second move increases the tiredness by 2, the third — by 3 and so on. For example, if a friend moves first to the left, then to the right (returning to the same point), and then again to the left his tiredness becomes equal to 1 + 2 + 3 = 6.

The friends want to meet in a integer point. Determine the minimum total tiredness they should gain, if they meet in the same point.

Input

The first line contains a single integer a (1 ≤ a ≤ 1000) — the initial position of the first friend.

The second line contains a single integer b (1 ≤ b ≤ 1000) — the initial position of the second friend.

It is guaranteed that a ≠ b.

Output

Print the minimum possible total tiredness if the friends meet in the same point.

Examples
input
Copy
3
4
output
1

A题:x轴上有两点,每个点每次移动一格的cost从1,2,3往上累加。问最少相遇的cost。

水题,模拟往中间走就过了。其实推公式应该可以O(1)做出来的,第一题不想费时间动脑子了就没这么做。

namespace Solver {
    void solve(){
        int a, b;
        int p = 1, sum = 0;
        scanf("%d%d", &a, &b);
        if(a>b) swap(a, b);
        while(a!=b)
        {
            a++;
            sum+=p;
            if(a==b) break;
            b--;
            sum+=p;
            p++;
        }
        printf("%d\n", sum);
    }
};
B. World Cup

The last stage of Football World Cup is played using the play-off system.

There are n teams left in this stage, they are enumerated from 1 to n. Several rounds are held, in each round the remaining teams are sorted in the order of their ids, then the first in this order plays with the second, the third — with the fourth, the fifth — with the sixth, and so on. It is guaranteed that in each round there is even number of teams. The winner of each game advances to the next round, the loser is eliminated from the tournament, there are no draws. In the last round there is the only game with two remaining teams: the round is called the Final, the winner is called the champion, and the tournament is over.

Arkady wants his two favorite teams to play in the Final. Unfortunately, the team ids are already determined, and it may happen that it is impossible for teams to meet in the Final, because they are to meet in some earlier stage, if they are strong enough. Determine, in which round the teams with ids a and b can meet.

Input

The only line contains three integers na and b (2 ≤ n ≤ 2561 ≤ a, b ≤ n) — the total number of teams, and the ids of the teams that Arkady is interested in.

It is guaranteed that n is such that in each round an even number of team advance, and that a and b are not equal.

Output

In the only line print "Final!" (without quotes), if teams a and b can meet in the Final.

Otherwise, print a single integer — the number of the round in which teams a and b can meet. The round are enumerated from 1.

Examples
input
Copy
4 1 2
output
1

B题:n只队伍,相邻两组比赛后晋级,问a,b两只队伍什么时候能碰到。

一开始想的是二分查找区间,后来发现,从二进制高位往地位遍历,第一个不同的位数即是两组相遇的轮数。(从0开始编号,第一轮是所有高位相等,而第1位不同两组之间对战,以此类推)。

结果因为手残码出了不少bug,输出还少了个!号。

namespace Solver {
    void solve(){
        int a, b, sum;
        int p = -1, q;
        scanf("%d%d%d", &sum, &a, &b);
        sum--,a--,b--;
        while(sum > 0)
        {
            p++;
            sum>>=1;
        }
        //printf("**%d\n", p);
        for(int i=p; i>=0; i--)
        {
            if(((a>>i)&1) ^ ((b>>i)&1)){
                q = i;
                break;
            }
        }

        if(p==q) printf("Final!\n");
        else printf("%d\n", q+1);
    }
};
C. Laboratory Work

Anya and Kirill are doing a physics laboratory work. In one of the tasks they have to measure some value n times, and then compute the average value to lower the error.

Kirill has already made his measurements, and has got the following integer values: x1x2, ..., xn. It is important that the values are close to each other, namely, the difference between the maximum value and the minimum value is at most 2.

Anya does not want to make the measurements, however, she can't just copy the values from Kirill's work, because the error of each measurement is a random value, and this coincidence will be noted by the teacher. Anya wants to write such integer values y1y2, ..., yn in her work, that the following conditions are met:

  • the average value of x1, x2, ..., xn is equal to the average value of y1, y2, ..., yn;
  • all Anya's measurements are in the same bounds as all Kirill's measurements, that is, the maximum value among Anya's values is not greater than the maximum value among Kirill's values, and the minimum value among Anya's values is not less than the minimum value among Kirill's values;
  • the number of equal measurements in Anya's work and Kirill's work is as small as possible among options with the previous conditions met. Formally, the teacher goes through all Anya's values one by one, if there is equal value in Kirill's work and it is not strike off yet, he strikes off this Anya's value and one of equal values in Kirill's work. The number of equal measurements is then the total number of strike off values in Anya's work.

Help Anya to write such a set of measurements that the conditions above are met.

Input

The first line contains a single integer n (1 ≤ n ≤ 100 000) — the numeber of measurements made by Kirill.

The second line contains a sequence of integers x1, x2, ..., xn ( - 100 000 ≤ xi ≤ 100 000) — the measurements made by Kirill. It is guaranteed that the difference between the maximum and minimum values among values x1, x2, ..., xn does not exceed 2.

Output

In the first line print the minimum possible number of equal measurements.

In the second line print n integers y1, y2, ..., yn — the values Anya should write. You can print the integers in arbitrary order. Keep in mind that the minimum value among Anya's values should be not less that the minimum among Kirill's values, and the maximum among Anya's values should be not greater than the maximum among Kirill's values.

If there are multiple answers, print any of them.

Examples
input
Copy
6
-1 1 1 0 0 -1
output
2
0 0 0 0 0 0 

C题:编数据。给出n个数,他们最大值和最小值之间最多差2,需要构造出来n个数,使这两个序列的平均值相同,且最大最小值相同,同时,一样的数尽可能少。

做这道题做的头大,题面不难可能自己手写的坑太多。首先发现,三个互相差1的数,为使两个序列的平均值相同,则要么x-1和x+1变成2个x。要么两个x变成一个x-1和x+1。那么只需要判断这两种变法那个能让相同位数最少就可以了。这道题这场结束后没花多久就搞出来了。结果场上有一个边界写错了,有一个情况少考虑了。最后改的也是乱七八糟的。留个坑看之后会不会补一个简单点的代码出来吧。

namespace Solver {
    void solve(){
        int n, b[5], cnt[5], tol = 0;
        memset(cnt, 0, sizeof cnt);
        scanf("%d", &n);
        for(int i=0; i<n; i++)
        {
            scanf("%d", &a[i]);
            s.insert(a[i]);
        }

        if(s.size()<3){
            for(set<int>::iterator it = s.begin(); it!=s.end(); it++)
                b[tol++] = *it;
            if(tol==2 && b[1]-b[0] == 2){
                for(int i=0; i<n; i++)
                    for(int j=0; j<2; j++)
                        if(a[i] == b[j]) cnt[j]++;
                printf("%d\n", max(cnt[0], cnt[1])-min(cnt[0],cnt[1]));
                for(int i=0; i<min(cnt[0], cnt[1]); i++)
                    printf("%d %d ", b[0]+1, b[0]+1);
                for(int i=0; i<cnt[0]-min(cnt[0],cnt[1]); i++)
                    printf("%d ", b[0]);
                for(int i=0; i<cnt[1]-min(cnt[0],cnt[1]); i++)
                    printf("%d ", b[1]);
                printf("\n");
            }

            else{
                printf("%d\n", n);
                for(int i=0;i<n;i++)
                    printf("%d ", a[i]);
                printf("\n");
            }
        }
        else {
            for(set<int>::iterator it = s.begin(); it!=s.end(); it++)
                b[tol++] = *it;

            for(int i=0; i<n; i++){
                for(int j=0; j<3; j++)
                    if(a[i] == b[j]) cnt[j]++;
            }

            //printf("%d %d %d\n", cnt[0], cnt[1], cnt[2]);
            if(cnt[1] + max(cnt[0], cnt[2]) - min(cnt[0], cnt[2]) > cnt[0]+cnt[2]+(cnt[1]&1))
            {
                printf("%d\n", cnt[0] + cnt[2] + (cnt[1]&1));
                if(cnt[1] & 1) printf("%d ", b[1]);

                for(int i=1; i*2<=cnt[1]; i++)
                    printf("%d %d ",b[0], b[2]);
                for(int i=0; i<cnt[0]; i++) printf("%d ", b[0]);
                for(int i=0; i<cnt[2]; i++) printf("%d ", b[2]);
            }
            else {
                printf("%d\n", cnt[1] + max(cnt[0], cnt[2]) - min(cnt[0], cnt[2]));
                for(int i=0; i<cnt[0]-min(cnt[0], cnt[2]); i++)
                    printf("%d ", b[0]);
                for(int i=0; i<cnt[1]+2*min(cnt[0], cnt[2]); i++)
                    printf("%d ", b[1]);
                for(int i=0; i<cnt[2]-min(cnt[0],cnt[2]); i++)
                    printf("%d ", b[2]);
            }
            printf("\n");
        }
    }
};
D. Peculiar apple-tree

In Arcady's garden there grows a peculiar apple-tree that fruits one time per year. Its peculiarity can be explained in following way: there are n inflorescences, numbered from 1 to n. Inflorescence number 1 is situated near base of tree and any other inflorescence with number i(i > 1) is situated at the top of branch, which bottom is pi-th inflorescence and pi < i.

Once tree starts fruiting, there appears exactly one apple in each inflorescence. The same moment as apples appear, they start to roll down along branches to the very base of tree. Each second all apples, except ones in first inflorescence simultaneously roll down one branch closer to tree base, e.g. apple in a-th inflorescence gets to pa-th inflorescence. Apples that end up in first inflorescence are gathered by Arcady in exactly the same moment. Second peculiarity of this tree is that once two apples are in same inflorescence they annihilate. This happens with each pair of apples, e.g. if there are 5 apples in same inflorescence in same time, only one will not be annihilated and if there are 8 apples, all apples will be annihilated. Thus, there can be no more than one apple in each inflorescence in each moment of time.

Help Arcady with counting number of apples he will be able to collect from first inflorescence during one harvest.

Input

First line of input contains single integer number n (2 ≤ n ≤ 100 000)  — number of inflorescences.

Second line of input contains sequence of n - 1 integer numbers p2, p3, ..., pn (1 ≤ pi < i), where pi is number of inflorescence into which the apple from i-th inflorescence rolls down.

Output

Single line of output should contain one integer number: amount of apples that Arcady will be able to collect from first inflorescence during one harvest.

Examples
input
Copy
3
1 1
output
1

D题,给一棵树,树上的各个节点都会结果,果会在每个单位时间往树根移动一个节点。当偶数个果子同时到达一个节点时会连连看消失掉,例如4个果子全部消失,3个果子则能留下1个。问最后能收获多少个果子。

一开始题面让的one harvest让我以为每个结点的果子只移动一次,就以为只需要判断每个结点后继个数的奇偶性再加和就可以了。结果发现所有的果子都要跑到根为止,那就再码一发BFS判断每层节点的奇偶性吧,然后就过了。。C题在场上WA了几发开始看D,结果D这么水....好在场上A了三题出来。

namespace Solver {
    int n, m;

    void dfs(int x, int cnt)
    {
        //printf("**%d: %d\n", x, cnt);
        vis[cnt]++;
        umax(tol, cnt);
        for(int i=0; i<v[x].size(); i++)
        {
            dfs(v[x][i], cnt+1);
        }
        return ;
    }

    void solve()
    {
        scanf("%d", &n);
        int sum = 0;
        memset(vis, 0, sizeof vis);
        for(int i=2; i<=n; i++){
            scanf("%d", &m);
            v[m].push_back(i);
        }

        dfs(1, 1);
        //printf("[%d]\n", tol);
        for(int i=1; i<=tol; i++){
            //printf("%d: %d\n", i, vis[i]);
            if(vis[i]&1) sum++;
        }

        printf("%d\n", sum);
    }
};

总结:中午两个小时开一场cf绰绰有余而且还比较舒服,但是可能是因为这种virtual场没有压力的缘故,读题又慢手写bug还多。思路不够清晰,之后要好好理一理。C题感觉就是一个纯的模拟,结果因为手残WA了N发。sad

我什么时候才够资格去刷div1的题啊,都不说打div1了,能会做div1的题也行啊。sad

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值