Codeforces Round #645 (Div. 2)

题目传送门

还是视频题解
昨晚比赛没打,就看了看题,ABD基本就是贪心+模拟,C感受一下算一下公式就行,E的话有点像交互,分情况讨论一下,\(x\leq 0\)的情况最简单,枚举一下后缀就行;\(x>0\)的时候利用到了倍增的思想,就刚好符合题目给出的条件。感觉这种将题目条件运用到极致的题目挺像交互了2333。F细节是真的多,各种判断,一不小心就卡半天。。不过思路还是有借鉴性,考虑逆操作和前缀和的一些性质,还有最后\(n=2\)时操作类似于欧几里得算法这里也很巧妙。。

补充一下E题思路:
后面\(\lfloor\frac{n}{2}\rfloor\)个数,假设都为\(x\)

考虑\(x\leq 0\)的情况:注意到最后的答案\(k>\lfloor\frac{n}{2}\rfloor\),否则\([n-k+1,n]\)这段区间和非正,不满足条件。那么可以直接枚举所有区间最后一个左端点的位置\(i\),当前会新产生\([i,n]\)这段区间,显然其区间和可以直接计算;对于之前的区间,区间右端点都会左移一位,也就是之前所有的区间都会减少一个相同的数\(x\)。我们要考察所有区间和的最小值,那么只需对所有区间维护一个最小值,当区间端点移动时,最小值减去\(x\)即可,之后再与当前新产生的区间取\(min\)即可。

\(x>0\)时,答案\(k\)没有明确的限制,我们可以发现若存在一个\(k>\lfloor\frac{n}{2}\rfloor\)合法,那么直接让\(k=n\)也是合法的;接下来就考虑\(k\leq \lfloor\frac{n}{2}\rfloor\)的情况,此时假设存在一个\(k\)合法,容易发现\(2k\)也是合法的答案。也就是说我们能够将\(k\)倍增至大于\(\lfloor\frac{n}{2}\rfloor\)的某个位置。归纳一下\(x>0\)直接取\(k=n\)即可。


代码如下:

A. Park Lighting
/*
 * Author:  heyuhhh
 * Created Time:  2020/5/27 9:52:38
 */
#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, m; cin >> n >> m;
    int ans = n * (m / 2);
    if (m & 1) {
        ans += n / 2;
        if (n & 1) ++ans;
    }
    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. Maria Breaks the Self-isolation
/*
 * Author:  heyuhhh
 * Created Time:  2020/5/27 9:35:36
 */
#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;
    vector <int> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    sort(all(a));
    int r = 1;
    for (int i = 0, j; i < n; i = j + 1) {
        int fur = -1;
        for (j = i; j < n; j++) {
            if (j - i + r >= a[j]) fur = j;
        }
        if (fur == -1) break;
        j = fur;
        r += j - i + 1;
    }
    cout << r << '\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. Celex Update
/*
 * Author:  heyuhhh
 * Created Time:  2020/5/27 9:25:12
 */
#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 sx, sy, ex, ey;
    cin >> sx >> sy >> ex >> ey;
    int n = ex - sx + 1, m = ey - sy + 1;
    if (n > m) swap(n, m);
    ll ans = 1ll * n * (n - 1);
    ll r = n + m - 3;
    r -= 2 * (n - 1);
    ans += r * (n - 1);
    ++ans;
    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;
}
D. The Best Vacation
/*
 * Author:  heyuhhh
 * Created Time:  2020/5/26 23:06:52
 */
#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() {
    ll n, x; cin >> n >> x;
    vector <ll> a(n << 1);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        a[i + n] = a[i];
    }
    a.pb(a[0]);
    auto calc = [&] (int l, int r) {
        return 1ll * (r - l + 1) * (l + r) / 2;  
    };
    vector <ll> sum(n << 1), sum2(n << 1);
    for (int i = 0; i < n << 1; i++) {
        if (i == 0) sum[i] = a[i];
        else sum[i] = sum[i - 1] + a[i];
    }
    for (int i = 0; i < n << 1; i++) {
        if (i == 0) sum2[i] = calc(1, a[i]);
        else sum2[i] = sum2[i - 1] + calc(1, a[i]);
    }
    auto chk = [&] (int i, int j) {
        return sum[j] - (i == 0 ? 0 : sum[i - 1]);
    };
    ll ans = 0;
    for (int i = 0; i < n; i++) {
        int l = i + 1, r = 2 * n, mid;
        while (l < r) {
            mid = (l + r) >> 1;
            if (chk(i, mid) <= x) l = mid + 1;
            else r = mid;
        }
        int j = l - 1;
        ll s = x - chk(i, j);
        if (s > 0) {
            ll k = min(a[i] - 1, a[j + 1] - s);
            ans = max(ans, calc(1 + k, a[i]) + calc(1, s + k) + sum2[j] - sum2[i]);
        } else if (s == 0) {
            ans = max(ans, sum2[j] - (i == 0 ? 0 : sum2[i - 1]));   
        } else {
            ans = max(ans, calc(a[i] - x + 1, a[i]));
        }
    }
    cout << ans << '\n';
}
 
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cout << fixed << setprecision(20);
    run();
    return 0;
}
E. Are You Fired?
/*
 * Author:  heyuhhh
 * Created Time:  2020/5/26 23:50:38
 */
