Codeforces Round #642 (Div. 3)

题目传送门

还是视频题解。这次录的心态有点炸,录完了发现没开麦克风。。又得再录一次。
题目不算很难,可能就F需要好好想一下,一开始写了个假的\(dp:dp[i][j][0/1]\)表示当前在\((i,j)\)位置,\(a_{i,j}\)是否发生了变化,维护一个\(pair\),一个是当前最小花费,另一个是当前\(a_{i,j}\)的值。但是写完了发现样例都过不了。但貌似这是个假的dp,不满足最优子结构。。。
但其实F注意一下有个点权值不变,那么这样就很好dp了。
E的话也不难,就是个线性dp。


代码如下:

A. Most Unstable Array
/*
 * Author:  heyuhhh
 * Created Time:  2020/5/15 9:18:11
 */
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <iomanip>
#include <assert.h>
#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;
    if (n == 1) cout << 0 << '\n';
    else if (n == 2) cout << m << '\n';
    else cout << 2 * m << '\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. Two Arrays And Swaps
/*
 * Author:  heyuhhh
 * Created Time:  2020/5/15 9:14:30
 */
#include <bits/stdc++.h>
#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), b(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    for (int i = 0; i < n; i++) {
        cin >> b[i];
    }
    sort(all(a));
    sort(all(b)), reverse(all(b));
    for (int i = 0; i < k; i++) {
        if (a[i] < b[i]) swap(a[i], b[i]);
    }
    cout << accumulate(all(a), 0) << '\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. Board Moves
/*
 * Author:  heyuhhh
 * Created Time:  2020/5/15 9:11:42
 */
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <iomanip>
#include <assert.h>
#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;
    n >>= 1;
    ll res = 1ll * n * (n + 1) * (2 * n + 1) / 6 * 8;
    cout << res << '\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. Constructing the Array
/*
 * Author:  heyuhhh
 * Created Time:  2020/5/15 0:08:23
 */
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <iomanip>
#include <assert.h>
#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 = 2e5 + 5;
 
struct node {
    int l, len;
    bool operator < (const node &A) const {
        if (len != A.len) return len < A.len;
        return l > A.l;
    }
};
 
void run() {
    int n; cin >> n;
    priority_queue <node> q;
    q.push(node{1, n});
    int T = 0;
    vector <int> a(n + 1);
    while (!q.empty()) {
        ++T;
        node cur = q.top(); q.pop();
        int l = cur.l, r = cur.l + cur.len - 1;
        int m = (l + r) / 2;
        a[m] = T;
        if (cur.len & 1) {
            if (m > l) q.push(node{l, cur.len / 2});
        } else {
            if (m > l) q.push(node{l, cur.len / 2 - 1});
        }
        if (m < r) q.push(node{l + (cur.len + 1) / 2, cur.len / 2});
    }
    for (int i = 1; i <= n; i++) {
        cout << a[i] << " \n"[i == 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;
}
E. K-periodic Garland
/*
 * Author:  heyuhhh
 * Created Time:  2020/5/14 23:57:45
 */
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <iomanip>
#include <assert.h>
#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
 
void run() {
    int n, k; cin >> n >> k;
    vector <int> dp(n + 1), sum(n + 1);
    string s; cin >> s;
    for (int i = 0; i < n; i++) {
        sum[i + 1] = sum[i] + (s[i] == '1');
    }
    if (sum[n] == 0) {
        cout << 0 << '\n';
        return;
    }
    
    int ans = INF;
    for (int i = 0; i < n; i++) {
        int res = sum[i] + sum[n] - sum[i + 1];
        res += (s[i] == '0');
        ans = min(ans, res);
    }
    
    auto calc = [&] (int l, int r) {
        return sum[r] - sum[l - 1];
    };
    dp[0] = 0;
    for (int i = 1; i <= n; i++) {
        dp[i] = sum[i - 1] + (s[i - 1] == '0');
        if (i >= k) {
            dp[i] = min(dp[i], dp[i - k] + calc(i - k + 1, i - 1) + (s[i - 1] == '0'));
        } 
    }
    
    for (int i = 1; i <= n; i++) {
        ans = min(ans, dp[i] + sum[n] - sum[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;
}
F. Decreasing Heights
/*
 * Author:  heyuhhh
 * Created Time:  2020/5/14 22:53:49
 */
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <iomanip>
#include <assert.h>
#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 0x3f3f3f3f3f3f3f3f
//#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 = 100 + 5;
 
int n, m;
ll a[N][N], dp[N][N];
 
void run() {
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            cin >> a[i][j];
        }
    }
    auto calc = [&](int sx, int sy, int ex, int ey, int op) {
        int x, y;
        if (op == 0) x = ex, y = ey;
        else x = sx, y = sy;
        ll res = 0;
        for (int i = sx; i <= ex; i++) {
            for (int j = sy; j <= ey; j++) {
                int d = abs(x - i) + abs(y - j);
                if (op == 0) {
                    if (a[i][j] >= a[x][y] - d) {
                        dp[i][j] = min(dp[i - 1][j], dp[i][j - 1]) + a[i][j] - a[x][y] + d;
                    }
                } else {
                    if (a[i][j] >= a[x][y] + d) {
                        dp[i][j] = min(dp[i - 1][j], dp[i][j - 1]) + a[i][j] - a[x][y] - d;
                    }
                }
            }
        }
        return 0;
    };
    ll res = INF;
    for (int x = 1; x <= n; x++) {
        for (int y = 1; y <= m; y++) {
            for (int i = 0; i <= n; i++) {
                for (int j = 0; j <= m; j++) {
                    dp[i][j] = INF;
                }
            }
            dp[1][0] = 0;
            calc(1, 1, x, y, 0);
            calc(x, y, n, m, 1);
            res = min(res, dp[n][m]);
        }
    }
    cout << res << '\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;
}
posted @ 2020-05-15 12:16  heyuhhh  阅读(229)  评论(0编辑  收藏  举报