Codeforces Round #651 (Div. 2)

传送门
视频题解

A. Maximum GCD

输出\(\lfloor\frac{n}{2}\rfloor\)即可。

B. GCD Compression

注意\(2n\)必然是偶数,\(2n-2\)同理。
那么我们直接奇数与奇数配对,偶数与偶数配对即可。
最后最多会剩下一个奇数与偶数,直接舍弃就行。
我的代码写得比较复杂度。。枚举了\(x\)的最小质因子。。。

C. Number Game

博弈题,简单分析即可。
\(n\)拆分为\(p_1^{q_1}\cdot p_2^{q_2}\cdots p_k^{q_k}\)
然后分情况考虑就行,比如\(p_1\)是否为\(2\)\(q_2+\cdots+q_k\)是否大于\(1\)。具体分析可以见视频。

D. Odd-Even Subsequence

知道\(k\)过后奇数个数与偶数个数那么就可以确定。
因为答案的形式为\(min(奇数,偶数)\),所以可以对某一个单独考虑,另外一个不需要考虑,最后两个取\(min\)即是答案。
单独考虑的话只需要保证相邻的数隔了至少一个位置以及前后是否能放就行,实现上可以用二分来确定当前的最优答案。

E. Binary Subsequence Rotation

首先判掉不合法的情况,并且丢弃\(s_i=t_i\)的位置。
之后将新的\(s\)记为\(ss\),注意我们每次会选择形如\(0101...,1010...\)这样的序列最优,不可能出现长度为奇数或者有两个连续的字符相同。
接下来的问题就找\(ss\)串中最少有多少个这样不重复的子序列即可。
注意到若有一个为\(1\),则必然和\(0\)匹配;\(0\)也同理。并且选择的位置不会影响答案,对于一个\(1\),选择前面的\(0\)或者后面的\(0\)不影响答案,故可以选择就近一个。
接下来问题则变为一个贪心问题。每次贪心模拟就行。
细节见代码。

F2. The Hidden Pair (Hard Version)

首先通过一次询问可以得知链的长度以及链上某一点。
如果知道\(s\),通过一次询问可以得到\(f\),这个询问与深度有关。
那么确定\(s\)可能会用到深度,发现深度存在单调性,故可以二分深度来确定\(s\)
\(F1\)就可以轻松A掉了,\(F2\)限制比较严格,我们需要减少二分的次数,首先我们容易发现\(l=\lceil\frac{len}{2}\rceil\),那么可以提高下限。之后利用数据特点,\(l\)较小的情况\(r\)不可能大,\(r\)较大\(l\)不会小,所以\(r=min(r,len)\)。这样把二分区间上下限范围缩一下就行。

代码如下:


A. Maximum GCD
/*
 * Author:  heyuhhh
 * Created Time:  2020/6/21 7:30:37
 */
#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;
//head
const int N = 1e5 + 5;

void run() {
    int n; cin >> n;
    cout << n / 2 << '\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. GCD Compression
/*
 * Author:  heyuhhh
 * Created Time:  2020/6/21 7:35:28
 */
#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;
//head
const int N = 2e3 + 5;

void run() {
    int n; cin >> n;
    vector <int> a(2 * n);
    for (int i = 0; i < 2 * n; i++) {
        cin >> a[i];
    }
    for (int x = 2; x < N; x++) {
        vector <vector<int>> v(N);
        vector <pii> ans;
        for (int i = 0; i < 2 * n; i++) {
            int t = (a[i] % x);
            if (sz(v[(x - t) % x])) {
                ans.push_back(MP(v[(x - t) % x].back(), i + 1));
                v[(x - t) % x].pop_back();
            } else {
                v[t].push_back(i + 1);
            }
        }
        int cnt = 0;
        for (int i = 0; i < N; i++) {
            cnt += sz(v[i]);
        }
        if (cnt == 0 || cnt == 2) {
            if (cnt == 0) ans.pop_back();
            for (auto it : ans) {
                cout << it.fi << ' ' << it.se << '\n';
            }
            return;
        }
    }
}

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. Number Game
/*
 * Author:  heyuhhh
 * Created Time:  2020/6/21 8:00:35
 */
#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;
//head
const int N = 1e5 + 5;

void first() {
    cout << "Ashishgup" << '\n';
}
void second() {
    cout << "FastestFinger" << '\n';
}

void run() {
    int n; cin >> n;
    if (n == 1) {
        second();
        return;
    }
    if (n & 1) {
        first();
        return;
    }
    int cnt = 0, cnt2 = 0;
    while (n % 2 == 0) {
        n /= 2;
        ++cnt;
    }
    for (int i = 3; 1ll * i * i <= n; i++) {
        while (n % i == 0) {
            n /= i;
            ++cnt2;
        }
    }
    if (n > 1) ++cnt2;
    if (cnt == 1) {
        if (cnt2 != 1) {
            first();
        } else {
            second();
        }
    } else {
        if (cnt2) {
            first();
        } else {
            second();
        }
    }
}

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;
}
D. Odd-Even Subsequence
/*
 * Author:  heyuhhh
 * Created Time:  2020/6/21 8:15:28
 */