#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 = 5e5 + 5;
 
int n;
int a[N];
ll sum[N];
 
void run() {
    cin >> n;
    int k = (n + 1) / 2;
    for (int i = 1; i <= k; i++) {
        cin >> a[i];
        sum[i] = sum[i - 1] + a[i];
    }
    int x; cin >> x;
    auto calc = [&] (ll t) {
        return sum[k] - sum[t - 1];  
    };
    ll Min = 1e18;
    int ans = -1;
    for (int i = 1; i <= k; i++) {
        ll res = calc(i) + 1ll * (n / 2) * x;
        if (Min == 1e18) {
            Min = res;
        } else {
            Min -= x;
            Min = min(Min, res);           
        }   
        if (Min > 0) ans = n - i + 1;
    }
    cout << ans << '\n';
}
 
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cout << fixed << setprecision(20);
    run();
    return 0;
}
F. Tasty Cookie
/*
 * Author:  heyuhhh
 * Created Time:  2020/5/27 15:17:12
 */
#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;
 
bool operator == (vector <ll>& A, vector <ll>& B) {
    for (int i = 0; i < sz(A); i++) {
        if (A[i] != B[i]) return false;
    }
    return true;   
}
 
vector <ll> rev(vector <ll>& A) {
    vector <ll> B = A;
    reverse(all(B));
    return B; 
}
 
void D(vector <ll>& A) {
    int n = sz(A);
    for (int i = n - 1; i > 0; i--) {
        A[i] -= A[i - 1];
    }
}
 
void out(ll cntp, ll cntr, vector <char>& ans) {
    reverse(all(ans));
    if (cntp > 2e5) {
        cout << "BIG" << '\n';
        cout << cntp << '\n';
    } else {
        cout << "SMALL" << '\n';
        cout << sz(ans) << '\n';
        for (auto it : ans) cout << it;
        cout << '\n';
    }
}    
        
void run() {
    int n; cin >> n;
    vector <ll> a(n), b(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    for (int i = 0; i < n; i++) {
        cin >> b[i];
    }
    if (n == 1) {
        if (a[0] == b[0]) {
            cout << "SMALL" << '\n';
            cout << 0 << '\n' << '\n';
        } else {
            cout << "IMPOSSIBLE" << '\n';
        }
        return;
    }
    ll cntr = 0, cntp = 0;
    vector <char> ans;
    bool BIG = false, f = false;
    if (n > 2) {
        while (!(a == b || a == rev(b))) {
            for (int i = 1; i < n; i++) {
                if (b[i] <= b[i - 1]) {
                    b = rev(b), ++cntr;
                    ans.push_back('R'); 
                    break;
                }
            }
            D(b), ++cntp;
            if (cntp > 2e5) BIG = true;
            if (!BIG) ans.push_back('P');
            for (int i = 0; i < n; i++) {
                if (b[i] < 0) {
                    cout << "IMPOSSIBLE" << '\n';
                    return;
                }
            }
        }
        if (a == rev(b)) f = true;
    } else {
        while (1) {
            if (b[0] == a[1]) {
                swap(a[0], a[1]);
                f = true;
            }
            if (b[0] == a[0]) {
                if (b[1] < a[1]) {
                    cout << "IMPOSSIBLE" << '\n';
                    return;
                }
                ll r = (b[1] - a[1]) % b[0];
                if (r == 0) {
                    ll now = ((b[1] - a[1]) / b[0]);
                    cntp += now;
                    if (cntp > 2e5) BIG = true;
                    if (!BIG) for (int i = 0; i < now; i++) {
                        ans.push_back('P');
                    }
                } else {
                    cout << "IMPOSSIBLE" << '\n';   
                    return;
                }
                break;
            }
            if (b[0] < a[0]) {
                cout << "IMPOSSIBLE" << '\n';   
                return; 
            }
            ll& x = b[0], &y = b[1];
            ll now = (y - y % x) / x;
            y %= x;
            cntp += now;
            if (cntp > 2e5) BIG = true;
            if (!BIG) for (int i = 0; i < now; i++) {
                ans.push_back('P');
            }
            swap(x, y), ++cntr;
            ans.push_back('R');
        }
    }
    if (f) {
        ++cntr;
        ans.push_back('R');
    }
    out(cntp, cntr, ans);
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cout << fixed << setprecision(20);
    run();
    return 0;
}
posted @ 2020-05-27 13:25  heyuhhh  阅读(494)  评论(0编辑  收藏  举报