Codeforces Round #625 (Div. 2)

传送门

A. Contest for Robots

签到。

Code
/*
 * Author:  heyuhhh
 * Created Time:  2020/3/1 22:23:11
 */
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <iomanip>
#define MP make_pair
#define fi first
#define se second
#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 << '\n'; }
  template<typename T, typename...Args>
  void err(T a, Args...args) { std::cout << a << ' '; err(args...); }
#else
  #define dbg(...)
#endif
void pt() {std::cout << '\n'; }
template<typename T, typename...Args>
void pt(T a, Args...args) {std::cout << a << ' '; pt(args...); }
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
//head
const int N = 100 + 5;

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

void run(){
    for(int i = 1; i <= n; i++) cin >> a[i];
    for(int i = 1; i <= n; i++) cin >> b[i];
    int cnta = 0, cntb = 0;
    for(int i = 1; i <= n; i++) {
        if(a[i] + b[i] == 1) {
            cnta += a[i];
            cntb += b[i];
        }    
    }
    if(cnta == 0) cout << -1 << '\n';
    else {
        int ans = (cntb + cnta) / cnta;
        cout << ans << '\n';
    }
}

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

B. Journey Planning

\(b_i=b_i-i\),最后所有相等的数取出来可以满足条件。
贪心计算就行。

Code
/*
 * Author:  heyuhhh
 * Created Time:  2020/3/1 22:13:33
 */
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <iomanip>
#define MP make_pair
#define fi first
#define se second
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f3f3f3f3f
#define Local
#ifdef Local
  #define dbg(args...) do { cout << #args << " -> "; err(args); } while (0)
  void err() { std::cout << '\n'; }
  template<typename T, typename...Args>
  void err(T a, Args...args) { std::cout << a << ' '; err(args...); }
#else
  #define dbg(...)
#endif
void pt() {std::cout << '\n'; }
template<typename T, typename...Args>
void pt(T a, Args...args) {std::cout << a << ' '; pt(args...); }
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
//head
const int N = 4e5 + 5, M = 1e6 + 5;
 
int n;
int b[M];
ll c[M];
 
void run(){
    for(int i = 1; i <= n; i++) {
        cin >> b[i];
        c[b[i] - i + N] += b[i];
    }
    ll ans = 0;
    for(int i = 1; i < M; i++) {
        ans = max(ans, c[i]);   
    }
    cout << ans << '\n';
}
 
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cout << fixed << setprecision(20);
    while(cin >> n) run();
    return 0;
}

C. Remove Adjacent

贪心。
从大的字符往小的枚举,然后暴力判断删除就行。

Code
/*
 * Author:  heyuhhh
 * Created Time:  2020/3/1 21:20:13
 */
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <iomanip>
#define MP make_pair
#define fi first
#define se second
#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 << '\n'; }
  template<typename T, typename...Args>
  void err(T a, Args...args) { std::cout << a << ' '; err(args...); }
#else
  #define dbg(...)
#endif
void pt() {std::cout << '\n'; }
template<typename T, typename...Args>
void pt(T a, Args...args) {std::cout << a << ' '; pt(args...); }
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
//head
const int N = 100 + 5;

int n;
char s[N];
bool chk[N];

void run(){
    memset(chk, 0, sizeof(chk));
    cin >> (s + 1);
    int ans = 0;
    for(int i = 25; i >= 1; i--) {
        for(int j = 1; j <= n; j++) {
            if(s[j] - 'a' == i) {
                bool ok = false;
                for(int k = j - 1; k >= 1; k--) {
                    if(!chk[k] && s[k] != s[j]) {
                        if(s[k] - 'a' == i - 1) ok = true;
                        break;   
                    }
                }   
                for(int k = j + 1; k <= n; k++) {
                    if(!chk[k] && s[k] != s[j]) {
                        if(s[k] - 'a' == i - 1) ok = true;
                        break;   
                    }
                }
                if(ok) {
                    ++ans;
                    chk[j] = true;   
                }
            }
        }   
    }
    cout << ans << '\n';
}

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