#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;
//head
const int N = 1e5 + 5;

void run() {
    int n, k; cin >> n >> k;
    vector <int> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    
    auto check = [&] (int s, int x, int m) {
        int cnt = 0;
        for (int i = s; i < n; i++) {
            if (a[i] <= x) {
                ++cnt, ++i;
                if (cnt >= m) {
                    if (s && (k & 1)) {
                        if (i < n) return true;
                        return false;
                    }
                    if (!s && !(k & 1)) {
                        if (i < n) return true;
                        return false;
                    }
                    return true;
                }
            }
        }
        return false;
    };
    auto solve = [&] (int s, int m) {
        //choose from s
        int l = 1, r = INF, mid;
        while (l < r) {
            mid = (l + r) >> 1;
            if (check(s, mid, m)) r = mid;
            else l = mid + 1;
        }
        return r;
    };
    int ans = min(solve(0, (k + 1) / 2), solve(1, k / 2));
    cout << ans << '\n';
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cout << fixed << setprecision(20);
    run();
    return 0;
}
E. Binary Subsequence Rotation
/*
 * Author:  heyuhhh
 * Created Time:  2020/6/21 8:59:29
 */
#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;
//head
const int N = 1e5 + 5;

void run() {
    int n; cin >> n;
    string s, t;
    cin >> s >> t;
    string ss = "";
    for (int i = 0; i < n; i++) {
        if (s[i] != t[i]) {
            ss += s[i];
        }
    }
    sort(all(s)), sort(all(t));
    for (int i = 0; i < n; i++) {
        if (s[i] != t[i]) {
            cout << -1 << '\n';
            return;
        }
    }
    n = ss.length();
    int ans = 0;
    int p0 = 0, p1 = 0;
    int ok0 = 0, ok1 = 0;
    for (int i = 0; i < n; i++) {
        if (ss[i] == '1') {
            if (p0) {
                ++ok0;
                --p0;
            } else {
                ++p1;
                if (ok1) {
                    --ok1;
                } else {
                    ++ans;
                }
            }
        } else {
            if (p1) {
                ++ok1;
                --p1;
            } else {
                ++p0;
                if (ok0) {
                    --ok0;
                } else {
                    ++ans;
                }
            }
        }
    }
    cout << ans << '\n';
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cout << fixed << setprecision(20);
    run();
    return 0;
}
F1. The Hidden Pair (Easy Version)
/*
 * Author:  heyuhhh
 * Created Time:  2020/6/21 11:36:18
 */
#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;
//head
const int N = 1e3 + 5;
 
int n;
vector <int> G[N];
 
pii query(vector <int>& nodes) {
    if (sz(nodes) == 0) {
        while(true);
    }
    cout << "?" << ' ' << sz(nodes);
    for (auto it : nodes) {
        cout << ' ' << it;
    }
    cout << endl;
    int x, d; cin >> x >> d;
    return MP(x, d);
};
 
void answer(int s, int f) {
    cout << "!" << ' ' << s << ' ' << f << endl;
    string ss;
    cin >> ss;
    if (ss == "Incorrect") {
        while(true);
    }
}
 
