A. Era
题目大意
给你一个长度为n的数组a,每次操作可以在数组的任意位置插入任意一个正整数。使得最后数组满足,对于任意索引i,a[i]<=i。问最少的操作次数?
解题思路
暴力枚举n个数即可,如果 a[i] > i,则在 i - 1 位置插入a[i] - i 个 1 即可,最后求一下一共插入了多少个数。
AC代码
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 110;
int a[N];
int main()
{
int t;
cin >> t;
while (t--)
{
int n;
cin >> n;
int res = 0; //记录一共插入了多少个数
for (int i = 1; i <= n; i++)
{
cin >> a[i];
if (a[i] <= i + res) continue;
else res += a[i] - (i + res);
}
cout << res << endl;
}
return 0;
}
B. XOR Specia-LIS-t
题目大意
给你一个长度为n的数组,可以把该数组分割成若干个连续子数组,设k为分割后得到的子数组的个数,则,h1,h2, h3, …, hk 为对应的子数组的最长递增子序列的长度。对这些数求异或和,问是否有一种方法让异或和等于0。
解题思路
(1) 当n为偶数时,不妨将原数组分成n个长度为1的子数组,这样每个hi 都为1,将偶数个1异或起来就是0.
(2) 当n为奇数时,我们可以分割出来一个长度为2的子数组,并且改子数组对应的h为1,将剩下的n-2个数分割成n-2个子数组,这样我们就又凑出来偶数个1,结果异或和等于0。所以,我们判断一下是否存在这种情况(a[ i ] =< a[i - 1] )即可。
AC代码
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 10;
int a[N];
int main()
{
int t;
cin >> t;
while (t--)
{
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++)
{
scanf("%d", &a[i]);
}
if (n % 2 == 0)
{
puts("YES");
continue;
}
else
{
bool f = 0;
for (int i = 2; i <= n; i++)
{
if (a[i] <= a[i - 1])
{
f = 1;
break;
}
}
if (f) puts("YES");
else puts("NO");
}
}
return 0;
}
C. Di-visible Confusion
题目大意
给你一个数组a,如果对于给定数组的第i个元素不能够被下标i+1整除,则可以删除这个元素,当然删除后,对应数组的下标也会发生改变。最后是否能够把这个数组变成空数组,如果能输出YES,反之,输出NO。
解题思路
(1) 对于第 1 个元素,它只能判断是否不能被2整除。假如第一个元素可以删除,则第 2 个元素只能判断是否不能被 2 和 3整除,只要有一个不能整除就可以删掉该元素。
(2) 同理,假设前 i - 1 个元素都可以删掉,则第 i 个元素 可以和 [2 ~ i + 1]进行判断。只要有一个不能整除就可以删掉该元素,反之不能。
AC代码
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 10;
int a[N];
int main()
{
int t;
scanf("%d", &t);
while (t--)
{
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++)
{
scanf("%d", &a[i]);
}
bool bug = 1;
for (int i = 1; i <= n; i++)
{
bool flag = 0;
for (int j = 2; j <= i + 1; j++) //该层循环不会超过22次
{
if (a[i] % j)
{
flag = 1;
break;
}
}
if (!flag)
{
puts("NO");
bug = 0;
break;
}
}
if (bug) puts("YES");
}
return 0;
}
D. Moderate Modular Mode
题目大意
给你两个偶数 x , y ,找到一个n满足该条件:n % x = y % n 。
解题思路
(1) 当 x = y 时,不妨让n = x。
(2) 当 x > y 时,n = x + y。因为(x + y)% x = y%(x + y)= y
(3) 当 x < y 时,n = (y / x * x + y) / 2。
AC代码
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
int t;
cin >> t;
while (t--)
{
int x, y;
cin >> x >> y;
if (x == y) cout << x << endl;
else if (x > y) cout << x + y << endl;
else cout << (y / x * x + y) / 2 << endl;
}
return 0;
}