Atcoder Beginner Contest094小结

原创 2018年04月16日 21:56:52

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;
}

AtCoder Regular Contest 095 C - Many Medians(去掉一个数求中位数)

Problem StatementWhen l is an odd number, the median of l numbers a1,a2,…,al is the (l+12)-th larges...
  • deepseazbw
  • deepseazbw
  • 2018年04月15日 10:32
  • 22

AtCoder Beginner Contest 091 题解

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

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

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

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 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 086 D - Checker

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

AtCoder Beginner Contest 063

这次又错过了比赛,但是想着还没做过,就补了一下。感觉真的比cf的div2简单,特别是前两个题,a+b,,beginner啊 A.a+b B.a~b C.贪心 #include #inclu...
  • huatian5
  • huatian5
  • 2017年06月04日 00:05
  • 154
收藏助手
不良信息举报
您举报文章:Atcoder Beginner Contest094小结
举报原因:
原因补充:

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