AtCoder Beginner Contest 094

原创 2018年04月17日 19:08:59

比赛索引:https://abc094.contest.atcoder.jp/

A - Cats and Dogs


Time limit : 2sec / Memory limit : 256MB

Score : 100 points

Problem Statement

There are a total of A+B cats and dogs.Among them, A are known to be cats, but the remaining B are not known to be either cats or dogs.

Determine if it is possible that there are exactly X cats among these A+B animals.

Constraints

  • 1A100
  • 1B100
  • 1X200
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B X

Output

If it is possible that there are exactly X cats, print YES; if it is impossible, print NO.


Sample Input 1

Copy
3 5 4

Sample Output 1

Copy
YES

If there are one cat and four dogs among the B=5 animals, there are X=4 cats in total.


Sample Input 2

Copy
2 2 6

Sample Output 2

Copy
NO

Even if all of the B=2 animals are cats, there are less than X=6 cats in total.


Sample Input 3

Copy
5 3 2

Sample Output 3

Copy
NO

Even if all of the B=3 animals are dogs, there are more than X=2 cats in total.

题目分析:

      已知有A,B,X。A表示已经知道有A只Cat,A+B表示一共有A+B只Cat和Dog,问这A+B只animals中,是否有可能有X只Cat。题目很简单->但是有一个小坑,Cat最多有A+B-1条,判断的时候要注意。

代码如下:

    #include <cmath>
    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    #define DB double
    #define SG string
    #define LL long long
    #define G(A,B) G[A][B]
    #define Fp(A,B,C,D) for(A=B;A<=C;A+=D)
    #define Fm(A,B,C,D) for(A=B;A>=C;A-=D)
    #define Clear(A) memset(A,0,sizeof(A))
    using namespace std;
    const LL Max=1e3+5;
    const LL Mod=1e9+7;
    const LL Inf=1e18;
    inline LL Read(){
    	LL X=0;char CH=getchar();bool F=0;
    	while(CH>'9'||CH<'0'){if(CH=='-')F=1;CH=getchar();}
    	while(CH>='0'&&CH<='9'){X=(X<<1)+(X<<3)+CH-'0';CH=getchar();}
    	return F?-X:X;
    }
    inline void Write(LL X){
    	if(X<0)X=-X,putchar('-');
    	if(X>9)Write(X/10);
    	putchar(X%10+48);
    }
    int main(){
    	LL I,J,K;
    	LL A=Read(),B=Read(),C=Read();
    	if(C>=A&&C<=A+B-1){
    		puts("YES");
    	} else {
    		puts("NO");
    	}
    	return 0; 
    }

B - Toll Gates


Time limit : 2sec / Memory limit : 256MB

Score : 200 points

Problem Statement

There are N+1 squares arranged in a row, numbered 0,1,…,N from left to right.

Initially, you are in Square X.You can freely travel between adjacent squares. Your goal is to reach Square 0 or Square N.However, for each i=1,2,…,M, there is a toll gate in Square Ai, and traveling to Square Ai incurs a cost of 1.It is guaranteed that there is no toll gate in Square 0, Square X and Square N.

Find the minimum cost incurred before reaching the goal.

Constraints

  • 1N100
  • 1M100
  • 1XN1
  • 1A1<A2<<AMN
  • AiX
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M X
A1 A2  AM

Output

Print the minimum cost incurred before reaching the goal.


Sample Input 1

Copy
5 3 3
1 2 4

Sample Output 1

Copy
1

The optimal solution is as follows:

  • First, travel from Square 3 to Square 4. Here, there is a toll gate in Square 4, so the cost of 1 is incurred.
  • Then, travel from Square 4 to Square 5. This time, no cost is incurred.
  • Now, we are in Square 5 and we have reached the goal.

In this case, the total cost incurred is 1.


Sample Input 2

Copy
7 3 2
4 5 6

Sample Output 2

Copy
0

We may be able to reach the goal at no cost.


Sample Input 3

Copy
10 7 5
1 2 3 4 6 8 9

Sample Output 3

Copy
3

题目分析:

      已知N+1个空格(编号0~N),其中经过某M个空格需要花费1,你现在在X号格子,你可以到达相邻的两个格子。问你到达0号或者N号格子的最小花费值。

      这道题也是一道水题,我们先将M个空格按照编号从小到大排序(防止测试数据无序),然后找X在这M个编号的位置。X左边的编号数就是需要的花费数,最后我们比较一下去最小即可。

