Codeforces Round #345 (Div. 2) [Codeforces651]

此处有目录↑

Codeforces Round #345 (Div. 2):http://codeforces.com/contest/651


A. Joysticks (贪心)

time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Examples
input
3 5
output
6
input
4 4
output
5
Note

In the first sample game lasts for 6 minute by using the following algorithm:

  • at the beginning of the first minute connect first joystick to the charger, by the end of this minute first joystick is at 4%, second is at 3%;
  • continue the game without changing charger, by the end of the second minute the first joystick is at 5%, second is at 1%;
  • at the beginning of the third minute connect second joystick to the charger, after this minute the first joystick is at 3%, the second one is at 2%;
  • continue the game without changing charger, by the end of the fourth minute first joystick is at 1%, second one is at 3%;
  • at the beginning of the fifth minute connect first joystick to the charger, after this minute the first joystick is at 2%, the second one is at 1%;
  • at the beginning of the sixth minute connect second joystick to the charger, after this minute the first joystick is at 0%, the second one is at 2%.

After that the first joystick is completely discharged and the game is stopped.

题目大意:有2个操纵杆,初始充能为a%和b%,每一秒只能对一个操纵杆充能1%,另一个操纵杆用掉2%能量,若有一个操纵杆能量为0%或者两者均为1%,则停止,求最长持续时间。

大致思路:贪心,每次选择能量低的充能即可,只需要注意仔细读题,理解终止条件

读题直接忽略了可以充能超过100%,但写代码也没有考虑能否超过100%,真走运

#include 

using namespace std;

int a,b,ans;

int main() {
    scanf("%d%d",&a,&b);
    ans=0;
    while(a>=1&&b>=1) {
        if(a==1&&b==1)
            break;
        ++ans;
        if(a>b) {
            a-=2;
            b+=1;
        }
        else {
            a+=1;
            b-=2;
        }
    }
    printf("%d\n",ans);
    return 0;
}


B. Beautiful Paintings (模拟)

time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Examples
input
5
20 30 10 50 40
output
4
input
4
200 100 100 200
output
2
Note

In the first sample, the optimal order is: 10, 20, 30, 40, 50.

In the second sample, the optimal order is: 100, 200, 100, 200.


大致题意:有n幅画,每幅画有一个迷人指数a[i],从第一幅画开始,若后面的画比前面的画更迷人(严格大于),则参观者会高兴,现在可以任意排列这n幅画,求参观者最多的高兴次数。

大致思路:求出能形成多少组严格递增的画,答案就是每组画的个数减去1的和

赛后看到别人的代码,发现这样太麻烦,可以直接统计迷人指数相同的画的最多的个数x,答案就是n-x

#include 
#include 
#include 

using namespace std;

int a,n,num[1005],cnt,ans;
bool vis[1005][1005];

int main() {
    scanf("%d",&n);
    cnt=ans=0;
    memset(num,0,sizeof(num));
    memset(vis,false,sizeof(vis));
    for(int i=0,j;i

C. Watchmen (排序||map)

time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Examples
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
Note

In the first sample, the distance between watchman 1 and watchman 2 is equal to |1 - 7| + |1 - 5| = 10 for Doctor Manhattan and  for Daniel. For pairs (1, 1)(1, 5) and (7, 5)(1, 5) Doctor Manhattan and Daniel will calculate the same distances.


题目大意:给定n个点,求哈密顿距离和普通距离相等的点的对数。

大致思路:列等式,然后两边平凡,可得:(x[i]-x[j])*(y[i]-y[j])=0,即这两个点在同一行或同一列时,这两种距离才相等。

分别按照x和y排序,统计各有多少对点,最后再减去在同一点出形成的点的对数

计算方法:对于x或y相等的m个点,这m个点可以形成C(m,2)对两种距离相等的点


方法一:排序

把ans定义成long long的时候,还想着这回不会出现溢出的问题,结果乘法溢出了,赛后才发现。。。


其实这个方法根本就不需要map,最后一个for循环可以同步统计坐标相同的点的个数

#include 
#include 
#include 
#include 

using namespace std;

int n;
long long ans=0,tmp=1;

struct Node {
    int x,y;
    bool operator < (const Node& a) const {
        return x mp;

inline bool cmp (const Node& a,const Node& b) {
    return a.y1&&(p[i].x!=p[i-1].x||p[i].y!=p[i-1].y))
            ans-=tmp*(tmp-1)/2;
    }
    printf("%I64d\n",ans);
    return 0;
}


方法二:map

看到好多人都这样写,才发现自己的方法不仅麻烦,而且时间复杂度较高,但是这个算法的代码跑的时间比我写的那个还慢。。。可能人太多了或者map用的过多

#include 
#include 

using namespace std;

int n,x,y;
long long ans=0;

map,int> pcnt;//pcnt记录坐标相同的点的个数
map xcnt,ycnt;//xcnt和ycnt分别记录x坐标和y坐标相同的个数

int main() {
    scanf("%d",&n);
    for(int i=0;i



你可能感兴趣的:(Codeforces,贪心,模拟)