hpuacm15级第13周周练

来源:互联网 发布:ubuntu登陆后桌面假死 编辑:程序博客网 时间:2024/05/16 10:13


A - Wet Shark and Odd and Even
Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u
Submit
 
Status
Description
Today, Wet Shark is given n integers. Using any of these integers no more than once, Wet Shark wants to get maximum possible even (divisible by 2) sum. Please, calculate this value for Wet Shark.

Note, that if Wet Shark uses no integers from the n integers, the sum is an even integer 0.

Input
The first line of the input contains one integer, n (1 ≤ n ≤ 100 000). The next line contains n space separated integers given to Wet Shark. Each of these integers is in range from 1 to 109, inclusive.

Output
Print the maximum possible even sum that can be obtained if we use some of the given integers.

Sample Input
Input
3
1 2 3
Output
6
Input
5
999999999 999999999 999999999 999999999 999999999
Output
3999999996
Hint
In the first sample, we can simply take all three integers for a total sum of 6.

In the second sample Wet Shark should take any four out of five integers 999 999 999.


代码:
#include<cstdio>#include<cstring>#include<queue>#include<algorithm>using namespace std;#define LL __int64LL x,s,a;int main(){int n;scanf("%d",&n);x=0;s=0;for (int i=0;i<n;i++){scanf("%I64d",&a);s+=a;if (a%2){if (x)x=min(x,a);elsex=a;}}if (s%2)s-=x;printf("%I64d\n",s);return 0;}







B - Wet Shark and Bishops
Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u
Submit
 
Status
Description
Today, Wet Shark is given n bishops on a 1000 by 1000 grid. Both rows and columns of the grid are numbered from 1 to 1000. Rows are numbered from top to bottom, while columns are numbered from left to right.
Wet Shark thinks that two bishops attack each other if they share the same diagonal. Note, that this is the only criteria, so two bishops may attack each other (according to Wet Shark) even if there is another bishop located between them. Now Wet Shark wants to count the number of pairs of bishops that attack each other.
Input
The first line of the input contains n (1 ≤ n ≤ 200 000) — the number of bishops.
Each of next n lines contains two space separated integers xi and yi (1 ≤ xi, yi ≤ 1000) — the number of row and the number of column where i-th bishop is positioned. It's guaranteed that no two bishops share the same position.
Output
Output one integer — the number of pairs of bishops which attack each other.
Sample Input
Input
5
1 1
1 5
3 3
5 1
5 5
Output
6
Input
3
1 1
2 3
3 5
Output
0
Hint
In the first sample following pairs of bishops attack each other: (1, 3), (1, 5), (2, 3), (2, 4), (3, 4) and (3, 5). Pairs (1, 2), (1, 4),(2, 5) and (4, 5) do not attack each other because they do not share the same diagonal.

代码:
#include<cstdio>#include<cstring>#include<queue>#include<algorithm>using namespace std;__int64 ZZ[4400],FF[4400];__int64 n,x,y,s;int main(){scanf("%I64d",&n);memset(ZZ,0,sizeof(ZZ));memset(FF,0,sizeof(FF));for (int i=0;i<n;i++){scanf("%I64d%I64d",&x,&y);ZZ[x+1000-y]++;FF[x+y]++;}s=0;for (int i=1;i<2*1000;i++)if (ZZ[i]>1)s+=ZZ[i]*(ZZ[i]-1)/2;for (int i=2;i<=2*1000;i++)if (FF[i]>1)s+=FF[i]*(FF[i]-1)/2;printf("%I64d\n",s);return 0;}







C - Joysticks
Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u
Submit
 
Status
Description
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.

Sample Input
Input
3 5
Output
6
Input
4 4
Output
5
代码:
#include<cstdio>#include<cstring>#include<queue>#include<algorithm>using namespace std;int main(){int a,b,c,x,y;scanf("%d%d",&a,&b);c=max(min(a-2,b-2),0);a-=c;b-=c;c*=2;while (a&&b){if (a==1&&b==1) break;x=min(a,b);y=max(a,b);x+=1;y-=2;c++;a=x;b=y;}printf("%d",c);return 0; }






D - Beautiful Paintings
Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u
Submit
 
Status
Description
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.