D. Navigation System

贪心。
从终点出发\(bfs\)找到终点\(t\)到每个点的最短距离。
然后根据\(p_i\)的出边贪心计算答案即可。

Code
/*
 * Author:  heyuhhh
 * Created Time:  2020/3/1 21:40:33
 */
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <iomanip>
#define MP make_pair
#define fi first
#define se second
#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 << '\n'; }
  template<typename T, typename...Args>
  void err(T a, Args...args) { std::cout << a << ' '; err(args...); }
#else
  #define dbg(...)
#endif
void pt() {std::cout << '\n'; }
template<typename T, typename...Args>
void pt(T a, Args...args) {std::cout << a << ' '; pt(args...); }
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
//head
const int N = 2e5 + 5;

int n, m;
vector <int> G[N], rG[N];
int k;
int p[N];
int d[N];

void run(){
    for(int i = 1; i <= m; i++) {
        int u, v; cin >> u >> v;
        G[u].push_back(v);
        rG[v].push_back(u);
    }
    cin >> k;
    for(int i = 1; i <= k; i++) cin >> p[i];
    memset(d, INF, sizeof(d)); d[p[k]] = 0;
    queue <int> q; q.push(p[k]); 
    while(!q.empty()) {
        int u = q.front(); q.pop();
        for(auto v : rG[u]) {
            if(d[v] > d[u] + 1) {
                d[v] = d[u] + 1;
                q.push(v);   
            }
        }
    }
    int Min = 0, Max = 0;
    for(int i = 1; i < k; i++) {
        int can = 0;
        for(auto v : G[p[i]]) {
            if(d[v] + 1 == d[p[i]]) ++can;
        }
        if(d[p[i + 1]] + 1 != d[p[i]]) {
            ++Min, ++Max;
        } else if(can > 1) ++Max;
    }
    cout << Min << ' ' << Max << '\n';
}

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

E. World of Darkraft: Battle for Azathoth

题意:
现有\(n\)个武器,\(m\)个护盾,每件装备都有一个属性值,武器为\(a_i\),护盾为\(b_i\),还有购买所需要的花费。
同时还有\(p\)个怪兽,每个怪兽有攻击力、防御力、金钱分别为\(x_i,y_i,z_i\)
现在你作为勇士,要购买一个武器和一个护盾,假设他们的属性值分别为\(a_i,b_i\),花费了\(cost\)去购买,那么能够打败所有\(x_i<a_i,y_i<b_i\)的怪兽,并且获得\(z_i\)的金钱。最后的利润为\(z_i-cost\)
现在要使得最后的利润最大。

思路:

  • 我们将所有的怪兽按照\(x_i\)升序排序。
  • 假设我们现在至少要打败一只怪兽,那么我们枚举打败的怪兽集合中\(x_i\)最大的怪兽。据此可以贪心确定出\(cost_a\)
  • 现在考虑如何确定如何购买一个护盾,使得最后利润最大。
  • 显然我们目前能打败的怪兽集合为\(1\)~\(i\)中的怪兽,并且最后我们一定是打败\(y_i\)连续的一个前缀集合的怪兽。
  • 一个防御力为\(y_i\)的怪兽,我们购买防御值为\([y_i+1,max]\)的护盾,都会产生贡献。
  • 所以我们用一个线段树维护购买每个值的护盾的最大利润即可,最终要实现询问区间最大值和区间加法。
  • 一只怪兽都不打败的情况显然可以直接计算。

可能有些细节,代码如下:

Code
/*
 * Author:  heyuhhh
 * Created Time:  2020/3/1 22:51:58
 */
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <iomanip>
#define MP make_pair
#define fi first
#define se second
#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 << '\n'; }
  template<typename T, typename...Args>
  void err(T a, Args...args) { std::cout << a << ' '; err(args...); }
