A
https://codeforces.com/contest/1759/problem/A
思路:
题目给的s最多有50个Yes,所以可以先定义一个50个Yes长度的串s,
最后判断听到的字符串是否是s的字串即可
代码:
#include <bits/stdc++.h>
//#include <iostream>
#define nops string::npos
using namespace std;
const int N = 1e5 + 10;
int t;
int a[N];
int main() {
cin >> t;
while (t --)
{
string s,s1;
for (int i = 1; i <= 50;i ++)
s1 += "Yes";
cin >> s;
if (s1.find(s) != nops) puts("YES");
else puts("NO");
}
return 0;
}
B
https://codeforces.com/contest/1759/problem/B
思路(暴力):
把输入的每个数都标记,然后从空的最小的数开始枚举,如果有几个数可以构成s,
则判断当把这几个数插入到集合中后,是否是满足条件(1~n都有且都只有一个);若构成的和
大于s,则直接输出no即可
代码
#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
int b[N];
int main()
{
int t;
cin >> t;
while (t --)
{
int m,s;
cin >> m >> s;
memset(a,0,sizeof a);
for (int i = 0; i < m;i ++)
{
int x;
cin >> x;
a[x] = 1;//标记
}
int sum = 0;
bool f = true;
int res = 0;//记录构成s的数的个数
for (int i = 1; i <= N;i ++)
{
if (!a[i])
{
sum += i;
res ++;
a[i] = 1;
}
if (sum == s) break;
else if (sum > s)
{
f = false;
break;
}
}
if (!f) cout << "NO" << endl;
else
{
for (int i = 1;i <= res + m;i ++)//判断从1~res + m是否都在数组中
{
if (!a[i])
{
f = false;
break;
}
}
if (f) cout << "YES" << endl;
else cout << "NO" << endl;
}
}
return 0;
}
C
https://codeforces.com/contest/1759/problem/C
思路:
- a == b时,需移动0次
- a != b时 :
- 若r - l < x,则输出-1
- 若r - l >= x :
- |a - b| >= x时,一步到位
- a < b 时 (a,b) :
若 a - l >= x || r - b >= x,则两步就可以到(最优策略,移到边界再向b移)
若 r - a >= x && b - l >= x,需三步
否则,没有可到达的方案,输出-1 - a > b时(b,a) :
若 b - l >= x || r - a >= x,只需两步(最优策略,移到边界再向b移)
若 a - l >= x && r - b >= x,需三步
否则,没有方案,输出-1
代码
#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
int main()
{
int t;
cin >> t;
while (t --)
{
int l,r,x,a,b;
cin >> l >> r >> x >> a >> b;
if (a == b) cout << 0 << endl;
else
{
if (r - l < x) cout << -1 << endl;
else
{
if (abs(a - b) >= x) cout << 1 << endl;
else
{
if (a < b)
{
if (a - l >= x || r - b >= x) cout << 2 << endl;
else if (r - a >= x && b - l >= x) cout << 3 << endl;
else cout << -1 << endl;
}
else
{
if (b - l >= x || r - a >= x) cout << 2 << endl;
else if (a - l >= x && r - b>= x) cout << 3 << endl;
else cout << -1 << endl;
}
}
}
}
}
return 0;
}