Codeforces Round #669 (Div. 2)
A. Ahahahahahahahaha
#include <bits/stdc++.h>
#define int ll
typedef long long ll;
const int INF = 0x3f3f3f3f3f;
const int mod = 1e9 + 7;
const int maxn = 1e6 + 10;
const int N = 50;
using namespace std;
void solve() {
int n, a = 0, b = 0;
cin >> n;
for (int i = 0,x; i < n; i++) cin >> x,x == 1 ? a++ : b++;
int t = n / 2;
if (a > b && t % 2 == 1) t++;
cout << t << endl;
for (int i = 0; i < t; ++i) cout << (a > b ? 1 : 0) << " ";
cout << endl;
}
signed main() {
// ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
int _ = 1;
cin >> _;
while (_--) {
solve();
}
return 0;
}
B. Big Vova
#include <bits/stdc++.h>
#define int ll
typedef long long ll;
const int INF = 0x3f3f3f3f3f;
const int mod = 1e9 + 7;
const int maxn = 1e3 + 10;
const int N = 50;
using namespace std;
int a[maxn],vis[maxn];
void solve() {
int n;
cin>>n;
for (int i = 1; i <=n; ++i) cin>>a[i],vis[i]=0;
sort(a+1,a+n+1);
int num=0,max_,index,gc=a[n];
while (num<n){
max_=0;
for (int i = 1; i <=n; ++i) {
int t=__gcd(gc,a[i]);
if (t>max_&&!vis[i]) max_=t,index=i;
}
gc=max_;
cout<<a[index]<<" ";
vis[index]=1;
num++;
}
cout<<endl;
}
signed main() {
// ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
int _ = 1;
cin >> _;
while (_--) {
solve();
}
return 0;
}
C. Chocolate Bunny
#include <bits/stdc++.h>
#define int ll
typedef long long ll;
const int INF = 0x3f3f3f3f3f;
const int mod = 1e9 + 7;
const int maxn = 1e4 + 10;
const int N = 50;
using namespace std;
int a[maxn],vis[maxn]={0};
void solve() {
int n,x,y,now=1;
cin>>n;
for (int i = 2; i<=n; ++i) {
cout<<"? "<<now<<" "<<i<<endl,cout.flush(),cin>>x;
cout<<"? "<<i<<" "<<now<<endl,cout.flush(),cin>>y;
x>y?(a[now]=x,vis[x]=1,now=i):(a[i]=y,vis[y]=1);
}
for(int i=1;i<=n;i++) if(!vis[i]) a[now]=i;
cout<<"! ";
for(int i=1;i<=n;i++) cout<<a[i]<<" ";
cout<<endl;
}
signed main() {
ios::sync_with_stdio(0);cin.tie(0),cout.tie(0);
int _ = 1;
// cin >> _;
while (_--) {
solve();
}
return 0;
}
D. Discrete Centrifugal Jumps
单调栈
#include <bits/stdc++.h>
#define int ll
typedef long long ll;
const int INF = 0x3f3f3f3f3f;
const int mod = 1e9 + 7;
const int maxn = 3e5 + 10;
const int N = 50;
using namespace std;
//read模板
int read(){
char c; int num, f = 1;
while(c = getchar(),!isdigit(c)) if(c == '-')f = -1; num = c - '0';
while(c = getchar(), isdigit(c)) num = num * 10 + c - '0';
return num * f;
}
int a[maxn],l[maxn],r[maxn],dp[maxn];
void solve() {
int n,topl,topr;
n=read();
for(int i=1;i<=n;i++) a[i]=read();
l[1]=r[1]=topl=topr=1,dp[1]=0;
for(int i=2;i<=n;i++){
dp[i]=dp[i-1]+1;
if (a[i]>a[i-1]){
for(int j=topl;j;j--){
dp[i]=min(dp[i],dp[l[j]]+1);
if (a[l[j]]>=a[i])break;
}
} else{
for(int j=topr;j;j--){
dp[i]=min(dp[i],dp[r[j]]+1);
if (a[r[j]]<=a[i])break;
}
}
while (topl&&a[l[topl]]<=a[i])topl--;l[++topl]=i;
while (topr&&a[r[topr]]>=a[i])topr--;r[++topr]=i;
}
cout<<dp[n];
}
signed main() {
// ios::sync_with_stdio(0);cin.tie(0),cout.tie(0);
int _ = 1;
// cin >> _;
while (_--) {
solve();
}
return 0;
}
E. Egor in the Republic of Dagestan
#include <bits/stdc++.h>
#define int ll
typedef long long ll;
const int INF = 0x3f3f3f3f3f;
const int mod = 1e9 + 7;
const int maxn = 5e5 + 10;
const int N = 50;
using namespace std;
struct node{
int to,c;
};
vector<node> g[maxn];
int col[maxn],dis[maxn];
void solve() {
int n,m;
cin>>n>>m;
for(int i=1;i<=m;i++){
int u,v,c;
cin>>u>>v>>c;
g[v].push_back({u,c});//从n遍历,所以返存边
}
memset(col,-1,sizeof(col));
memset(dis,0x3f,sizeof(dis));
queue<int >q;
q.push(n);dis[n]=0;
while (q.size()){
int v=q.front();q.pop();
for(auto& x:g[v]){
int to=x.to,c=x.c;
if (col[to]==-1){
col[to]=!c;//使1到n最大所以染成另一种颜色
} else if(col[to]==c&&dis[to]>dis[v]+1){
//再次遇到这个点,如果松弛(dis[v]>dis[u]+1),入队
// 否则它就是在最短路上直接走
dis[to]=dis[v]+1;
q.push(to);
}
}
}
cout<<(dis[1]>=n?-1:dis[1])<<endl;//1点未到达说明不通输出-1
for (int i = 1; i <=n; ++i)
cout<<(col[i]==-1? 0 : col[i]);//其余点全染0
cout<<endl;
}
signed main() {
ios::sync_with_stdio(0),cin.tie(0), cout.tie(0);
int _ = 1;
// cin >> _;
while (_--) {
solve();
}
return 0;
}