# Codeforces Round #649 (Div. 2)

### A. XXXXX

Code
/*
* Author:  heyuhhh
* Created Time:  2020/6/13 23:07:56
*/
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <iomanip>
#include <assert.h>
#include <functional>
#include <numeric>
#define MP make_pair
#define fi first
#define se second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
#define Local
#ifdef Local
#define dbg(args...) do { cout << #args << " -> "; err(args); } while (0)
void err() { std::cout << std::endl; }
template<typename T, typename...Args>
void err(T a, Args...args) { std::cout << a << ' '; err(args...); }
template <template<typename...> class T, typename t, typename... A>
void err(const T <t> &arg, const A&... args) {
for (auto &v : arg) std::cout << v << ' '; err(args...); }
#else
#define dbg(...)
#endif
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int N = 1e5 + 5;

void run() {
int n, x; cin >> n >> x;
vector <int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
a[i] %= x;
}
vector <int> sum(n);
sum[0] = a[0];
for(int i = 1; i < n; i++) {
sum[i] = (sum[i - 1] + a[i]) % x;
}
int last = -1;
int ans = -1;
for (int i = 0; i < n; i++) {
if (sum[i] != 0) {
ans = i + 1;
} else if (last != -1) {
ans = max(ans, i - last);
}
if (sum[i] > 0 && last == -1) {
last = i;
}
}
cout << ans << '\n';
}

int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cout << fixed << setprecision(20);
int T; cin >> T; while(T--)
run();
return 0;
}


### B. Most socially-distanced subsequence

Code
/*
* Author:  heyuhhh
* Created Time:  2020/6/13 23:14:56
*/
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <iomanip>
#include <assert.h>
#include <functional>
#include <numeric>
#define MP make_pair
#define fi first
#define se second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
#define Local
#ifdef Local
#define dbg(args...) do { cout << #args << " -> "; err(args); } while (0)
void err() { std::cout << std::endl; }
template<typename T, typename...Args>
void err(T a, Args...args) { std::cout << a << ' '; err(args...); }
template <template<typename...> class T, typename t, typename... A>
void err(const T <t> &arg, const A&... args) {
for (auto &v : arg) std::cout << v << ' '; err(args...); }
#else
#define dbg(...)
#endif
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int N = 1e5 + 5;

void run() {
int n; cin >> n;
vector <int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
vector <int> b;
b.push_back(a[0]);
for (int i = 1, j; i < n; i = j) {
j = i;
if (a[j] < a[j - 1]) {
while (j < n && a[j] < a[j - 1]) ++j;
b.push_back(a[j - 1]);
} else {
while (j < n && a[j] > a[j - 1]) ++j;
b.push_back(a[j - 1]);
}
}
cout << sz(b) << '\n';
for (auto it : b) {
cout << it << ' ';
}
cout << '\n';
}

int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cout << fixed << setprecision(20);
int T; cin >> T; while(T--)
run();
return 0;
}


### C. Ehab and Prefix MEXs

• $$MEX(\{b_1,b_2,\cdots,b_i\})=a_i$$

Code
/*
* Author:  heyuhhh
* Created Time:  2020/6/15 9:28:17
*/
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <iomanip>
#include <assert.h>
#include <functional>
#include <numeric>
#define MP make_pair
#define fi first
#define se second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
#define Local
#ifdef Local
#define dbg(args...) do { cout << #args << " -> "; err(args); } while (0)
void err() { std::cout << std::endl; }
template<typename T, typename...Args>
void err(T a, Args...args) { std::cout << a << ' '; err(args...); }
template <template<typename...> class T, typename t, typename... A>
void err(const T <t> &arg, const A&... args) {
for (auto &v : arg) std::cout << v << ' '; err(args...); }
#else
#define dbg(...)
#endif
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int N = 1e6 + 5;

bool del[N];
int n;
int a[N], b[N];

void run() {
memset(b, -1, sizeof(b));
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int i = 2; i <= n; i++) {
if (a[i] != a[i - 1]) {
del[a[i - 1]] = true;
b[i] = a[i - 1];
}
}
del[a[n]] = true;
int m = 0;
for (int i = 1; i <= n; i++) if (b[i] == -1) {
while (del[m]) ++m;
b[i] = m++;
}
for (int i = 1; i <= n; i++) {
cout << b[i] << " \n"[i == n];
}
}

int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cout << fixed << setprecision(20);
run();
return 0;
}


### D. Ehab's Last Corollary

Code
/*
* Author:  heyuhhh
* Created Time:  2020/6/14 0:25:26
*/
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <iomanip>
#include <assert.h>
#include <functional>
#include <numeric>
#define MP make_pair
#define fi first
#define se second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
#define Local
#ifdef Local
#define dbg(args...) do { cout << #args << " -> "; err(args); } while (0)
void err() { std::cout << std::endl; }
template<typename T, typename...Args>
void err(T a, Args...args) { std::cout << a << ' '; err(args...); }
template <template<typename...> class T, typename t, typename... A>
void err(const T <t> &arg, const A&... args) {
for (auto &v : arg) std::cout << v << ' '; err(args...); }
#else
#define dbg(...)
#endif
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int N = 1e5 + 5;

