A - Perfect Permutation
构造一个数组使得ai能被i整除的个数最少。可以知道个数至少为1,1能整除任何数,那答案可以是1吗,是可以的比如构造 2 3 4 ... 1 。
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int mo = 1e3;
const int N = 1e5 + 5;
void solve()
{
int n;
cin >> n;
for(int i=2;i<=n;i++) cout<<i<<' ';
cout<<1;
cout << '\n';
return ;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--)
{
solve();
}
return 0;
}
B - Party
题意一开始没有说明确,要仔细看样例,后面比赛中也加了解释。聚会中,如果有一对朋友,这一对朋友就要吃1个蛋糕,比如a有3个朋友到场了,他会吃3个蛋糕,如果a到场了但他没有朋友,他不吃蛋糕。
所以,如果有m对朋友,m是偶数,就都可以来参加聚会。如果m是奇数,我们就要删除奇数对朋友的蛋糕,而‘不开心值’和朋友对数无关,只跟人有关,那么我们只可能删除1个人或2个人.
删掉1个人:对于所有朋友数是奇数的人,删掉这个人就可以了,我们找到“不开心值”最小的删掉。
删掉2个人:如果有一对朋友,那么删掉他们相当于删掉cnt[a]+cnt[b]-1个人,如果他们都有偶数个朋友,那么删掉这2个人相当于删掉奇数个蛋糕,结果成立。
对于2个不是朋友的人,如果1个人的朋友值是奇数,只删一个人显然更优。
如果2个人的朋友都有偶数个,删掉2人后,只删掉偶数个蛋糕,结果不成立。
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
const int mo = 1e3;
const int N = 1e5 + 5;
int n, m;
int a[N];
int ans;
int x[N], y[N];
bool cnt[N];
void solve()
{
cin >> n >> m;
ans = 100000006;
memset(cnt, 0, sizeof cnt);
for (int i = 1; i <= n; i++)
{
cin >> a[i];
//ans += a[i];
}
for (int i = 1; i <= m; i++)
{
cin >> x[i] >> y[i];
cnt[x[i]] = 1 - cnt[x[i]];
cnt[y[i]] = 1 - cnt[y[i]];
}
if (m % 2 == 0)
{
cout << "0\n";
return ;
}
for (int i = 1; i <= m; i++)
{
if ( cnt[x[i]] == 1 ) ans = min(ans, a[x[i]]);
if ( cnt[y[i]] == 1 ) ans = min(ans, a[y[i]]);
if ( cnt[y[i]] == cnt[x[i]] )
{
ans = min(ans, a[x[i]] + a[y[i]]);
}
}
cout << ans << endl;
return ;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--)
{
solve();
}
return 0;
}