Codeforces Round#495 (Div. 2)

比赛传送门

A. Sonya and Hotels

枚举每两个相邻位置,计算对答案的贡献即可

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include<cstdio>
#include<algorithm>
using namespace std;

const int maxn=105;
int n,d,a[maxn];
int main(){
scanf("%d%d",&n,&d);
for (int i=1;i<=n;i++) scanf("%d",&a[i]);
sort(a+1,a+1+n);
int ans=2;
for (int i=2;i<=n;i++)
if (a[i]-a[i-1]==2*d) ans++;else
if (a[i]-a[i-1]>2*d) ans+=2;
printf("%d",ans);
return 0;
}

B. Sonya and Exhibition

一开始想了好久,后来发现直接010101……就好了

1
2
3
4
5
6
7
#include<cstdio>

int main(){
int n;scanf("%d",&n);
for (int i=1;i<=n;i++) putchar('0'+(i&1));
return 0;
}

C. Sonya and Robots

枚举第一个机器人走的位置,看第二个机器人有多少个合法位置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include<cstdio>
#include<cstring>
#include<algorithm>
#define cl(x,y) memset(x,y,sizeof(x))
using namespace std;
typedef long long ll;

const int maxn=200005;
int n,a[maxn],N;
bool vis[maxn];
struct data{
int x,ty;
data (int _x=0,int _ty=0):x(_x),ty(_ty) {}
bool operator<(const data&b)const {return x<b.x;}
}b[maxn];
int main(){
scanf("%d",&n);
for (int i=1;i<=n;i++) scanf("%d",&a[i]);
for (int i=1;i<=n;i++)
if (!vis[a[i]]) b[++N]=data(i,0),vis[a[i]]=1;
cl(vis,0);
int m=0;
for (int i=n;i>=1;i--)
if (!vis[a[i]]) m++,b[++N]=data(i,1),vis[a[i]]=1;
sort(b+1,b+1+N);
ll ans=0;
for (int i=1,j;i<=N;i=j){
int num[2]; num[0]=num[1]=0;
for (j=i;j<=N&&b[i].x==b[j].x;j++) num[b[j].ty]++;
m-=num[1];
ans+=num[0]*m;
}
printf("%lld",ans);
return 0;
}

D. Sonya and Matrix

看这里

E. Sonya and Ice Cream

看这里