Codeforces 364Div2

22 篇文章 0 订阅
22 篇文章 0 订阅

A. Cards


time limit per test1 secondmemory limit per test256 megabytes

There are n cards (n is even) in the deck. Each card has a positive integer written on it. n2 people will play new card game. At the beginning of the game each player gets two cards, each card is given to exactly one player.

Find the way to distribute cards such that the sum of values written of the cards will be equal for each player. It is guaranteed that it is always possible.
Input

The first line of the input contains integer n (2 ≤ n ≤ 100) — the number of cards in the deck. It is guaranteed that n is even.

The second line contains the sequence of n positive integers a1, a2, …, an (1 ≤ ai ≤ 100), where ai is equal to the number written on the i-th card.

Output

Print n / 2 pairs of integers, the i-th pair denote the cards that should be given to the i-th player. Each card should be given to exactly one player. Cards are numbered in the order they appear in the input.

It is guaranteed that solution exists. If there are several correct answers, you are allowed to print any of them.

Examples

Input

6
1 5 7 4 4 3

Output

1 3
6 2
4 5

Input

4
10 10 10 10

Output

1 2
3 4

Note

In the first sample, cards are distributed in such a way that each player has the sum of numbers written on his cards equal to 8.

In the second sample, all values ai are equal. Thus, any distribution is acceptable.

题意:将2 × n个数分给n个人,使得每一个人的都有两个数,并且和相等

#include <bits/stdc++.h>

using namespace std;

int arr[110];

int n;

bool vis[110];

int main()
{
    int sum = 0;

    cin>>n;

    for(int i =0;i<n;i++)
    {
        cin>>arr[i];

        sum+=arr[i];
    }

    sum/=(n/2);

    for(int i = 0;i<n;i++)
    {
        if(!vis[i])
        {
            vis[i] = true;

            for(int j = i+1;j<n;j++)
            {
                if(vis[j]) continue;
                if(arr[i] + arr[j] == sum)
                {
                    printf("%d %d\n",i+1,j+1);

                    vis[j] = true;

                    break;
                }
            }
        }
    }
    return 0;
}

B. Cells Not Under Attack

time limit per test2 secondsmemory limit per test256 megabytes

Vasya has the square chessboard of size n × n and m rooks. Initially the chessboard is empty. Vasya will consequently put the rooks on the board one after another.

The cell of the field is under rook’s attack, if there is at least one rook located in the same row or in the same column with this cell. If there is a rook located in the cell, this cell is also under attack.

You are given the positions of the board where Vasya will put rooks. For each rook you have to determine the number of cells which are not under attack after Vasya puts it on the board.
Input

The first line of the input contains two integers n and m (1 ≤ n ≤ 100 000, 1 ≤ m ≤ min(100 000, n2)) — the size of the board and the number of rooks.

Each of the next m lines contains integers xi and yi (1 ≤ xi, yi ≤ n) — the number of the row and the number of the column where Vasya will put the i-th rook. Vasya puts rooks on the board in the order they appear in the input. It is guaranteed that any cell will contain no more than one rook.

Output

Print m integer, the i-th of them should be equal to the number of cells that are not under attack after first i rooks are put.

Examples

Input

3 3
1 1
3 1
2 2

Output

4 2 0

Input

5 2
1 5
5 1

Output

16 9

Input

100000 1
300 400

Output

9999800001

Note

On the picture below show the state of the board after put each of the three rooks. The cells which painted with grey color is not under the attack.

这里写图片描述

题意:rooks可以攻击他所在的行和列的所有的格子给你一个n*n的格子,判断在放了前i个rooks之后有多少个格子不受到攻击。如果已知攻击的行数为x,列数为y,那么攻击的格子数量为 x×ny×(nx) ,所以没有受到攻击的格子数量为 n2(xny(nx))

#include <bits/stdc++.h>

using namespace std;

typedef long long LL;

const int Max = 110000;

bool x[Max];

bool y[Max];

int main()
{
    int n,m;

    scanf("%d %d",&n,&m);

    int u,v;

    LL ans1 = 0,ans2= 0;

    LL sum = 1LL*n*n;

    for(int i = 0;i<m;i++)
    {
        scanf("%d %d",&u,&v);

        if(!x[u]) ans1++;

        if(!y[v]) ans2 ++;

        x[u] = true;

        y[v]  = true;

        cout<<sum-(ans1*n+ans2*(n-ans1))<<endl;

    }
    return 0;
}

C. They Are Everywhere


time limit per test2 secondsmemory limit per test256 megabytes

Sergei B., the young coach of Pokemons, has found the big house which consists of n flats ordered in a row from left to right. It is possible to enter each flat from the street. It is possible to go out from each flat. Also, each flat is connected with the flat to the left and the flat to the right. Flat number 1 is only connected with the flat number 2 and the flat number n is only connected with the flat number n - 1.