vector <int> dep[N];
int deep[N];
int len;
 
void dfs(int u, int fa) {
    deep[u] = deep[fa] + 1;
    dep[deep[u]].push_back(u);
    for (auto v : G[u]) if (v != fa) {
        dfs(v, u);
    }
}
 
pii get(int x) {
    if (sz(dep[x]) == 0) {
        return MP(-1, -1);
    }
    vector <int> nodes;
    for (auto it : dep[x]) {
        nodes.push_back(it);
    }
    return query(nodes);
}
 
void run() {
    cin >> n;
    for (int i = 1; i <= n; i++) {
    	G[i].clear();
	}
    for (int i = 1; i < n; i++) {
        int u, v; cin >> u >> v;
        G[u].push_back(v);
        G[v].push_back(u);
    }
    vector <int> nodes;
    for (int i = 1; i <= n; i++) {
        nodes.push_back(i);
    }
    pii now = query(nodes);
    int rt = now.fi;
    len = now.se;
    for (int i = 0; i <= n; i++) {
        dep[i].clear();
    }
    deep[0] = -1;
    dfs(rt, 0);
    int l = 0, r = n + 1, mid;
    int s, f;
    while (l < r) {
        mid = (l + r) >> 1;
        pii now = get(mid);
        if (now.se == len) {
            l = mid + 1;
            s = now.fi;
        } else {
            r = mid;
        }
    }
    for (int i = 0; i <= n; i++) {
        dep[i].clear();
    }
    dfs(s, 0);
    now = get(len);
    f = now.fi;
    answer(s, f);
}
 
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;
}
F2. The Hidden Pair (Hard Version)
/*
 * Author:  heyuhhh
 * Created Time:  2020/6/21 11:36:18
 */
#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;
//head
const int N = 1e3 + 5;

int n;
vector <int> G[N];

pii query(vector <int>& nodes) {
    if (sz(nodes) == 0) {
        exit(0);
    }
    cout << "?" << ' ' << sz(nodes);
    for (auto it : nodes) {
        cout << ' ' << it;
    }
    cout << endl;
    int x, d; cin >> x >> d;
    return MP(x, d);
};

void answer(int s, int f) {
    cout << "!" << ' ' << s << ' ' << f << endl;
    string ss;
    cin >> ss;
    if (ss == "Incorrect") {
        while(true);
    }
}

vector <int> dep[N];
int deep[N], Max;
int len;

void dfs(int u, int fa) {
    deep[u] = deep[fa] + 1;
    Max = max(deep[u], Max);
    dep[deep[u]].push_back(u);
    for (auto v : G[u]) if (v != fa) {
        dfs(v, u);
    }
}

pii get(int x) {
    if (sz(dep[x]) == 0) {
        return MP(-1, -1);
    }
    vector <int> nodes;
    for (auto it : dep[x]) {
        nodes.push_back(it);
    }
    return query(nodes);
}

void run() {
    cin >> n;
    for (int i = 1; i <= n; i++) {
    	G[i].clear();
	}
    for (int i = 1; i < n; i++) {
        int u, v; cin >> u >> v;
        G[u].push_back(v);
        G[v].push_back(u);
    }
    vector <int> nodes;
    for (int i = 1; i <= n; i++) {
        nodes.push_back(i);
    }
    pii now = query(nodes);
    int rt = now.fi;
    len = now.se;
    for (int i = 0; i <= n; i++) {
        dep[i].clear();
    }
    deep[0] = -1;
    Max = 0;
    int s, f;
    dfs(rt, 0);
    int l = (len + 1) / 2, r = min(len, Max) + 1, mid;
    while (l < r) {
        mid = (l + r) >> 1;
        pii now = get(mid);
        if (now.se == len) {
            l = mid + 1;
            s = now.fi;
        } else {
            r = mid;
        }
    }
    for (int i = 0; i <= n; i++) {
        dep[i].clear();
    }
	dfs(s, 0);
    now = get(len);
    f = now.fi;
    answer(s, f);
}

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;
}
posted @ 2020-06-21 13:53  heyuhhh  阅读(421)  评论(0编辑  收藏  举报