Codeforces Round #709 (Div. 2, based on Technocup 2021 Final Round)
B - Restore Modulo
#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
#define int long long
typedef long long LL;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int inf = 1e18;
const int mod = 998244353;
//const int mod = 1e9 + 7;
const int maxn = 1e5 + 10;
const int N = 6e6 + 100;
int a[maxn];
void solve() {
int n;
cin >> n;
int mx = 0;
for (int i = 0; i < n; ++i) {
cin >> a[i];
mx = max(a[i], mx);
}
int f = 1;
for (int i = 1; i < n; ++i) if (a[i] != a[i - 1]) f = 0;
if (f) {
cout << 0 << "\n";
return;
}
f=1;
for (int i = 1; i < n-1; ++i) if (a[i+1]-a[i]!=a[i] - a[i - 1]) f = 0;
if (f) {
cout << 0 << "\n";
return;
}
int c = -1;
for (int j = 1; j < n; ++j) {
if (a[j] > a[j - 1]) {
if (c != -1) {
if (a[j] - a[j - 1] == c) continue;
else {
cout << -1 << "\n";
return;
}
} else c = a[j] - a[j - 1];
}
}
int m = -1;
for (int i = 1; i < n; ++i) {
if (a[i] < a[i - 1]) {
if (m == -1) {
m = a[i - 1] + c - a[i];
} else {
if ((a[i - 1] + c) % m != a[i]) {
cout << -1 << "\n";
return;
}
}
}
}
if (m < mx||c>m) {
cout << -1 << "\n";
return;
}
//cout<<m<<" "<<c;
for (int j = 1; j < n; ++j) {
if (a[j]==a[j-1]) {
cout<<-1<<"\n";
return;
}
}
cout << m << " " << c << "\n";
}
signed main() {
// ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int _ = 1;
cin >> _;
while (_--) {
solve();
}
return 0;
}
//72265672
C - Basic Diplomacy
#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
#define int long long
typedef long long LL;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int inf = 1e18;
const int mod = 998244353;
//const int mod = 1e9 + 7;
const int maxn = 1e5 + 10;
const int N = 6e6 + 100;
vector<int> a[maxn];
vector<int> pos[maxn];
int ans[maxn];
void solve() {
int n, m;
cin >> n >> m;
for (int i = 0; i <=max(m,n); ++i) {
a[i].clear();
pos[i].clear();
ans[i]=0;
}
for (int i = 1; i <=m; ++i) {
int k;
cin >> k;
for (int j = 1; j <=k; ++j) {
int x;
cin >> x;
a[i].push_back(x);
pos[x].push_back(i);
}
}
int mx = 0;
int y=0;
for (int i = 1; i <=n; ++i) {
if (pos[i].size() > mx) {
mx = pos[i].size();
y = i;
}
}
if (mx <= (m + 1) / 2) {
cout << "YES\n";
for (int i = 1; i <=m; ++i) {
cout<<a[i][0]<<" ";
}
cout<<"\n";
return;
}
for (int l = 0; l < pos[y].size(); ++l) {
int id=pos[y][l];
ans[id]=y;
}
for (int i = 0; i < pos[y].size(); ++i) {
int id = pos[y][i];
if (a[id].size() == 1)
continue;
else{
mx--;
ans[id]=(a[id][0]==y?a[id][1]:a[id][0]);
}
if (mx == (m + 1) / 2) break;
}
if (mx>(m+1)/2)cout<<"NO\n";
else{
cout<<"YES\n";
for (int i = 1; i <=m; ++i) {
if (ans[i]==0) cout<<a[i][0]<<" ";
else cout<<ans[i]<<" ";
}
}
cout<<"\n";
}
signed main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int _ = 1;
cin >> _;
while (_--) {
solve();
}
return 0;
}
//72265672
D. Playlist
队列
#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
#define int long long
typedef long long LL;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int inf = 1e18;
const int mod = 998244353;
//const int mod = 1e9 + 7;
const int maxn = 1e5 + 10;
const int N = 6e6 + 100;
int gcd(int a,int b) {return !b?a:gcd(b,a%b);}
void solve() {
int n;
cin>>n;
vector<int > a(n);
for(int& i:a) cin>>i;
vector<int > vis(n,0),res;
vector<int >nxt(n);
queue<int > q;
for (int i = 0; i < n; ++i) {
q.push(i);
nxt[i]=(i+1)%n;
}
while (!q.empty()){
int pos=q.front();q.pop();
if (!vis[pos]&&gcd(a[pos],a[nxt[pos]])==1){
vis[nxt[pos]]=1;
res.push_back(nxt[pos]+1);
nxt[pos]=nxt[nxt[pos]];
q.push(pos);
}
}
cout<<res.size();
for(auto v:res) cout<<" "<<v;
cout<<"\n";
}
signed main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int _ = 1;
cin >> _;
while (_--) {
solve();
}
return 0;
}