Codeforces Round #598 (Div. 3)

Portal

A. Payment Without Change

Sign.


Code

/*
 * Author:  heyuhhh
 * Created Time:  2019/11/4 21:19:19
 */
#include <bits/stdc++.h>
#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 = 1e5 + 5;
 
int a, b, n, S;
 
void run(){
    cin >> a >> b >> n >> S;
    ll r = S - 1ll * a * n;
    if(r <= 0) r = S % n;
    if((ll)b >= r) pt("YES");
    else pt("NO");
}
 
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. Minimize the Permutation

The meaning of problems:
given a \ (. 1 \) ~ \ (n-\) arranged, for a current position \ (I \) , can be exchanged \ (a_i, A_. 1} + {I \) . But to meet each location can only be exchanged once and up to exchange \ (n-1 \) times.
Lexicographically smallest sequence after the output operation.

Ideas:
directly from the back of each position can be considered greedy constantly.
Each exchange will sweep over a sequence at least once, so complexity is \ (O (^ n-2) \) .


Code

/*
 * Author:  heyuhhh
 * Created Time:  2019/11/4 21:26:23
 */
#include <bits/stdc++.h>
#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 = 105;
 
int n;
int a[N];
bool chk[N];
 
void run(){
    cin >> n;
    int p;
    for(int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    memset(chk, 0, sizeof(chk));
    int cnt = 0;
    while(1) {
        cnt = 0;
        for(int i = n - 1; i >= 1; i--) {
            if(chk[i]) continue;
            if(a[i + 1] < a[i]) {
                swap(a[i + 1], a[i]), ++cnt;
                chk[i] = 1;
            }
        }
        if(!cnt) break;
    }
    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;
}

C. Platforms Jumping

That Italy:
one located in \ (0 \) number position, and now to jump \ (n + 1 \) number position from each jump \ (X \) jumps to \ (. 1 + X \) ~ \ ( D + X \) .
Given \ (m \) the length of the block of wood, to arrange that you are \ (m \) position of the block of wood (without changing the relative order and can not overlap), the output of the last program, or the person can not successfully to \ ( n + 1 \) point.

Ideas:

  • Determine the feasibility of direct greedy judge, this time in the ideal case, this man can jump the farthest far;
  • When considering the specific embodiment, the total length of the set of planks \ (L \) , then there is \ (nL \) voids, the voids can not be too long nor too short.
  • Therefore, according to this gap directly greedy to put planks can be.

Refer to code:


Code

/*
 * Author:  heyuhhh
 * Created Time:  2019/11/4 22:07:56
 */
#include <bits/stdc++.h>
#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 = 1005;
 
int n, m, d;
int c[N];
int a[N], ans[N];
 
void run(){
    cin >> n >> m >> d;
    int tot = 0;
    for(int i = 1; i <= m; i++) cin >> c[i], tot += c[i];
    int s = 0;
    for(int i = 1; i <= m; i++) {
        s += d + c[i] - 1;
    }
    s += d;
    if(s < n + 1) {
        cout << "NO" << '\n';
        return ;
    }
    pt("YES");
    int r = n - tot;
    s = 0;
    for(int i = 1; i <= m; i++) {
        int t = min(r , d - 1);
        r -= t;
        s += t + 1;
        a[s] = i;
        s += c[i] - 1;
    }
    for(int i = 1, j; i <= n; i = j + 1) {
        j = i;
        if(a[i]) {
            for(; j <= i + c[a[i]] - 1; j++) ans[j] = a[i];
            --j;
        }
    }
    for(int i = 1; i <= n; i++) cout << ans[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. Binary String Minimizing

The meaning of problems:
given a \ (01 \) string, now perform one operation of: two consecutive character positions of the exchange.
Up to execute \ (K \) operations, the resulting output string lexicographically smallest.

Thinking:
Each \ (0 \) sequentially greedy move forward on the line, according to the foregoing (1 \) \ number of program output.


Code

/*
 * Author:  heyuhhh
 * Created Time:  2019/11/4 21:43:49
 */
#include <bits/stdc++.h>
#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;
ll k;
char s[N];
int sum[N], has[N];
 
void run(){
    cin >> n >> k;
    cin >> (s + 1);
    int tot = 0;
    for(int i = 0; i <= n; i++) has[i] = 0;
    for(int i = 1; i <= n; i++) {
        sum[i] = sum[i - 1];
        if(s[i] == '1') ++sum[i], ++tot;
        else {
            ++has[sum[i]];
        }
    }
    for(int i = 1; i <= n && k; i++) if(s[i] == '0') {
        if((ll)sum[i] <= k) {
            --has[sum[i]];
            ++has[0];
            k -= sum[i];
        }   
        else {
            --has[sum[i]];
            sum[i] -= k;
            ++has[sum[i]];
            k = 0;
        }
    }
    int cnt = 0;
    while(cnt < tot) {
        while(has[cnt]--) cout << 0;
        cout << 1;
        ++cnt;
    }
    while(has[cnt]--) cout << 0;
    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;
}

E. Yet Another Division Into Teams

Meaning of the questions:
to \ (n \) student group, "value" of each set to the maximum value minus the minimum.
Now requires at least three students in each group, ask "value" Finally all groups and for the minimum number.

Ideas:

  • Apparently the order does not affect the students' answers, you can row a sequence.
  • After that is a simple packet \ (dp: dp [i] \) represents the front \ (i \) individuals to answer the smallest grouping. Enumeration front when transferring \ (j \) transferred.
  • \ (O (n ^ 2) \) obviously can not afford, found in front of \ (dp \) value certainly choose the smallest, so use it to maintain a priority queue.

Note that some of the details, details see the code:


Code

/*
 * Author:  heyuhhh
 * Created Time:  2019/11/5 9:03:50
 */
#include <bits/stdc++.h>
#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;
struct node{
    ll v;
    int id;
    bool operator < (const node &A) const{
        return v > A.v;
    }
}a[N];
 
ll dp[N];
int ans[N], tmp[N], from[N];
 
 
void run(){
    memset(from, 0, sizeof(from));
    memset(tmp, 0, sizeof(tmp));
    for(int i = 1; i <= n; i++) {
        cin >> a[i].v; a[i].id = i;
    }
    a[n + 1] = node{0, 0};
    sort(a + 1, a + n + 1, [&](node A, node B) {
        return A.v < B.v;
    });
    priority_queue <node> q;
    dp[0] = -a[1].v;
    q.push(node{dp[0], 0});
    for(int i = 1; i <= n; i++) {
        vector <node> vec(4);
        while(!q.empty()) {
            node now = q.top(); q.pop();
            vec.push_back(now);
            if(i - now.id >= 3) {
                dp[i] = now.v + a[i].v - a[i + 1].v;
                q.push(node{dp[i], i});
                from[i] = now.id;
                break;
            } 
        }
        for(auto it : vec) q.push(it);
    }
    int p = n;
    while(p) {
        tmp[from[p] + 1] = 1;
        p = from[p];        
    }
    for(int i = 1; i <= n; i++) tmp[i] += tmp[i - 1];
    cout << dp[n] << ' ' << tmp[n] << '\n';
    for(int i = 1; i <= n; i++) ans[a[i].id] = tmp[i];
    for(int i = 1; i <= n; i++) cout << ans[i] << " \n"[i == n];
}
 
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cout << fixed << setprecision(20);
    while(cin >> n) run();
    return 0;
}

F. Equalizing Two Strings

The meaning of problems:
given two strings, the two strings can now be turned over many times the same length while the substring, the same query can eventually become substring.

Ideas:

  • Because you can flip any number of times, then flip for any time, we may be equivalent to more than \ (swap \) operations. Then we consider only the length \ (2 \) flipping operation.
  • First excluded certain number of characters in the two strings are not the same.
  • It can be found: if \ (t \) strings and there are two consecutive identical characters, some legal. Because the continuous exchange of these two strings do not change the appearance.
  • Further reasoning: if \ (t \) a character occurs more than one string \ (1 \) times, it is legitimate.
  • Then only consider the length does not exceed \ (26 \) situations. Now judge \ (s \) string can move to the \ (t \) string, we need only look to reverse both the number of parity can be the difference, if it is even legal, or illegal.

The correctness of the brain make it should be better understood ...


Code

/*
 * Author:  heyuhhh
 * Created Time:  2019/11/4 22:38:32
 */
#include <bits/stdc++.h>
#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; char s[N], t[N];
int cnt[2][26];
int c1[N], c2[N];
 
void run(){
    cin >> n;
    cin >> (s + 1) >> (t + 1);
    for(int i = 0; i < 26; i++) {
        cnt[0][i] = cnt[1][i] = 0;
    }
    for(int i = 1; i <= n; i++) ++cnt[0][s[i] - 'a'];
    for(int i = 1; i <= n; i++) ++cnt[1][t[i] - 'a'];
    for(int i = 0; i < 26; i++) if(cnt[0][i] != cnt[1][i]) {
        pt("NO"); return; 
    }
    for(int i = 0; i < 26; i++) if(cnt[1][i] > 1) {
        pt("YES"); return;
    }
    int d = 0;
    for(int i = 1; i <= n; i++) 
        for(int j = i + 1; j <= n; j++) {
            if(s[j] - '0' < s[i] - '0') ++d;
            if(t[j] - '0' < t[i] - '0') --d;
        }
    if(d & 1) pt("NO");
    else pt("YES");
}
 
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;
}

Guess you like

Origin www.cnblogs.com/heyuhhh/p/11797516.html