Codeforces Round #752 (Div. 2)
A. Era
思路分析:
- 答案其实就是这个数减去它改变位置后的pos即可。
- 对于第一位如果不是1,那么它就要在前面插入 a [ 1 ] − 1 a[1] - 1 a[1]−1个数,来使得它能够 < = i <= i <=i,然后要注意的是,在你插入数之后,位于你刚刚插入的位置及其以后的下标都会增加,增加多少呢?其实累加起来就是 a n s ans ans,自己推一下就好了。
- 一开始直接想假了,每次都是拿后面那个数减去前面那个数累加。
代码
#include <bits/stdc++.h>
using namespace std;
long long a[200];
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--)
{
memset(a, 0, sizeof(a));
int n;
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
}
long long ans = 0;
long long tmp = 0;
for (int i = 1; i <= n; i++)
{
if (a[i] > (tmp + i))
{
ans += (a[i] - tmp - i);
tmp = ans;
}
}
cout << ans << endl;
}
return 0;
}
B. XOR Specia-LIS-t
思路分析:
- 这一题我直接傻逼了(高估了题目难度),想啥按位去了,其实不需要。
- 一开始我直接想到的是如果 n n n为偶数,那么我们直接把原数组一个一个分开即可,这样就有偶数个 1 1 1相异或,得到的答案肯定就是 0 0 0。
- 然后开始考虑奇数,如果我们能找到一对逆序数的话,我们就可以把那两个数取出来作为一个整体,那么剩下的数就只有 n − 2 n - 2 n−2个了,也就可以构成 n − 2 n - 2 n−2个 1 1 1(奇数个),然后再加上刚刚那个逆序对的话,也就一共有 n − 1 n - 1 n−1个 1 1 1,也就是偶数个 1 1 1,异或得到 0 0 0。
代码
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
int a[maxn];
int cnt;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--)
{
cnt = 0;
int n;
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
}
if (n % 2 == 0)
{
cout << "YES" << endl;
}
else
{
int flag = 0;
for (int i = 1; i <= n; i++)
{
if (i > 1 && a[i] <= a[i - 1])
{
flag = 1;
}
}
if (flag)
{
cout << "YES" << endl;
}
else
{
cout << "NO" << endl;
}
}
}
return 0;
}
C. Di-visible Confusion
思路分析:
- 我们考虑任意一个数 a i a_i ai,如果答案是YES的话,它必定会在 1 − i 1 - i 1−i位置间被删除,那么我们对于每一个数都去看一下它会不会在 1 − i 1 - i 1−i位置间被删除即可。
- 一开始没用这种做法的原因是,算法复杂度分析不出来,以为时间不太够,只能说刷题太少了,哎。
代码
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
int a[maxn];
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--)
{
bool ok = 1;
int n;
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
}
for (int i = 1; i <= n; i++)
{
bool flag = 0;
for (int j = i + 1; j >= 2; j--)
{
if (a[i] % j)
{
flag = 1;
break;
}
}
ok &= flag;
}
cout << (ok == 1 ? "YES" : "NO") << endl;
}
return 0;
}
D. Moderate Modular Mode
思路分析:
-
我们先进行分类讨论,如果 x > y x > y x>y了,那么必然有 n = x + y n = x + y n=x+y,因为 ( x + y ) m o d x = y , y m o d ( x + y ) = y (x + y) mod x = y, y mod (x + y) = y (x+y)modx=y,ymod(x+y)=y。
-
那么要考虑的情况就是 x < = y x <= y x<=y了,我们可以这样想。
-
当场做的时候属实没想到,一直在推数学公式。。。。
代码
#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll x, y;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--)
{
cin >> x >> y;
if (x > y)
{
cout << x + y << endl;
}
else
{
cout << (y - y % x / 2) << endl;
}
}
return 0;
}