vector <int> G[N];
int n, m, k;
int deep[N], father[N];
vector <int> d[N];

int t;
vector <int> cyc;

void find(int u, int v) {
while (u != v) {
cyc.push_back(u);
u = father[u];
}
cyc.push_back(v);
}

void dfs(int u, int fa) {
father[u] = fa;
deep[u] = deep[fa] + 1;
d[deep[u]].push_back(u);
for (auto v : G[u]) if (v != fa) {
if (deep[v] == -1) {
dfs(v, u);
} else if (deep[u] > deep[v]) {
if (deep[u] - deep[v] + 1 <= k) {
if (t == 2) continue;
t = 2;
find(u, v);
}
}
}
}

void run() {
memset(deep, -1, sizeof(deep));
cin >> n >> m >> k;
for (int i = 1; i <= m; i++) {
int u, v; cin >> u >> v;
G[u].push_back(v);
G[v].push_back(u);
}
dfs(1, 0);
if (t == 2) {
cout << 2 << '\n';
cout << sz(cyc) << '\n';
for (auto it : cyc) {
cout << it << ' ';
}
cout << '\n';
return;
}
vector <int> ans;
int need = (k + 1) / 2;
for (int i = 0; i <= n && need; i += 2) {
for (auto it : d[i]) {
ans.push_back(it);
if (--need == 0) {
break;
}
}
}
if (need) {
need = (k + 1) / 2;
ans.clear();
for (int i = 1; i <= n && need; i += 2) {
for (auto it : d[i]) {
ans.push_back(it);
if (--need == 0) {
break;
}
}
}
}
cout << 1 << '\n';
for (auto it : ans) {
cout << it << ' ';
}
cout << '\n';
}

int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cout << fixed << setprecision(20);
run();
return 0;
}


### E. X-OR

• 每次随机选择一个数$$p_i$$，并询问其余在集合$$S$$中的数，找到所有的$$p_j\&p_i=p_j$$$$j$$，并且找到$$p_i|p_j$$最小的$$val$$，那么容易发现$$val=p_i$$。然后将$$S$$清空将得到的下标集合放入$$S$$中。不断重复此过程。最后会剩下两个数，一个为$$0$$，一个为$$2^t$$，再通过随机找到$$0$$即可。

• 将之前的随机选择一个数该为随机选择两个数进行询问，若$$p_i|p_j$$的二进制位数不超过$$\frac{logn}{2}$$，那么就选择这个数然后执行上面类似的操作。

Code
/*
* Author:  heyuhhh
* Created Time:  2020/6/15 10:16:32
*/
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <iomanip>
#include <assert.h>
#include <functional>
#include <numeric>
#include <random>
#define MP make_pair
#define fi first
#define se second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
#define Local
#ifdef Local
#define dbg(args...) do { cout << #args << " -> "; err(args); } while (0)
void err() { std::cout << std::endl; }
template<typename T, typename...Args>
void err(T a, Args...args) { std::cout << a << ' '; err(args...); }
template <template<typename...> class T, typename t, typename... A>
void err(const T <t> &arg, const A&... args) {
for (auto &v : arg) std::cout << v << ' '; err(args...); }
#else
#define dbg(...)
#endif
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int N = 1e5 + 5;

mt19937 rnd(time(NULL));

void run() {
int n; cin >> n;
auto query = [&] (int i, int j) {
cout << "? " << i << ' ' << j << endl;
int x; cin >> x;
return x;
};

int high;
for (int i = 11; i >= 0; i--) {
if ((n - 1) >> i & 1) {
high = i + 1;
break;
}
}
vector <int> z(high, -1);
while (count(all(z), -1)) {
int i = rand() % n + 1, j = rand() % n + 1;
if (i == j) continue;
int val = query(i, j);
for (int k = 0; k < high; k++) {
if (!(val >> k & 1)) {
z[k] = i;
}
}
}

auto get = [&](int p) {
int res = 0;
for (int i = 0; i < high; i++) {
if (z[i] == p) continue;
int val = query(z[i], p);
if (val >> i & 1) {
res += 1 << i;
}
}
return res;
};

vector <int> ans(n, -1);
int zero = 1;
int val = get(1);
ans[0] = val;
for (int i = 2; i <= n; i++) {
int v = query(zero, i);
if ((v & val) == v) {
zero = i;
val = get(i);
ans[i - 1] = val;
}
}
for (int i = 1; i <= n; i++) {
if (ans[i - 1] == -1) {
ans[i - 1] = query(zero, i);
}
}
cout << "!";
for (auto it : ans) {
cout << ' ' << it;
}
}

int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cout << fixed << setprecision(20);
run();
return 0;
}

posted @ 2020-06-16 18:07  heyuhhh  阅读(433)  评论(10编辑  收藏  举报