Codeforces Round #345 (Div. 2) 解题报告

来源:互联网 发布:nba2k15mc霍华德的数据 编辑:程序博客网 时间:2024/06/03 13:33

A. Joysticks


Friends are going to play console. They have two joysticks and only one charger for them. Initially first joystick is charged at a1 percent and second one is charged at a2 percent. You can connect charger to a joystick only at the beginning of each minute. In one minute joystick either discharges by 2 percent (if not connected to a charger) or charges by 1 percent (if connected to a charger).

Game continues while both joysticks have a positive charge. Hence, if at the beginning of minute some joystick is charged by 1 percent, it has to be connected to a charger, otherwise the game stops. If some joystick completely discharges (its charge turns to 0), the game also stops.

Determine the maximum number of minutes that game can last. It is prohibited to pause the game, i. e. at each moment both joysticks should be enabled. It is allowed for joystick to be charged by more than 100 percent.

Input
The first line of the input contains two positive integers a1 and a2 (1 ≤ a1, a2 ≤ 100), the initial charge level of first and second joystick respectively.

Output
Output the only integer, the maximum number of minutes that the game can last. Game continues until some joystick is discharged.

input
3 5
output
6

input
4 4
output
5

题目链接:cf-651A

题目大意:游戏规则:一个数减2,另一个数+1.直到一个数字为0,或者两个数都小于2

题目思路:直接暴力,注意结束条件

以下是代码:

#include <bits/stdc++.h>#define mst(a) memset(a,0,sizeof (a))#define FOR(i,n) for (int i = 0; i < n; i++)#define INF 1e9#define eps 1e-10using namespace std;typedef long long ll;int main(){    int a,b;    cin >> a >> b;    int ans = 0;    int dir;    if (a > 1) dir = 1;    else dir = 2;    while(1)    {        if ((a == 2 && b == 1) || (a == 1 && b == 2) || (a == 2 && b == 2))        {            ans ++;            break;        }        if (a == 1 && b == 1) break;        if (dir == 1)        {            b += a / 2;            ans += a / 2;            a %= 2;            if (a == 0) a = 2,b--,ans--;            dir = 2;        }        else if (dir == 2)        {            a += b / 2;            ans += b / 2;            b %= 2;            if (b == 0) b = 2,a--,ans--;            dir = 1;        }    }    cout << ans << endl;    return 0;}

B. Beautiful Paintings


There are n pictures delivered for the new exhibition. The i-th painting has beauty ai. We know that a visitor becomes happy every time he passes from a painting to a more beautiful one.

We are allowed to arranged pictures in any order. What is the maximum possible number of times the visitor may become happy while passing all pictures from first to last? In other words, we are allowed to rearrange elements of a in any order. What is the maximum possible number of indices i (1 ≤ i ≤ n - 1), such that ai + 1 > ai.

Input
The first line of the input contains integer n (1 ≤ n ≤ 1000) — the number of painting.

The second line contains the sequence a1, a2, …, an (1 ≤ ai ≤ 1000), where ai means the beauty of the i-th painting.

Output
Print one integer — the maximum possible number of neighbouring pairs, such that ai + 1 > ai, after the optimal rearrangement.

input
5
20 30 10 50 40
output
4

input
4
200 100 100 200
output
2

题目链接:cf-651B

题目大意:给出一个n长度的序列。问最多存在多少对ai + 1 > ai.

题目思路:eg:100 200 300 | 100 200 | 100 分块处理,把出现一次的先放前面,出现两次的排序接后面作为第二块,出现3次的…….详见代码

以下是代码:

#include <bits/stdc++.h>#define mst(a) memset(a,0,sizeof (a))#define FOR(i,n) for (int i = 0; i < n; i++)#define INF 1e9#define eps 1e-10using namespace std;typedef long long ll;int vis[1005];int cnt[1005] = {0};  int main(){    int n;    cin >> n;    int ans = 0;    int maxcnt = 0;    for (int i = 0; i < n; i++)    {        int num;        cin >> num;        vis[num]++;        maxcnt = max(maxcnt,vis[num]);  //最多的出现几次,即有多少块。    }       for (int i = 0; i < 1001; i++)    {        cnt[vis[i]]++;  //出现i次的数字有几个     }    for (int i = 1; i < 1001; i++)  //出现i次     {        if (cnt[i]) ans += cnt[i] * i;     }    cout << ans - maxcnt << endl;     return 0;}

C. Watchmen


Watchmen are in a danger and Doctor Manhattan together with his friend Daniel Dreiberg should warn them as soon as possible. There are n watchmen on a plane, the i-th watchman is located at point (xi, yi).

They need to arrange a plan, but there are some difficulties on their way. As you know, Doctor Manhattan considers the distance between watchmen i and j to be |xi - xj| + |yi - yj|. Daniel, as an ordinary person, calculates the distance using the formula .这里写图片描述

The success of the operation relies on the number of pairs (i, j) (1 ≤ i < j ≤ n), such that the distance between watchman i and watchmen j calculated by Doctor Manhattan is equal to the distance between them calculated by Daniel. You were asked to compute the number of such pairs.

Input
The first line of the input contains the single integer n (1 ≤ n ≤ 200 000) — the number of watchmen.

Each of the following n lines contains two integers xi and yi (|xi|, |yi| ≤ 109).

Some positions may coincide.

Output
Print the number of pairs of watchmen such that the distance between them calculated by Doctor Manhattan is equal to the distance calculated by Daniel.

input
3
1 1
7 5
1 5
output
2

input
6
0 0
0 1
0 2
-1 1
0 1
1 1
output
11

题目链接:cf-650A

题目大意:求 |xi - xj| + |yi - yj| 和这里写图片描述计算结果相同的点又对少对

题目思路:将两个式子平方,我们可以发现,令2 * (xi - xj) * (yi - yj) 等于0即可。所以如果两个点x相同或者y相同则满足条件。注意需要减去x,y都相同的点

以下是代码:

#include<bits\stdc++.h>using namespace std;#define ll long longmap<ll,int> x, y;map<ll,int> vis;int main(){    int n;    cin >> n;    ll a,b;    for( int i=0 ; i<n ; i++ ){        scanf("%I64d%I64d", &a, &b);        ll s = a*10000000000+b;        x[a]++, y[b]++;        vis[s]++;    }    ll ans = 0;    for( map<ll,int>::iterator i=x.begin() ; i!=x.end() ; i++ ){        ll q = i->second;        ans += q*(q-1)/2;    }    for( map<ll,int>::iterator i=y.begin() ; i!=y.end() ; i++ ){        ll q = i->second;        ans += q*(q-1)/2;    }    for( map<ll,int>::iterator i=vis.begin() ; i!=vis.end() ; i++ ){        ll q = i->second;        if( q>1 ){            ans -= q*(q-1) / 2;        }    }    cout << ans << endl;    return 0;}
1 0
原创粉丝点击