代码如下:

    #include <cmath>
    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    #define DB double
    #define SG string
    #define LL long long
    #define G(A,B) G[A][B]
    #define Fp(A,B,C,D) for(A=B;A<=C;A+=D)
    #define Fm(A,B,C,D) for(A=B;A>=C;A-=D)
    #define Clear(A) memset(A,0,sizeof(A))
    using namespace std;
    const LL Max=1e3+5;
    const LL Mod=1e9+7;
    const LL Inf=1e18;
    LL N,M,X,A[Max];
    inline LL Read(){
    	LL X=0;char CH=getchar();bool F=0;
    	while(CH>'9'||CH<'0'){if(CH=='-')F=1;CH=getchar();}
    	while(CH>='0'&&CH<='9'){X=(X<<1)+(X<<3)+CH-'0';CH=getchar();}
    	return F?-X:X;
    }
    inline void Write(LL X){
    	if(X<0)X=-X,putchar('-');
    	if(X>9)Write(X/10);
    	putchar(X%10+48);
    }
    int main(){
    	LL I,J,K;
    	N=Read(),M=Read(),X=Read();
    	Fp(I,1,M,1){
    		A[I]=Read();
    	}sort(A+1,A+1+M);
    	Fp(I,1,M-1,1){
    		if(A[I]<X&&X<A[I+1]){
    			break;
    		}
    	}Write(min(I,M-I));
    	return 0;
    }

C - Many Medians

->感觉题目有些东西复制粘贴不过来大家可以自己翻一下。

题目分析:

      L是一个奇数,定义某L个数中的中数是这个L个数中权值排名第(L+1)/2的数。

      输入N个数(N为偶数),输出N行。对于每I行,输出这N个数中(不考虑AI)中的中数大小。

      其实理解题意以后, 我们不难得到以下的做法:我们可以将这N个数记录编号后排序,然后对于每组数,查找当前忽略的数的位置,然后得到这个数的编号,比较这个数的编号与N/2的大小,如果当前忽略的数的编号>N/2的话,我们就可以输出这N个数排序后第N/2个数的值,否则,就输出这N个数排序后第N/2+1个数的值。

代码如下:

    #include <cmath>
    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    #define DB double
    #define SG string
    #define LL long long
    #define G(A,B) G[A][B]
    #define Fp(A,B,C,D) for(A=B;A<=C;A+=D)
    #define Fm(A,B,C,D) for(A=B;A>=C;A-=D)
    #define Clear(A) memset(A,0,sizeof(A))
    using namespace std;
    const LL Max=2e5+5;
    const LL Mod=1e9+7;
    const LL Inf=1e18;
    struct Node{
    	LL ID,V;
    }G[Max];
    LL N,A[Max];
    inline LL Read(){
    	LL X=0;char CH=getchar();bool F=0;
    	while(CH>'9'||CH<'0'){if(CH=='-')F=1;CH=getchar();}
    	while(CH>='0'&&CH<='9'){X=(X<<1)+(X<<3)+CH-'0';CH=getchar();}
    	return F?-X:X;
    }
    inline void Write(LL X){
    	if(X<0)X=-X,putchar('-');
    	if(X>9)Write(X/10);
    	putchar(X%10+48);
    }
    bool Cmp(Node A,Node B){
    	return A.V<B.V;
    }
    LL Find(LL V){
    	LL I,J,K;
    	LL Left=1,Right=N;
    	while(Left<=Right){
    		LL Mid=Left+Right>>1;
    		if(G[Mid].V>V){
    			Right=Mid-1;
    		} else if(G[Mid].V<V){
    			Left=Mid+1;
    		} else {
    			return Mid;
    		}
    	}
    }
    int main(){
    	LL I,J,K;
    	N=Read();
    	Fp(I,1,N,1){
    		A[I]=G[I].V=Read();G[I].ID=I;
    	}sort(G+1,G+1+N,Cmp);
    	Fp(I,1,N,1){
    		LL P=Find(A[I]);
    		if(P>(N>>1)){
    			Write(G[(N>>1)].V);puts("");
    		} else {
    			Write(G[(N>>1)+1].V);puts("");
    		}
    	}
    	return 0;
    }

D - Binomial Coefficients


Time limit : 2sec / Memory limit : 256MB

Score : 400 points

Problem Statement

Let comb(n,r) be the number of ways to choose r objects from among n objects, disregarding order.From n non-negative integers a1,a2,…,an, select two numbers ai>aj so that comb(ai,aj) is maximized.If there are multiple pairs that maximize the value, any of them is accepted.

Constraints

  • 2n105
  • 0ai109
  • a1,a2,…,an are pairwise distinct.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

n
a1 a2  an

Output

Print ai and aj that you selected, with a space in between.


Sample Input 1

Copy
5
6 9 4 2 11

Sample Output 1

Copy
11 6

comb(ai,aj) for each possible selection is as follows:

  • comb(4,2)=6
  • comb(6,2)=15
  • comb(6,4)=15
  • comb(9,2)=36
  • comb(9,4)=126
  • comb(9,6)=84
  • comb(11,2)=55
  • comb(11,4)=330
  • comb(11,6)=462
  • comb(11,9)=55

Thus, we should print 11 and 6.


Sample Input 2

Copy
2
100 0

Sample Output 2

Copy
100 0

