AtCoder Beginner Contest 094 2018/04/14 20:10:00 ~ 2018/04/14 21:50:00
这场比赛是我人生为数不多的AK,也刷新了我的名次记录,感觉很好。其实题目还是比较水的,毕竟以前比赛D题会卡我一段时间。
闲话少说,上题解:
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
- 1≤A≤100
- 1≤B≤100
- 1≤X≤200
- 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
3 5 4
Sample Output 1
YES
If there are one cat and four dogs among the B=5 animals, there are X=4 cats in total.
Sample Input 2
2 2 6
Sample Output 2
NO
Even if all of the B=2 animals are cats, there are less than X=6 cats in total.
Sample Input 3
5 3 2
Sample Output 3
NO
Even if all of the B=3 animals are dogs, there are more than X=2 cats in total.
题解:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
void read(int &x)
{
x=0;
char c=getchar();
while(c<'0' || c>'9')c=getchar();
while(c>='0' && c<='9')
{
x=x*10+c-'0';
c=getchar();
}
}
void write(int x)
{
if(x==0)
{
putchar(48);
return;
}
int len=0,dg[20];
while(x>0)
{
dg[++len]=x%10;
x/=10;
}
for(int i=len;i>=1;i--)
{
putchar(dg[i]+48);
}
}
int main()
{
int a,b,x;
cin>>a>>b>>x;
if (a+b>=x && a<=x)
{
cout<<"YES"<<endl;
}
else
{
cout<<"NO"<<endl;
}
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
- 1≤N≤100
- 1≤M≤100
- 1≤X≤N−1
- 1≤A1<A2<…<AM≤N
- Ai≠X
- 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
5 3 3 1 2 4
Sample Output 1
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
7 3 2 4 5 6
Sample Output 2
0
We may be able to reach the goal at no cost.
Sample Input 3
10 7 5 1 2 3 4 6 8 9
Sample Output 3
3
题解:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
int main()
{
int n,m,x;
cin>>n>>m>>x;
int a[m+1],i;
for (i=1;i<=m;i++)
{
cin>>a[i];
}
int ans1,ans2;
ans1=0;
ans2=0;
for (i=1;i<x;i++)
{
for (int j=1;j<=m;j++)
{
if (i==a[j])
{
ans1++;
}
}
}
for (i=x+1;i<=n;i++)
{
for (int j=1;j<=m;j++)
{
if (i==a[j])
{
ans2++;
}
}
}
if (ans1<ans2)
{
cout<<ans1;
}
else
{
cout<<ans2;
}
return 0;
}
C - Many Medians
Time limit : 2sec / Memory limit : 256MB
Score : 300 points
Problem Statement
When l is an odd number, the median of l numbers a1,a2,…,al is the (
l+1 |
2 |
)-th largest value among a1,a2,…,al.
You are given N numbers X1,X2,…,XN, where N is an even number. For each i=1,2,…,N, let the median of X1,X2,…,XN excluding Xi, that is, the median of X1,X2,…,Xi−1,Xi+1,…,XN be Bi.
Find Bi for each i=1,2,…,N.
Constraints
- 2≤N≤200000
- N is even.
- 1≤Xi≤109
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N X1 X2 ... XN
Output
Print N lines. The i-th line should contain Bi.
Sample Input 1
4 2 4 4 3
Sample Output 1
4 3 3 4
- Since the median of X2,X3,X4 is 4, B1=4.
- Since the median of X1,X3,X4 is 3, B2=3.
- Since the median of X1,X2,X4 is 3, B3=3.
- Since the median of X1,X2,X3 is 4, B4=4.
Sample Input 2
2 1 2
Sample Output 2
2 1
Sample Input 3
6 5 5 4 4 3 3
Sample Output 3
4 4 4 4 4 4
题解:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
void read(int &x)
{
x=0;
char c=getchar();
while(c<'0' || c>'9')c=getchar();
while(c>='0' && c<='9')
{
x=x*10+c-'0';
c=getchar();
}
}
int main()
{
ios::sync_with_stdio(false);
int n;
read(n);
int a[n+1],i,t[n+1];
for (i=1;i<=n;i++)
{
read(a[i]);
t[i]=a[i];
}
sort(a+1,a+n+1);
int maxv,minv;
maxv=a[n/2];
minv=a[n/2+1];
for (i=1;i<=n;i++)
{
if (t[i]<=maxv)
{
cout<<a[n/2+1]<<endl;
}
else
{
cout<<a[n/2]<<endl;
}
}
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
- 2≤n≤105
- 0≤ai≤109
- 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
5 6 9 4 2 11
Sample Output 1
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
2 100 0
Sample Output 2
100 0
题解:偏数学思想,答案是最大的数和离最大的数除以2最近的数
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
void read(int &x)
{
x=0;
char c=getchar();
while(c<'0' || c>'9')c=getchar();
while(c>='0' && c<='9')
{
x=x*10+c-'0';
c=getchar();
}
}
int main()
{
ios::sync_with_stdio(false);
int n;
read(n);
int a[n+1],t[n+1],i;
for (i=1;i<=n;i++)
{
read(a[i]);
t[i]=a[i];
}
sort(a+1,a+n+1);
double maxv,minv;
maxv=a[n]/2.0;
minv=INF;
//cout<<maxv<<endl;
for (i=1;i<=n-1;i++)
{
if (fabs(a[i]-maxv)<minv)
{
minv=fabs(a[i]-maxv);
}
}
for (i=1;i<=n;i++)
{
if (fabs(t[i]-maxv)==minv && t[i]!=a[n])
{
cout<<a[n]<<" "<<t[i]<<endl;
return 0;
}
}
return 0;
}