Sample Input
Input
5
20 30 10 50 40
Output
4
Input
4
200 100 100 200
Output
2
代码:
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;int shu[1050],n,s,ss,lp;bool fafe[1050];int main(){scanf("%d",&n);memset(fafe,true,sizeof(fafe));for (int i=0;i<n;i++)scanf("%d",&shu[i]);sort(shu,shu+n);ss=0;while (1){lp=-1;s=0;for(int i=0;i<n;i++)if (fafe[i]){if (lp==-1){lp=i;s++;fafe[i]=false;}else{if (shu[i]>shu[lp]){lp=i;s++;fafe[i]=false;}}}if (s==0||s==1) break;ss+=max(s-1,0);}printf("%d\n",ss);return 0;}






E - Watchmen
Time Limit:3000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u
Submit
 
Status
Description
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.

Sample Input
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
Hint
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.


题解:
只有x相同或y相同时,, |xi - xj| + |yi - yj|.   才等于.。。。。



代码:
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;struct node{int x,y;}dian[200200];bool cm(node xx,node yy){return xx.y<yy.y;}bool c(node xx,node yy){if (xx.x==yy.x)return xx.y<yy.y;elsereturn xx.x<yy.x;}__int64 s,lp;int main(){int n;scanf("%d",&n);for (int i=0;i<n;i++)scanf("%d%d",&dian[i].x,&dian[i].y);lp=0;s=0;sort(dian,dian+n,cm);for (int i=1;i<n;i++){if (dian[i].y==dian[lp].y)s+=i-lp;elselp=i;}lp=0;sort(dian,dian+n,c);for (int i=1;i<n;i++){if (dian[i].x==dian[lp].x)s+=i-lp;elselp=i;}lp=0;for (int i=1;i<n;i++){if (dian[i].x==dian[lp].x&&dian[i].y==dian[lp].y)s-=i-lp;elselp=i;}printf("%I64d\n",s);return 0;}






F - Infinite Sequence
Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u
Submit
 
Status
Description
Vasya likes everything infinite. Now he is studying the properties of a sequence s, such that its first element is equal to a (s1 = a), and the difference between any two neighbouring elements is equal to c (si - si - 1 = c). In particular, Vasya wonders if his favourite integer b appears in this sequence, that is, there exists a positive integer i, such that si = b. Of course, you are the person he asks for a help.

Input
The first line of the input contain three integers a, b and c ( - 109 ≤ a, b, c ≤ 109) — the first element of the sequence, Vasya's favorite number and the difference between any two neighbouring elements of the sequence, respectively.

Output
If b appears in the sequence s print "YES" (without quotes), otherwise print "NO" (without quotes).

Sample Input
Input
1 7 3
Output
YES
Input
10 10 0
Output
YES
Input
1 -4 5
Output
NO
Input
0 60 50
Output
NO
代码:
#include<cstdio>__int64 a,b,c;int main(){scanf("%I64d%I64d%I64d",&a,&b,&c);if (c==0){if (a==b) printf("YES\n");else printf("NO\n");}else{if ((b-a)%c==0&&(b-a)/c>=0)printf("YES\n");else printf("NO\n");}return 0;}






G - Restoring Painting
Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u
Submit
 
Status
Description
Vasya works as a watchman in the gallery. Unfortunately, one of the most expensive paintings was stolen while he was on duty. He doesn't want to be fired, so he has to quickly restore the painting. He remembers some facts about it.

The painting is a square 3 × 3, each cell contains a single integer from 1 to n, and different cells may contain either different or equal integers.
The sum of integers in each of four squares 2 × 2 is equal to the sum of integers in the top left square 2 × 2.
Four elements a, b, c and d are known and are located as shown on the picture below.

Help Vasya find out the number of distinct squares the satisfy all the conditions above. Note, that this number may be equal to 0, meaning Vasya remembers something wrong.

Two squares are considered to be different, if there exists a cell that contains two different integers in different squares.

Input
The first line of the input contains five integers n, a, b, c and d (1 ≤ n ≤ 100 000, 1 ≤ a, b, c, d ≤ n) — maximum possible value of an integer in the cell and four integers that Vasya remembers.

Output
Print one integer — the number of distinct valid squares.

