Atcoder Beginner Contest094小結

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

  • 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.


題解:

#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

  • 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

題解:

#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,…,Xi1,Xi+1,…,XN be Bi.

Find Bi for each i=1,2,…,N.

Constraints

  • 2N200000
  • N is even.
  • 1Xi109
  • 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

Copy
4
2 4 4 3

Sample Output 1

Copy
4
3
3
4
  • Since the median of X2,X3,X4 is 4B1=4.
  • Since the median of X1,X3,X4 is 3B2=3.
  • Since the median of X1,X2,X4 is 3B3=3.
  • Since the median of X1,X2,X3 is 4B4=4.

Sample Input 2

Copy
2
1 2

Sample Output 2

Copy
2
1

Sample Input 3

Copy
6
5 5 4 4 3 3

Sample Output 3

Copy
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

  • 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

題解:偏數學思想,答案是最大的數和離最大的數除以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;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章