题目分析:

      已知N个数,Ai,Aj为其中的某两个数。求C(Ai,Aj)的最大值(Ai>Aj)。

      由组合数的定义不难得到一个小性质:如果P>Q>=M,那么C(P,M)恒大于C(Q,M)。于是我们最后的答案一定包含这个N个数的最大数。那么问题就转为了求这C(Max,Ai)的最大值。联想一下杨辉三角,可以知道其中一行的最大值是这一行最中间的数,于是我们只需要找到这N个数中最接近Max/2的数即可。

代码如下:

    #include <cmath>
    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    #define DB double
    #define SG string
    #define LL long long
    #define G(A,B) G[A][B]
    #define Fp(A,B,C,D) for(A=B;A<=C;A+=D)
    #define Fm(A,B,C,D) for(A=B;A>=C;A-=D)
    #define Clear(A) memset(A,0,sizeof(A))
    using namespace std;
    const LL Max=1e5+5;
    const LL Mod=1e9+7;
    const LL Inf=1e18;
    LL N,Cnt=0,G[Max];
    LL Ans,Tmp=Inf;
    inline LL Read(){
    	LL X=0;char CH=getchar();bool F=0;
    	while(CH>'9'||CH<'0'){if(CH=='-')F=1;CH=getchar();}
    	while(CH>='0'&&CH<='9'){X=(X<<1)+(X<<3)+CH-'0';CH=getchar();}
    	return F?-X:X;
    }
    inline void Write(LL X){
    	if(X<0)X=-X,putchar('-');
    	if(X>9)Write(X/10);
    	putchar(X%10+48);
    }
    int main(){
    	LL I,J,K;
    	N=Read();
    	Fp(I,1,N,1){
    		G[I]=Read();
    	}sort(G+1,G+1+N);
    	Fp(I,1,N-1,1){
    		if(Tmp>=abs((G[N]>>1)-G[I])){
    			Ans=G[I];
    			Tmp=abs((G[N]>>1)-G[I]);
    		}
    	}cout<<G[N]<<" "<<Ans;
    	return 0;
    }

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yanyidu/article/details/79979014

AtCoder Beginner Contest 091 题解

               AtCoder Beginner Contest 091总结:原本升蓝了要打regular...结果报错名了,只能打不计分的了...A题AC代码:#include &am...
  • sortmin
  • sortmin
  • 2018年03月18日 01:45
  • 55

AtCoder Beginner Contest 055

A #include #include #include #include #include #include #include #include #include using n...
  • sasuke__
  • sasuke__
  • 2017年02月18日 21:15
  • 197

AtCoder Regular Contest 093(AtCoder Beginner Contest 092)题解

               AtCoder Regular Contest 093C题题意:给定N个点,求每次去掉一个点后,依次走过剩余每个点的距离和,最后需要返回起点。C题题解:用一个数组B记录每...
  • sortmin
  • sortmin
  • 2018年03月26日 00:54
  • 54

AtCoder Beginner Contest 087-D - People on a Line

D - People on a Line Time limit : 2sec / Memory limit : 256MB Score : 400 points Pr...
  • sortmin
  • sortmin
  • 2018年02月02日 23:22
  • 35

AtCoder Beginner Contest 057 - 初探atcoder

ZJOI讲课听说了atcoder这个OJ,感觉时间段很兹磁,都是晚上八点到九点四十。准备来一发...
  • QWsin
  • QWsin
  • 2017年03月26日 20:48
  • 1360

AtCoder Beginner Contest 075

A 题 cout&lt;&lt;(a^b^c)&lt;&lt;endl; B – Minesweeper(来源atcoder) a) 题意,求一个点上下左右共有...
  • weixin_38686780
  • weixin_38686780
  • 2017年10月15日 11:42
  • 91

AtCoder Regular Contest 093(E-Bichrome Spanning Tree)

题意: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&...
  • hnust_Derker
  • hnust_Derker
  • 2018年03月27日 18:47
  • 73

AtCoder Beginner Contest 086 D - Checker

D - CheckerTime limit : 2sec / Memory limit : 256MBScore : 500 pointsProblem StatementAtCoDeer is th...
  • aaakirito
  • aaakirito
  • 2018年02月26日 23:50
  • 36

AtCoder Beginner Contest 086 C - Traveling

AtCoder 086 C Beginner Concest 086C Problem Statement AtCoDeer the deer is going on a trip in a two...
  • Jasmineaha
  • Jasmineaha
  • 2018年01月29日 00:30
  • 58

AtCoder Beginner Contest 088

AtCoder Beginner Contest 088总结:rank203,+90分。C题考虑复杂了,直接暴力就好,总体很简单,D题最短路模板题。题目链接:https://abc088.contes...
  • sortmin
  • sortmin
  • 2018年02月19日 00:26
  • 46
收藏助手
不良信息举报
您举报文章:AtCoder Beginner Contest 094
举报原因:
原因补充:

(最多只允许输入30个字)