# A

## 题意

Select an arbitrary interval $[l,r]$and for all values $i(0 \leq i \leq r - l)$, replace $a_{l+i}$ with $a_{l+i}$&$a_{r−i}$ at the same time.This operation can be performed any number of times. Minimize the maximum value in the sequence.

## 思路

#include <cstdio>
#include <iostream>
const int N = 150;
using namespace std;
int t, n, ans, a[N];
int main() {
ios::sync_with_stdio(false);
cin >> t;
while(t--) {
cin >> n;
for(int i = 1; i <= n; i++) cin >> a[i];
ans = a[1];
for(int i = 2; i <= n; i++) ans = ans & a[i];
cout << ans << endl;
}
return 0;
}


# B

## 题意

Given a string containing 'B', 'R', or '?'，you need to replace the '?' with 'B' or 'R' to get a new string.

At the same time, you should minimize the number of the ”BB“ and "RR".

## 思路

#include <bits/stdc++.h>
const int INF = 1 << 30;
const int N = 150;
typedef long long ll;
using namespace std;
struct node {
int x, y;
}b[N];
int t, n, cnt, num, a[N];
char c;
int main() {
cin >> t;
while(t--) {
num = cnt = 0;
cin >> n;
for(int i = 1; i <= n; i++) {
cin >> c;
if(c == '?') {
a[i] = -1;
++cnt;
}
else {
a[i] = (c == 'B') ? 1 : 2;
b[++num] = (node){i, cnt};
cnt = 0;
}
}
for(int i = 1; i <= num; i++) {
for(int j = b[i].x - 1; j >= b[i].x - b[i].y; j--) {
a[j] = (a[j + 1] == 1) ? 2 : 1;
}
}
for(int i = 1; i <= n; i++) {
if(a[i] == 1 || a[i] == 2)
a[i] == 1 ? cout << "B" : cout << "R" ;
else
a[i - 1] == 1 ? (a[i] = 2, cout << "R") : (a[i] = 1, cout << "B");
}
cout << endl;
}
return 0;
}


# C

## 题意

Follow the rules to add edges, then find a way which goes through every point exactly once.

## 思路

#include <bits/stdc++.h>
const int N = 10050;
using namespace std;
int t, n, num, a[N], ans[N];
bool flag, vis[N];
vector<int> g[N];
void dfs(int x, int step) {
if(step == n && num - 1 == step) {
flag = 1;
for(int i = 1; i <= num; i++)
cout << ans[i] << " ";
cout << endl;
return ;
}
for(int i = 0; i < g[x].size() && !flag; i++) {
int to = g[x][i];
if(vis[to])
continue;
vis[to] = 1;
ans[++num] = to;
dfs(to, step + 1);
num--;
vis[to] = 0;
}
}
int main() {
cin >> t;
while(t--) {
cin >> n;
for(int i = 1; i <= n - 1; i++)
g[i].push_back(i + 1);
for(int i = 1; i <= n; i++) {
cin >> a[i];
a[i] ? g[n + 1].push_back(i) : g[i].push_back(n + 1);
}
for(int i = 1; i <= n + 1; i++) {
if(flag)
break;
memset(vis, 0, sizeof(vis));
vis[i] = 1;
num = 1;
ans[num] = i;
dfs(i, 0);
}
if(!flag)
cout << "-1" << endl;
for(int i = 1; i <= n + 1; i++)
g[i].clear();
memset(vis, 0, sizeof(vis));
flag = 0;
}
return 0;
}


# D1

## 题意

Given two forests(A forest is an undirected graph without cycles (not necessarily connected).), you can add an edge between $u$ and $v$$(u, v \in [1, n])$ in two forests at the same time.Maximum the number of edges they can add, and which edges to add.

## 思路

#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
const int N = 1050;
using namespace std;
int n, m1, m2, u, v, fa[2][N];
struct node {
int x, y;
};
vector<node> ans;
int get_fa(bool tp, int x) { return x == fa[tp][x] ? x : fa[tp][x] = get_fa(tp, fa[tp][x]); }
int main() {
ios::sync_with_stdio(false);
cin >> n >> m1 >> m2;
for(int i = 1; i <= n; i++) fa[0][i] = fa[1][i] = i;
for(int i = 1; i <= m1; i++) {
cin >> u >> v;
fa[0][get_fa(0, v)] = get_fa(0, u);
}
for(int i = 1; i <= m2; i++) {
cin >> u >> v;
fa[1][get_fa(1, v)] = get_fa(1, u);
}
for(int i = 1; i <= n; i++) {
for(int j = i + 1; j <= n; j++) {
if((get_fa(0, i) != get_fa(0, j)) && (get_fa(1, i) != get_fa(1, j))) {
fa[0][get_fa(0, j)] = get_fa(0, i);
fa[1][get_fa(1, j)] = get_fa(1, i);
ans.push_back((node){i, j});
}
}
}
cout << ans.size() << endl;
for(int i = 0; i < ans.size(); i++)
cout << ans[i].x << " " << ans[i].y << endl;
return 0;
}

posted @ 2021-08-17 22:20  BeyondLimits  阅读(35)  评论(0编辑  收藏  举报