cf之路,1,Codeforces Round #345 (Div. 2)

时间:2021-08-21 00:33:03

 cf之路,1,Codeforces Round #345 (Div. 2)

ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度。所以做了round#345连试试水的深浅。。。。。

      其实这个应该是昨天就写完的,不过没时间了,就留到了今天。。

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.

题目大意:是给你两个(控制棒)电量a,b,并且只有一个充电器,每分钟的开始只能给一个充电;如果某分钟开始时,a没充电则电量减2,b充电则电量加1.如果a 或b等于0时则游戏结束(可以推出a=b=1时游戏也结束)。

现在让你求这个游戏能玩多久。

思路,对a,b比大小,使a>=b;每次给b充电,a不充,然后ab比大小使a>b,一直循环到游戏结束条件触发

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <queue>
#include <stack>
#include <map>
#include <vector> using namespace std; int main (void)
{
int a, b, t,time=;
cin >> a >> b;
if (a < b)
{
t = a;
a = b;
b = t;
}
while (a && b)
{
if (b == && a == )
{
break;
}
a -= ;
b++;
if (a < b)
{
t = a;
a = b;
b = t;
}
time++;
}
cout<<time<<endl;
return ;
}
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.

题目大意是给你一个数列a[n],按某种方式排列后,a[i]<a[i+1]时,happy time+1,求happy time的最大值

思路:先对数组从小到大排序,然后遍历,遍历时如果数字相同的则只记录一次,把剩下的相同的数放数组头部(因为数组头已经不需要再用了),并记录压入数组头的数,不同的数则为此次的happytime数。然后重复上诉过程,直到压入数组的个数为0时结束,最后的happytime和即为答案;

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <queue>
#include <stack>
#include <map>
#include <vector> using namespace std;
int a[]; int main (void)
{
int n, temp, num = , tot=;
cin>>n;
for (int i = ; i < n; i++)
{
scanf("%d", &a[i]);
}
while (n)
{
sort(a, a + n);
num = ;
temp = a[];
for (int i = ; i < n; i++)
{
if (temp == a[i])
{
a[num++] = temp;
}
temp = a[i];
}
tot += n - num -;
n = num;
}
cout<<tot<<endl;
return ;
}
C. Watchmen
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 aren 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 cf之路,1,Codeforces Round #345 (Div. 2).

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 jcalculated 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 cf之路,1,Codeforces Round #345 (Div. 2) for Daniel. For pairs (1, 1), (1, 5) and (7, 5), (1, 5) Doctor Manhattan and Daniel will calculate the same distances.

题意:题目给你n个点的坐标,你要做的是求出其中符合规则(用 cf之路,1,Codeforces Round #345 (Div. 2)和|xi - xj| + |yi - yj|得到的答案相等)的点的对数,用数学化简一下,就能发现,只要两个点的x或y相等就可以

    所以先把坐标按x排序,求出每个x相同的点的个数(然后使用组合数公式C(N,2)),再对y排序,同样求出有多少对点相同,不过此时,会重复计算:如果一对点x相同y也相同,就会重复计算

  所以最后遍历求出重复计算的点的对数(同样使用组合数公式),想想为什么

  还有注意排序的方法

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <queue>
#include <stack>
#include <map>
#include <vector> using namespace std;
struct point
{
int x, y;
};
struct point a[]; bool cmp1(point a, point b)
{
if(a.x == b.x)
return a.y<b.y;
return a.x < b.x;
}
bool cmp2(point a, point b)
{
if(a.y==b.y)
return a.x<b.x;
return a.y<b.y;
}
int main (void)
{
long long n, num = , t, temp;
struct point p;
cin >> n;
for (int i = ; i < n; i++)
{
scanf("%d%d", &a[i].x, &a[i].y);
}
//因为当x或y相等时,两人算的结果会相同,所以先按x排序,求出x相等的点
//在用组合数公式c(x,2),然后按y排序,如果一对点的
//x和y都相等,就不计入个数
sort(a, a + n, cmp1);
t = a[].x;
temp = ;
for (int i = ; i < n; i++)
{
if (t == a[i].x)
{
temp++;
}
else
{
num += temp * (temp - ) / ;
temp = ;
t = a[i].x;
}
}
num += temp * (temp - ) / ;
sort(a, a + n, cmp2);
p = a[];
temp = ;
for (int i = ; i < n; i++)
if (p.y == a[i].y )
{
temp++;
p.x=a[i].x;
}
else
{
num += temp * (temp - ) / ;
temp = ;
p = a[i];
}
num += temp * (temp - ) / ;
temp = t=;
p=a[];
for (int i = ; i < n; i++)
if (p.y == a[i].y && p.x == a[i].x)
{
temp++;
}
else
{
t+=temp*(temp+)/;
temp=;
p = a[i];
}
t+=temp*(temp+)/;
cout << num-t<< endl;
return ;
}

还有几题没写,估计也不一定写的出来,毕竟渣渣