There is exactly one Pokemon of some type in each of these flats. Sergei B. asked residents of the house to let him enter their flats in order to catch Pokemons. After consulting the residents of the house decided to let Sergei B. enter one flat from the street, visit several flats and then go out from some flat. But they won’t let him visit the same flat more than once.

Sergei B. was very pleased, and now he wants to visit as few flats as possible in order to collect Pokemons of all types that appear in this house. Your task is to help him and determine this minimum number of flats he has to visit.

Input

The first line contains the integer n (1 ≤ n ≤ 100 000) — the number of flats in the house.

The second line contains the row s with the length n, it consists of uppercase and lowercase letters of English alphabet, the i-th letter equals the type of Pokemon, which is in the flat number i.

Output

Print the minimum number of flats which Sergei B. should visit in order to catch Pokemons of all types which there are in the house.
Examples
Input

3
AaA

Output

2

Input

7
bcAAcbc

Output

3

Input

6
aaBCCe

Output

5

Note

In the first test Sergei B. can begin, for example, from the flat number 1 and end in the flat number 2.

In the second test Sergei B. can begin, for example, from the flat number 4 and end in the flat number 6.

In the third test Sergei B. must begin from the flat number 2 and end in the flat number 6.

题意:给你一个字符串,一个字符表示一个种类,判断在字符串中选取所有种类的最小的长度。很显然的尺取法,先统计一下种类的个数,然后扫一遍就可以了。

#include <bits/stdc++.h>

using namespace std;

int  vis[200];

int main()
{
    int n;

    string s;

    cin>>n;


    cin>>s;

    int num = 0;

    for(int i = 0;i<s.size();i++)
    {
        if(!vis[s[i]]) num++;
        vis[s[i]]++;
    }

    int ans = s.size();

    int ant = 0;

    int l = 0;

    memset(vis,0,sizeof(vis));

    for(int i =0 ;i<s.size();i++)
    {
        if(!vis[s[i]]) ant++;

        vis[s[i]] ++;
        if(ant == num)
        {
            while(l<s.size() && ant == num)
            {

                ans = min(i-l+1,ans);

                vis[s[l]] --;


                if(!vis[s[l]]) ant--;

                l++;
            }
        }
    }

    cout<<ans<<endl;


    return 0;
}

D. As Fast As Possible


time limit per test1 secondmemory limit per test256 megabytes

On vacations n pupils decided to go on excursion and gather all together. They need to overcome the path with the length l meters. Each of the pupils will go with the speed equal to v1. To get to the excursion quickly, it was decided to rent a bus, which has seats for k people (it means that it can’t fit more than k people at the same time) and the speed equal to v2. In order to avoid seasick, each of the pupils want to get into the bus no more than once.

Determine the minimum time required for all n pupils to reach the place of excursion. Consider that the embarkation and disembarkation of passengers, as well as the reversal of the bus, take place immediately and this time can be neglected.
Input

The first line of the input contains five positive integers n, l, v1, v2 and k (1 ≤ n ≤ 10 000, 1 ≤ l ≤ 10 9,1v1<v2109 , 1 ≤ k ≤ n) — the number of pupils, the distance from meeting to the place of excursion, the speed of each pupil, the speed of bus and the number of seats in the bus.
Output

Print the real number — the minimum time in which all pupils can reach the place of excursion. Your answer will be considered correct if its absolute or relative error won’t exceed 106 .
Examples
Input

5 10 1 2 5

Output

5.0000000000

Input

3 6 1 2 1

Output

4.7142857143

Note

In the first sample we should immediately put all five pupils to the bus. The speed of the bus equals 2 and the distance is equal to 10, so the pupils will reach the place of excursion in time 10 / 2 = 5.

从起点到终点长度为l,n个人的步行的速度为 v1 ,汽车的速度为 v2 ,每一个人最多做一次汽车,汽车的座位为k。为n个人到达终点最少的时间。如果没有汽车,所有人到达终点的时间是确定的 t=lv1 ,如果我们使用汽车,那么时间取决于最慢的人,所以我们要减少时间,每个都要减少的相同,这样才能使得结果最优,我们将n个人分为num组,汽车将每一组送的距离为s,那么对于第一组,汽车需要将他们从起点送s的距离然后返回ss,那么汽车移动的距离为s-ss,对于第 2,3,4,num1 都是如此,而最后一组不需要返回,所以汽车移动的距离为s,所以 (num1)×(sss)+sl , sss=(ss×v1v2v1+v2+sv1v2) ,我们化简一下 sl×(v1+v2)(num1)×2v1+v1+v2 ,我们可以确定汽车送的距离越长时间越少,所以 t=lsv1+sv2 ,比赛的时候懒得推公式,就写了个二分