Sample Input
Input
2 1 1 1 2
Output
2
Input
3 3 1 2 3
Output
6
代码:
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;int main(){int n,a,b,c,d,s1,s2,s3,s4,mi,ma;__int64 k,s;scanf("%d%d%d%d%d",&n,&a,&b,&c,&d);s1=a+b;s2=b+d;s3=d+c;s4=a+c;mi=min(min(s1,s2),min(s3,s4));ma=max(max(s1,s2),max(s3,s4));k=ma-mi;if (k>=n)//这几天智障 {printf("0\n");}else{k=n-k;s=n*k;printf("%I64d\n",s);}return 0;}







H - Bear and Game
Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u
Submit
 
Status
Description
Bear Limak likes watching sports on TV. He is going to watch a game today. The game lasts 90 minutes and there are no breaks.

Each minute can be either interesting or boring. If 15 consecutive minutes are boring then Limak immediately turns TV off.

You know that there will be n interesting minutes t1, t2, ..., tn. Your task is to calculate for how many minutes Limak will watch the game.

Input
The first line of the input contains one integer n (1 ≤ n ≤ 90) — the number of interesting minutes.

The second line contains n integers t1, t2, ..., tn (1 ≤ t1 < t2 < ... tn ≤ 90), given in the increasing order.

Output
Print the number of minutes Limak will watch the game.

Sample Input
Input
3
7 20 88
Output
35
Input
9
16 20 30 40 50 60 70 80 90
Output
15
Input
9
15 20 30 40 50 60 70 80 90
Output
90
代码:
#include<cstdio>#include<cstring>int shu[95];bool fafe;int main(){int n;scanf("%d",&n);for (int i=1;i<=n;i++)scanf("%d",&shu[i]);shu[0]=0;fafe=true;for (int i=1;i<=n;i++)if (shu[i]-shu[i-1]>15){fafe=false;printf("%d\n",shu[i-1]+15);break;}if (fafe){if (90-shu[n]>15)printf("%d\n",shu[n]+15);elseprintf("90\n");}return 0;}





I - Problems for Round
Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u
Submit
 
Status
Description
There are n problems prepared for the next Codeforces round. They are arranged in ascending order by their difficulty, and no two problems have the same difficulty. Moreover, there are m pairs of similar problems. Authors want to split problems between two division according to the following rules:

Problemset of each division should be non-empty.
Each problem should be used in exactly one division (yes, it is unusual requirement).
Each problem used in division 1 should be harder than any problem used in division 2.
If two problems are similar, they should be used in different divisions.
Your goal is count the number of ways to split problem between two divisions and satisfy all the rules. Two ways to split problems are considered to be different if there is at least one problem that belongs to division 1 in one of them and to division 2 in the other.

Note, that the relation of similarity is not transitive. That is, if problem i is similar to problem j and problem j is similar to problem k, it doesn't follow that i is similar to k.

Input
The first line of the input contains two integers n and m (2 ≤ n ≤ 100 000, 0 ≤ m ≤ 100 000) — the number of problems prepared for the round and the number of pairs of similar problems, respectively.

Each of the following m lines contains a pair of similar problems ui and vi (1 ≤ ui, vi ≤ n, ui ≠ vi). It's guaranteed, that no pair of problems meets twice in the input.

Output
Print one integer — the number of ways to split problems in two divisions.

Sample Input
Input
5 2
1 4
5 2
Output
2
Input
3 3
1 2
2 3
1 3
Output
0
Input
3 2
3 1
3 2
Output
1
代码:
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;bool lp[100100];int main(){int s,n,m,a,b,x,y;bool fafe=true;memset(lp,false,sizeof(lp));scanf("%d%d",&n,&m);a=0;b=100100;if (m==0){printf("%d\n",n-1);}else{for (int i=0;i<m;i++){scanf("%d%d",&x,&y);if (x>y){x=x+y;y=x-y;x=x-y;}lp[x]=lp[y]=true;a=max(x,a);b=min(b,y);}if (a>=b)printf("0\n");elseprintf("%d\n",b-a);}return 0;}




J - Little Artem and Presents
Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u
Submit
 
Status
Description
Little Artem got n stones on his birthday and now wants to give some of them to Masha. He knows that Masha cares more about the fact of receiving the present, rather than the value of that present, so he wants to give her stones as many times as possible. However, Masha remembers the last present she received, so Artem can't give her the same number of stones twice in a row. For example, he can give her 3 stones, then 1 stone, then again 3 stones, but he can't give her 3 stones and then again 3 stones right after that.

How many times can Artem give presents to Masha?

Input
The only line of the input contains a single integer n (1 ≤ n ≤ 109) — number of stones Artem received on his birthday.

Output
Print the maximum possible number of times Artem can give presents to Masha.

Sample Input
Input
1
Output
1
Input
2
Output
1
Input
3
Output
2
Input
4
Output
3


代码:
#include<cstdio>#include<cstdio>int main(){int n,s;scanf("%d",&n);s=n/3*2;if (n%3) s++;printf("%d\n",s);return 0;}







K - Holidays
Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u
Submit
 
Status
Description
On the planet Mars a year lasts exactly n days (there are no leap years on Mars). But Martians have the same weeks as earthlings — 5 work days and then 2 days off. Your task is to determine the minimum possible and the maximum possible number of days off per year on Mars.

Input
The first line of the input contains a positive integer n (1 ≤ n ≤ 1 000 000) — the number of days in a year on Mars.

Output
Print two integers — the minimum possible and the maximum possible number of days off per year on Mars.

Sample Input
Input
14
Output
4 4
Input
2
Output
0 2


代码:

#include<cstdio>#include<cstring>#include<queue>#include<algorithm>using namespace std;int n,s,s1,s2,sheng;int main(){scanf("%d",&n);s=n/7*2;sheng=n%7;s1=min(2,sheng);if (sheng==6) s2=1;else s2=0;s1+=s;s2+=s;printf("%d %d\n",s2,s1);return 0;}





L - Pouring Rain
Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u
Submit
 
Status
Description
A lot of people in Berland hates rain, but you do not. Rain pacifies, puts your thoughts in order. By these years you have developed a good tradition — when it rains, you go on the street and stay silent for a moment, contemplate all around you, enjoy freshness, think about big deeds you have to do.

Today everything had changed quietly. You went on the street with a cup contained water, your favorite drink. In a moment when you were drinking a water you noticed that the process became quite long: the cup still contained water because of rain. You decided to make a formal model of what was happening and to find if it was possible to drink all water in that situation.

Thus, your cup is a cylinder with diameter equals d centimeters. Initial level of water in cup equals h centimeters from the bottom.


You drink a water with a speed equals v milliliters per second. But rain goes with such speed that if you do not drink a water from the cup, the level of water increases on e centimeters per second. The process of drinking water from the cup and the addition of rain to the cup goes evenly and continuously.

Find the time needed to make the cup empty or find that it will never happen. It is guaranteed that if it is possible to drink all water, it will happen not later than after 104 seconds.

Note one milliliter equals to one cubic centimeter.

Input
The only line of the input contains four integer numbers d, h, v, e (1 ≤ d, h, v, e ≤ 104), where:

d — the diameter of your cylindrical cup,
h — the initial level of water in the cup,
v — the speed of drinking process from the cup in milliliters per second,
e — the growth of water because of rain if you do not drink from the cup.
Output
If it is impossible to make the cup empty, print "NO" (without quotes).

Otherwise print "YES" (without quotes) in the first line. In the second line print a real number — time in seconds needed the cup will be empty. The answer will be considered correct if its relative or absolute error doesn't exceed 10 - 4. It is guaranteed that if the answer exists, it doesn't exceed 104.

Sample Input
Input
1 2 3 100
Output
NO
Input
1 1 1 1
Output
YES
3.659792366325

代码:
#include<cstdio>#include<cmath>double PI,S;double d,h,v,e,lp,cha;int main(){int n,s;PI=acos(0.0)*2;//printf("%.16lf",PI);scanf("%lf%lf%lf%lf",&d,&h,&v,&e);S=d*d/4*PI;if (e*S>v){printf("NO\n");}else if (e*S<v){cha=v-e*S;lp=(h*S)/cha;printf("YES\n");printf("%lf\n",lp);}elseprintf("NO\n");return 0;}



0 0