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.

题目链接:http://codeforces.com/contest/651/problem/A

题意:现有两部手机,可是只有一根充电线。两个手机不可能同时充电。如果手机不充电的话,每分钟会降2%的点,如果充电的话可以每分钟可以涨1%的电。一旦其中有一部手机没电了就直接game over了。给出这两部手机的初始电量(初始电量不超过100,但你给他们充电的话电量可以超过100)。问两部手机最久能撑多久?

分析:直接模拟一下过程就好了,找出两部手机电量最小的不小于2即可!

下面给出AC代码:

 #include <bits/stdc++.h>
using namespace std;
int main()
{
int a1,a2;
cin>>a1>>a2;
int ans=;
while()
{
if(a1<=a2)
swap(a1,a2);
if(a1<)
break;
a1-=;
a2+=;
ans++;
if(a1<=||a2<=)
break;
}
cout<<ans<<endl;
}

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.

题目链接:http://codeforces.com/contest/651/problem/B

题意:把a这个数组重新排列,使得序列中 满足条件ai+1>ai 的i尽可能的多,输出符合条件的i的数目。

分析:暴力可做,找出对应美丽值相同的最大个数t,n-t即为所求值!

下面给出AC代码:

 #include <bits/stdc++.h>
using namespace std;
int a[];
int main()
{
int n,x;
int ans=,t=-;
cin>>n;
for(int i=;i<=n;i++)
{
cin>>x;
a[x]++;
}
for(int i=;i<=;i++)
t=max(t,a[i]);
cout<<n-t<<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 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个表匠,第i个表匠的位置为(xi, yi).

他们需要安排一个任务计划,但是确发现了一些问题很难解决。马医生从第i个表匠到第j个表匠所需要的时间为|xi - xj| + |yi - yj|。然而蛋蛋所需要的时间为

要想成功完成任务,必须保证两人从第i个表匠出发,同时到达第j个表匠。现在请你计算最多有多少组表匠的位置满足条件

分析:map去搞一搞就好了!

下面给出AC代码:【这个代码编译器运行不了,我也不知道为啥,队友写的,参考参考】

 #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,x,y,ans;
map<ll,ll> a,b;
map<pair<ll,ll>,ll> c;
int main()
{
for(cin>>n;cin>>x>>y;)
ans+=(a[x]++)+(b[y]++)-(c[make_pair(x,y)]++);
cout<<ans<<endl;
return ;
}

它解:容斥原理

 #include <bits/stdc++.h>
using namespace std;
struct Node
{
int a,b;
}num[];
bool cmp1(Node a,Node b) //第一次排序
{
if(a.a==b.a)
{
return a.b<b.b;
}
return a.a<b.a;
}
bool cmp2(Node a,Node b) //第二次排序
{
if(a.b==b.b)
{
return a.a<b.a;
}
return a.b<b.b;
}
int main()
{
int n;
while(~scanf("%d",&n))
{
for(int i=;i<n;i++)
{
scanf("%d%d",&num[i].a,&num[i].b);
}
sort(num,num+n,cmp1);
__int64 temp=; //几个是相同的
__int64 tt=; //重复的个数
__int64 res=;
for(int i=;i<n;i++)
{
if(num[i].a==num[i-].a) //xi == xi-1
{
temp++;
if(num[i].b==num[i-].b)
{
tt++;
}
else
{
res-=tt*(tt-)/; //去重
tt=;
}
}
else
{
res+=temp*(temp-)/; //排列组合,从temp个两两组合的个数
res-=tt*(tt-)/;
tt=;
temp=;
}
}
if(tt!=) //判断结尾是不是有些没有去重
{
res-=tt*(tt-)/;
}
tt=;
if(temp!=) //判断结尾有些是不是没有计算
{
res+=temp*(temp-)/;
}
temp=;
sort(num,num+n,cmp2); //第二次排序
for(int i=;i<n;i++)
{
if(num[i].b==num[i-].b)
{
temp++;
}
else
{
res+=temp*(temp-)/;
temp=;
}
}
if(temp!=)
{
res+=temp*(temp-)/;
}
printf("%I64d\n",res);
}
return ;
}

Codeforces Round #345 (Div. 2)【A.模拟,B,暴力,C,STL,容斥原理】的更多相关文章

  1. Codeforces Round #345 (Div. 2) D. Image Preview 暴力 二分

    D. Image Preview 题目连接: http://www.codeforces.com/contest/651/problem/D Description Vasya's telephone ...

  2. Codeforces Round #345 (Div. 2) B. Beautiful Paintings 暴力

    B. Beautiful Paintings 题目连接: http://www.codeforces.com/contest/651/problem/B Description There are n ...

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

     cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅.....   ...

  4. Codeforces Round #345 (Div. 2)——A. Joysticks(模拟+特判)

    A. Joysticks time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

  5. Codeforces Round #249 (Div. 2) (模拟)

    C. Cardiogram time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  6. Codeforces Round #366 (Div. 2) C 模拟queue

    C. Thor time limit per test 2 seconds memory limit per test 256 megabytes input standard input outpu ...

  7. codeforces Codeforces Round #345 (Div. 1) C. Table Compression 排序+并查集

    C. Table Compression Little Petya is now fond of data compression algorithms. He has already studied ...

  8. Codeforces Round #345 (Div. 1) A. Watchmen 模拟加点

    Watchmen 题意:有n (1 ≤ n ≤ 200 000) 个点,问有多少个点的开平方距离与横纵坐标的绝对值之差的和相等: 即 = |xi - xj| + |yi - yj|.(|xi|, |y ...

  9. 题解——Codeforces Round #508 (Div. 2) T1 (模拟)

    依照题意暴力模拟即可A掉 #include <cstdio> #include <algorithm> #include <cstring> #include &l ...

随机推荐

  1. iOS UICollectionView(转一) XIB+纯代码创建:cell,头脚视图 cell间距

    之前用CollectionViewController只是皮毛,一些iOS从入门到精通的书上也是泛泛而谈.这几天好好的搞了搞苹果的开发文档上CollectionViewController的内容,亲身 ...

  2. iOS 进阶—— iOS 内存管理

    1 似乎每个人在学习 iOS 过程中都考虑过的问题 alloc retain release delloc 做了什么? autoreleasepool 是怎样实现的? __unsafe_unretai ...

  3. APP上传APP Store遇到的各种问题

    内容含敏感话题或对苹果不友好的信息(如苹果婊) 使用了友盟的统计SDK,获取了IDFA但是上传填写无广告 采用友盟IDFA的sdk,并用友盟的默认淘宝页面广告,被告知和产品内容不符(最近) App在i ...

  4. 聊天机器人(基于android)

    1.本人最近写了一个小项目关于语音聊天的,采用讯飞语音引擎和数据,看看效果 2.项目名称叫小秘书,它可以和你进行交互,可以通过语音聊天,蛮有意思的,聊天内容你也可以定制 3.如果想做这款应用,先看看我 ...

  5. Linux第九讲随笔 -进程管理 、ps aux 、

    Linux第九讲1,进程管理 Linux在执行每一个程序时,就会在内存中为这个程序建立一个进程,以便让内核可以管理这个运行中的进程,进程是系统分配各种资源,进程调度的基本单位. 怎么查看进程 一.ps ...

  6. Java订单号生成,唯一订单号(日均千万级别不重复)

    Java订单号生成,唯一订单号 相信大家都可以搜索到很多的订单的生成方式,不懂的直接百度.. 1.订单号需要具备以下几个特点. 1.1 全站唯一性. 1.2 最好可读性. 1.3 随机性,不能重复,同 ...

  7. js间隔几秒弹出一次联系框

    运行效果截图如下: 在线演示地址如下: http://demo.jb51.net/js/2015/js-3-sec-alert-dlg-codes/ 具体代码如下: <html> < ...

  8. 阿里云服务器 ubuntu14.04 配置ftp

    1.执行apt-get update 2.使用apt-get命令安装vsftp:apt-get install vsftpd -y 3.先检查一下nologin的位置,通常在/usr/sbin/nol ...

  9. popupwindow那些坑

    1. new PopupWindow(vw, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); 如果 ...

  10. [转]Linux网络配置命令ifconfig输出信息解析

    eth0      Link encap:Ethernet  HWaddr 00:1e:4f:e9:c2:84 inet addr:128.224.163.153  Bcast:128.224.163 ...