二分版

#include <bits/stdc++.h>

using namespace std;

const double eps = 1e-9;

int n,k;

double l,v1,v2;

double t;

int dbcmp(double s)
{
    if(fabs(s)<eps) return 0;

    return s>0?1:-1;
}

double Gets(double s)
{
    return (s-(s/v2)*v1)/(v1+v2)*v1+s/v2*v1;
}

int main()
{
    scanf("%d %lf %lf %lf %d",&n,&l,&v1,&v2,&k);


    int num = (n+k-1)/k;

    double L = 0,R = l;

    t = l/v1;

    while(dbcmp(R-L)>0)
    {
        double mid = (L+R)/2;

        if(mid+(num-1)*Gets(mid)>l)
        {
            R = mid;

            continue;
        }

        if(t >((l-mid)/v1+mid/v2))
        {
            t = (l-mid)/v1+mid/v2;

            L = mid;
        }
        else R = mid;

    }
    printf("%.10f\n",t);
    return 0;
}

公式版

#include <bits/stdc++.h>

using namespace std;

const double eps = 1e-9;

int n,k;

double l,v1,v2;

double t;

int main()
{
    scanf("%d %lf %lf %lf %d",&n,&l,&v1,&v2,&k);

    int num = (n+k-1)/k;

    double s = l*(v2+v1)/((num-1)*2*v1+v1+v2);

    printf("%.10f\n",(l-s)/v1+s/v2);

    return 0;
}

E. Connecting Universities


time limit per test3 secondsmemory limit per test256 megabytes

Treeland is a country in which there are n towns connected by n - 1 two-way road such that it’s possible to get from any town to any other town.

In Treeland there are 2k universities which are located in different towns.

Recently, the president signed the decree to connect universities by high-speed network.The Ministry of Education understood the decree in its own way and decided that it was enough to connect each university with another one by using a cable. Formally, the decree will be done!

To have the maximum sum in the budget, the Ministry decided to divide universities into pairs so that the total length of the required cable will be maximum. In other words, the total distance between universities in k pairs should be as large as possible.

Help the Ministry to find the maximum total distance. Of course, each university should be present in only one pair. Consider that all roads have the same length which is equal to 1.
Input

The first line of the input contains two integers n and k (2 ≤ n ≤ 200 000, 1 ≤ k ≤ n / 2) — the number of towns in Treeland and the number of university pairs. Consider that towns are numbered from 1 to n.

The second line contains 2k distinct integers u1,u2,...,u2k(1uin) — indices of towns in which universities are located.

The next n - 1 line contains the description of roads. Each line contains the pair of integers x j and y j ( 1xj,yj  ≤ n), which means that the j-th road connects towns xj and yj. All of them are two-way roads. You can move from any town to any other using only these roads.

Output

Print the maximum possible sum of distances in the division of universities into k pairs.

Examples

Input

7 2
1 5 6 2
1 3
3 2
4 5
3 7
4 3
4 6

Output

6

Input

9 3
3 2 1 6 5 9
8 9
3 2
2 7
3 4
7 6
4 5
2 1
2 8

Output

9

Note

The figure below shows one of possible division into pairs in the first test. If you connect universities number 1 and 6 (marked in red) and universities number 2 and 5 (marked in blue) by using the cable, the total distance will equal 6 which will be the maximum sum in this example.

这里写图片描述

题意:跟你一个n个节点的树,和2*k个树中的点,求将2*k个点分为k个点对,是的点对之间的距离和最大。唐老师的结论:总存在一种方案满足这条边被算的次数等于左右关键点数量的最小值。

#include <bits/stdc++.h>

using namespace std;

typedef long long LL;

const int Max = 210000;

vector<int>E[Max];

bool vist[Max],vis[Max];

int n,k;

LL ans;

int num[Max];

void dfs(int u,int fa)
{
    if(vist[u])
    {
        num[u]++;
    }


    vis[u] = true;

    int Mi = 0;

    for(int i = 0;i<E[u].size();i++)
    {
        if(E[u][i] == fa) continue;

        dfs(E[u][i],u);

        Mi+=num[E[u][i]];

        ans += min(k*2-num[E[u][i]],num[E[u][i]]);
    }

    num[u]+=Mi;


}

int main()
{

    scanf("%d %d",&n,&k);

    int u,v;

    for(int i = 0;i<k*2;i++)
    {
        scanf("%d",&u);

        vist[u] = true;
    }

    for(int i = 0;i<n-1;i++)
    {
        scanf("%d %d",&u,&v);

        E[u].push_back(v);

        E[v].push_back(u);
    }

    ans =0;

    for(int i = 1;i<=n;i++)
    {
        if(vist[i] &&!vis[i])
        {
            dfs(i,-1);
        }
    }

    cout<<ans<<endl;

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值