#else
  #define dbg(...)
#endif
void pt() {std::cout << '\n'; }
template<typename T, typename...Args>
void pt(T a, Args...args) {std::cout << a << ' '; pt(args...); }
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
//head
const int N = 1e6 + 5;

int n, m, p;
pii a[N], b[N];
ll mina[N], minb[N];

struct monster {
    int x, y, z;  
    bool operator < (const monster &A) const {
        return x < A.x;   
    }
}c[N];

ll maxv[N << 2], lz[N << 2];
ll d[N];

void push_up(int o) {
    maxv[o] = max(maxv[o << 1], maxv[o << 1|1]);
}

void tag(int o, int l, int r, ll v) {
    maxv[o] += v;
    lz[o] += v;
}

void push_down(int o, int l, int r) {
    if(lz[o]) {
        int mid = (l + r) >> 1;
        tag(o << 1, l, mid, lz[o]);
        tag(o << 1|1, mid + 1, r, lz[o]);
        lz[o] = 0;
    }   
}

void build(int o, int l, int r) {
    if(l == r) {
        maxv[o] = -d[l];
        return;
    }   
    int mid = (l + r) >> 1;
    build(o << 1, l, mid), build(o << 1|1, mid + 1, r);
    push_up(o);
}

void update(int o, int l, int r, int L, int R, ll v) {
    if(L <= l && r <= R) {
        tag(o, l, r, v);
        return;
    }
    push_down(o, l, r);
    int mid = (l + r) >> 1;
    if(L <= mid) update(o << 1, l, mid, L, R, v);
    if(R > mid) update(o << 1|1, mid + 1, r, L, R, v);
    push_up(o);
}

ll query(int o, int l, int r, int L, int R) {
    if(L <= l && r <= R) {
        return maxv[o];   
    }
    push_down(o, l, r);
    int mid = (l + r) >> 1;
    ll res = -1e18;
    if(L <= mid) res = query(o << 1, l, mid, L, R);
    if(R > mid) res = max(res, query(o << 1|1, mid + 1, r, L, R));
    return res;
}

void run(){
    for(int i = 1; i <= n; i++) cin >> a[i].fi >> a[i].se;
    for(int i = 1; i <= m; i++) cin >> b[i].fi >> b[i].se;
    sort(a + 1, a + n + 1, [&](pii A, pii B) {
        return A.fi < B.fi;
    });
    sort(b + 1, b + m + 1, [&](pii A, pii B) {
        return A.fi < B.fi;
    });
    vector <int> aa;
    for(int i = 1; i <= n; i++) aa.push_back(a[i].fi);
    mina[n + 1] = minb[m + 1] = INF;
    for(int i = n; i >= 1; i--) mina[i] = min(mina[i + 1], (ll)a[i].se);
    for(int i = m; i >= 1; i--) minb[i] = min(minb[i + 1], (ll)b[i].se);
    for(int i = 1; i <= p; i++) {
        cin >> c[i].x >> c[i].y >> c[i].z;   
    }
    sort(c + 1, c + p + 1);
    memset(d, INF, sizeof(d));
    for(int i = 1; i <= m; i++) {
        d[b[i].fi] = min(d[b[i].fi], (ll)b[i].se);
    }
    build(1, 1, N - 1);
    ll ans = -mina[1] - minb[1];
    for(int i = 1; i <= p; i++) {
        update(1, 1, N - 1, c[i].y + 1, N - 1, c[i].z);
        ll t = query(1, 1, N - 1, c[i].y + 1, N - 1);
        int tt = upper_bound(all(aa), c[i].x) - aa.begin() + 1;
        ans = max(ans, t - mina[tt]);
    }
    cout << ans << '\n';
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cout << fixed << setprecision(20);
    while(cin >> n >> m >> p) run();
    return 0;
}
posted @ 2020-03-02 10:28  heyuhhh  阅读(389)  评论(0编辑  收藏  举报