Codeforces Round #775 (Div. 2) A-C
A. Game
题意:给你一个01序列,1代表陆地0代表海洋,相邻的陆地可以走,对于海洋而言你有一次机会花费i+x个金币跳到任意陆地x,问最小花费
找到第一个0和最后一个0,计算一下花费即可,特判一下全是陆地的情况。
感想:一开始读题读错了,以为是可以无限制跳,得注意
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long ll;
const int N=1e2+10,INF=1e8;
int a[N];
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
int n,f=1;
scanf("%d", &n);
for (int i = 1; i <= n;i++)
{
scanf("%d", &a[i]);
if(a[i]==0)
f = 0;
}
if(n==2||f)
printf("0\n");
else
{
int ans = 0;
int x = 0, y = 0;
for (int i = 1; i <= n;i++)
{
if(a[i]==0)
{
x = i;
break;
}
}
for (int i = n; i >= 1;i--)
{
if(a[i]==0)
{
y = i;
break;
}
}
if(x==y)
ans = 2;
else
ans = y + 2 - x;
printf("%d\n", ans);
}
}
return 0;
}
B. Game of Ball Passing
题意:有n个人在练习传球,给你长度为n的序列表示每个人传球的次数,确定他们最少用了几个球在练习传球
思路:排序一下,如果最大值大于前面所有值的和,说明1个球肯定是不能符合的,最少的球数即为a[n]-sum[n-1],如果a[n]<=sum[n-1]的话那一个球就够了
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long ll;
const int N=1e5+10,INF=1e8;
int a[N];
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
int n, cnt = 0,f=1;
scanf("%d", &n);
for (int i = 1; i <= n;i++)
{
scanf("%d", &a[i]);
if(a[i])
f = 0;
}
if(f)
{
printf("0\n");
continue;
}
sort(a + 1,a + 1 + n);
ll sum = 0;
for (int i = 1; i < n;i++)
sum += a[i];
if(a[n]<=sum)
printf("1\n");
else
printf("%d\n", a[n] - sum);
}
return 0;
}