Dashboard - Codeforces Round 889 (Div. 2) - Codeforces
A. Dalton the Teacher
题意:
给出一个排列ai,求最少进行几次交换操作swap(ai,aj)使得对于每个位置i != ai。
题解:
#define _CRT_SECURE_NO_WARNINGS
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<vector>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
const LL N = 2e5 + 10, MOD = 998244353, INF = 0x3f3f3f3f;
void solve()
{
int n, cnt = 0;
scanf("%d", &n);
for (int i = 1, x; i <= n; ++i)
{
scanf("%d", &x);
cnt += (x == i);
}
printf("%d\n", cnt + 1 >> 1);
}
int main()
{
int T = 1;
scanf("%d", &T);
while (T--)
{
solve();
}
return 0;
}
B. Longest Divisors Interval
题意:
给出一个数n,求一个最大的区间长度使得区间[l,r]中l<=i<=r,n%i==0。
题解:
实际上就是从1开始的区间...例如你的区间长度为t,那这个区间一定包含t的倍数,既然如此那不如直接从1开始。
#define _CRT_SECURE_NO_WARNINGS
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<vector>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
const LL N = 2e5 + 10, MOD = 998244353, INF = 0x3f3f3f3f;
void solve()
{
LL n, ans = 1;
scanf("%lld", &n);
while (n % ans == 0)++ans;
printf("%lld\n", ans - 1);
}
int main()
{
int T = 1;
scanf("%d", &T);
while (T--)
{
solve();
}
return 0;
}
C2. Dual (Hard Version)
题意:
给出一个长度为n的数组1<=n<=20,每个数ai,-20<=ai<=20。你可以进行一种操作:选择i,j使ai+=aj。求一种操作方案使得数组非降序。(eazy限制次数最多50次,hard限制次数最多31次)
题解:
考虑先把数组变成全非正或者全负,然后做一遍前缀和或者后缀和即可。
ez版:存在正数的情况可以把任意一个正数自己加自己五次,这样即使是1也能变成32,然后给所有负数加上32就能使数组非负。不存在正数直接后缀和。
hard:这里仅讨论变成全正,变成全负的反着整一下就行了。首先我们需要19步做前缀,所以我们还剩下31-19=12步。
对于负数个数小于等于7个的情况,我们可以把一个正数变成大于20的数(1变成32最多5步),然后把所有负数变成正数(最多7步),5+7=12刚好极限。
当对于正数和负数个数都小于12个的情况,我们找到正数的最大值和负数的最小值,如果相加大于等于0,我们可以把所有负数都加上正数的最大值,这样我们可以花最多12步使得数组非负,也是蓟县
#define _CRT_SECURE_NO_WARNINGS
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<vector>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
const LL N = 2e1 + 10, MOD = 998244353, INF = 0x3f3f3f3f;
LL a[N];
void solve()
{
vector<PII>ans;
LL n, cnt1 = 0, cnt2 = 0, mx = -20, mn = 20;
scanf("%lld", &n);
for (int i = 1; i <= n; ++i)
{
scanf("%lld", &a[i]);
if (a[i] > 0)cnt1++;
if (a[i] < 0)cnt2++;
mx = max(mx, a[i]);
mn = min(mn, a[i]);
}
if (cnt1 || cnt2)
{
if (cnt1 <= 7)//全变成负
{
int t = 0;
for (int i = 1; i <= n; ++i)
if (a[i] < 0)t = i;
if (t)
{
for (int i = 1; i <= 5; ++i)
ans.push_back({ t,t });
for (int i = 1; i <= n; ++i)
if (a[i] > 0)ans.push_back({ i,t });
for (int i = n; i > 1; --i)
ans.push_back({ i - 1,i });
}
else//没负数
{
for (int i = 1; i < n; ++i)
ans.push_back({ i + 1 ,i });
}
}
else if (cnt2 <= 7)
{
int t = 0;
for (int i = 1; i <= n; ++i)
if (a[i] > 0)t = i;
for (int i = 1; i <= 5; ++i)
ans.push_back({ t,t });
for (int i = 1; i <= n; ++i)
if (a[i] < 0)ans.push_back({ i,t });
for (int i = 1; i < n; ++i)
ans.push_back({ i + 1 ,i });
}
else if (mx + mn <= 0)
{
int t = 0;
for (int i = 1; i <= n; ++i)
if (a[i] == mn)t = i;
for (int i = 1; i <= n; ++i)
if (a[i] > 0)ans.push_back({ i,t });
for (int i = n; i > 1; --i)
ans.push_back({ i - 1,i });
}
else
{
int t = 0;
for (int i = 1; i <= n; ++i)
if (a[i] == mx)t = i;
for (int i = 1; i <= n; ++i)
if (a[i] < 0)ans.push_back({ i,t });
for (int i = 1; i < n; ++i)
ans.push_back({ i + 1 ,i });
}
}
printf("%d\n", ans.size());
for (auto& i : ans)
{
printf("%d %d\n", i.first, i.second);
// a[i.first] += a[i.second];
}
// for (int i = 1; i <= n; ++i)
// printf("%lld ", a[i]);
}
int main()
{
int T = 1;
scanf("%d", &T);
while (T--)
{
solve();
}
return 0;
}
D好抽象的bitset...写题太少完全看不出来...