Codeforces Round #654 (Div. 2)

Codeforces Round #684 (Div. 2)

 

A - Buy the String

贪心模拟

#include <stdio.h>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map> 
#include <stack>
#include <sstream>
#include <set>
// #pragma GCC optimize(2)

//#define int long long
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define rush() int T;scanf("%d",&T);for(int Ti=1;Ti<=T;++Ti)
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define mm(i,v) memset(i,v,sizeof i);
#define mp(a, b) make_pair(a, b)
#define pi acos(-1)
#define fi first
#define se second

using namespace std;
typedef long long ll;
typedef double db;
typedef pair<int, int > PII;
priority_queue< PII, vector<PII>, greater<PII> > que;
stringstream ssin; //  ssin << string   while ( ssin >> int)
const ll LINF = 0x7fffffffffffffffll;

const int N = 4e5 + 5, M = 4e5 + 5, mod = 1e9 + 7, INF = 0x3f3f3f3f;
int _, c0, c1, h, n;
char s[N];

inline ll read() {
    char c=getchar();ll x=0,f=1;
    while(c<'0'||c>'9'){if(c=='-')f=-1; c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0'; c=getchar();}
    return x*f;
}


int main()
{
    #ifndef ONLINE_JUDGE
        freopen("in.txt","r",stdin);
        freopen("out.txt","w",stdout);
    #endif
    
    _ = read();
    while (_--) {
        n = read(); c0 = read(); c1 = read(); h = read();
        scanf("%s", s + 1);
        int ans = 0;
        for (int i = 1; i <= n; ++i) {
            if (s[i] == '0')
                ans += min(c0, c1 + h);
            else
                ans += min(c1, c0 + h);
        }
        cout << ans << '\n';
    }

    // #ifndef ONLINE_JUDGE
    //     system("pause");
    // #endif
}
View Code

 

B - Sum of Medians

发现一定是从最后开始取

#include <stdio.h>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map> 
#include <stack>
#include <sstream>
#include <set>
// #pragma GCC optimize(2)

//#define int long long
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define rush() int T;scanf("%d",&T);for(int Ti=1;Ti<=T;++Ti)
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define mm(i,v) memset(i,v,sizeof i);
#define mp(a, b) make_pair(a, b)
#define pi acos(-1)
#define fi first
#define se second

using namespace std;
typedef long long ll;
typedef double db;
typedef pair<int, int > PII;
priority_queue< PII, vector<PII>, greater<PII> > que;
stringstream ssin; //  ssin << string   while ( ssin >> int)
const ll LINF = 0x7fffffffffffffffll;

const int N = 1e6 + 5, M = 4e5 + 5, mod = 1e9 + 7, INF = 0x3f3f3f3f;
int _, n, k;
int a[N];

inline ll read() {
    char c=getchar();ll x=0,f=1;
    while(c<'0'||c>'9'){if(c=='-')f=-1; c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0'; c=getchar();}
    return x*f;
}


int main()
{
    #ifndef ONLINE_JUDGE
        freopen("in.txt","r",stdin);
        freopen("out.txt","w",stdout);
    #endif
    _ = read();
    while (_--) {
        n = read(); k = read();
        rep(i, 1, n * k) a[i] = read();
        
        ll sum = 0;
        ll now = n * k - n / 2, cnt = 1;
        while (cnt <= k) {
            sum += a[now];
            now -= n / 2 + 1;
            cnt++;
        }

        printf("%lld\n",sum);
    }

    // #ifndef ONLINE_JUDGE
    //     system("pause");
    // #endif
}
View Code

 

C1 - Binary Table (Easy Version)

简单版本的话对于每个1都可以通过三次操作让其变成0

#include <stdio.h>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map> 
#include <stack>
#include <sstream>
#include <set>
// #pragma GCC optimize(2)

//#define int long long
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define rush() int T;scanf("%d",&T);for(int Ti=1;Ti<=T;++Ti)
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define mm(i,v) memset(i,v,sizeof i);
#define mp(a, b) make_pair(a, b)
#define pi acos(-1)
#define fi first
#define se second

using namespace std;
typedef long long ll;
typedef double db;
typedef pair<int, int > PII;
priority_queue< PII, vector<PII>, greater<PII> > que;
stringstream ssin; //  ssin << string   while ( ssin >> int)
const ll LINF = 0x7fffffffffffffffll;

const int N = 210, M = 4e5 + 5, mod = 1e9 + 7, INF = 0x3f3f3f3f;
int _, n, m, cnt;
char s[N][N];

struct node {
    int x1, y1, x2, y2, x3, y3;
}e[M];

inline ll read() {
    char c=getchar();ll x=0,f=1;
    while(c<'0'||c>'9'){if(c=='-')f=-1; c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0'; c=getchar();}
    return x*f;
}


int main()
{
    #ifndef ONLINE_JUDGE
        freopen("in.txt","r",stdin);
        freopen("out.txt","w",stdout);
    #endif
    
    _ = read();
    while (_--) {

    cnt = 0;

    n = read();
    m = read();
    rep(i, 1, n) scanf("%s", s[i] + 1);

    rep(i, 1, n) {
        rep(j, 1, m) {
            if (s[i][j] == '0') continue;

            if (i == 1 && j == 1) {
                e[++cnt] = {1, 1, 1, 2, 2, 1};
                e[++cnt] = {1, 1, 1, 2, 2, 2};
                e[++cnt] = {1, 1, 2, 1, 2, 2};
                continue;
            }
            if (i == n && j == m) {
                e[++cnt] = {n, m, n, m - 1, n - 1, m};
                e[++cnt] = {n, m, n, m - 1, n - 1, m - 1};
                e[++cnt] = {n, m, n - 1, m - 1, n - 1, m};
                continue;
            }

            if (i + 1 <= n && j + 1 <= m) {
                e[++cnt] = {i,j,i+1,j,i+1,j+1};
                e[++cnt] = {i,j,i,j+1,i+1,j+1};
                e[++cnt] = {i,j,i+1,j,i,j+1};
            } else if (i > 1 && j > 1) {
                e[++cnt] = {i,j,i,j-1,i-1,j-1};
                e[++cnt] = {i,j,i,j-1,i-1,j};
                e[++cnt] = {i,j,i-1,j-1,i-1,j};
            } else if (i + 1 <= n && j - 1 >= 1) {
                e[++cnt] = {i,j,i+1,j-1,i,j-1};
                e[++cnt] = {i,j,i+1,j-1,i+1,j};
                e[++cnt] = {i,j,i+1,j,i,j-1};
            } else {
                e[++cnt] = {i,j,i-1,j,i,j+1};
                e[++cnt] = {i,j,i-1,j,i-1,j+1};
                e[++cnt] = {i,j,i,j+1,i-1,j+1};
            }

        }
    }

    cout << cnt << '\n';
    rep(i, 1, cnt) {
        cout << e[i].x1 << " " << e[i].y1 << " " << e[i].x2 << " " << e[i].y2 << " " << e[i].x3 << " " << e[i].y3 << '\n';
    }

    }


    // #ifndef ONLINE_JUDGE
    //     system("pause");
    // #endif
}
View Code

 

C2 - Binary Table (Hard Version)

hard版本可以发现对于每一个2*2的矩形我们都可以在四步之内将其全置成0

那么我们只需判断行和列是否为奇数,若为奇数预处理掉多出来的一行即可

#include <stdio.h>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map> 
#include <stack>
#include <sstream>
#include <set>
// #pragma GCC optimize(2)

//#define int long long
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define rush() int T;scanf("%d",&T);for(int Ti=1;Ti<=T;++Ti)
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define mm(i,v) memset(i,v,sizeof i);
#define mp(a, b) make_pair(a, b)
#define pi acos(-1)
#define fi first
#define se second

using namespace std;
typedef long long ll;
typedef double db;
typedef pair<int, int > PII;
priority_queue< PII, vector<PII>, greater<PII> > que;
stringstream ssin; //  ssin << string   while ( ssin >> int)
const ll LINF = 0x7fffffffffffffffll;

const int N = 210, M = 4e5 + 5, mod = 1e9 + 7, INF = 0x3f3f3f3f;
int _, n, m, cnt;
char s[N][N];

inline ll read() {
    char c=getchar();ll x=0,f=1;
    while(c<'0'||c>'9'){if(c=='-')f=-1; c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0'; c=getchar();}
    return x*f;
}

struct node {
    int x1, y1, x2, y2, x3, y3;
}e[M];

void f(int x, int y) {
    if (s[x][y] == '1') s[x][y] = '0';
    else s[x][y] = '1';
}
void f1(PII x) {
    f(x.fi, x.se);
    f(x.fi - 1, x.se);
    f(x.fi, x.se + 1);
    e[++cnt] = {x.fi, x.se, x.fi - 1, x.se, x.fi, x.se + 1};
}
void f2(PII x) {
    f(x.fi, x.se);
    f(x.fi + 1, x.se);
    f(x.fi, x.se + 1);
    e[++cnt] = {x.fi, x.se, x.fi + 1, x.se, x.fi, x.se + 1};
}
void f3(PII x) {
    f(x.fi, x.se);
    f(x.fi + 1, x.se);
    f(x.fi, x.se - 1);
    e[++cnt] = {x.fi, x.se, x.fi + 1, x.se, x.fi, x.se - 1};
}
void f4(PII x) {
    f(x.fi, x.se);
    f(x.fi - 1, x.se);
    f(x.fi, x.se - 1);
    e[++cnt] = {x.fi, x.se, x.fi - 1, x.se, x.fi, x.se - 1};
}

void f1(int x, int y) {f1({x, y});}
void f2(int x, int y) {f2({x, y});}
void f3(int x, int y) {f3({x, y});}
void f4(int x, int y) {f4({x, y});}

int get(int x, int y) {
    int sum = 0;
    sum += s[x][y] == '1';
    sum += s[x][y + 1] == '1';
    sum += s[x + 1][y] == '1';
    sum += s[x + 1][y + 1] == '1';
    return sum;
}

void solve(int x, int y) {
    if (get(x, y) == 1) {
        if (s[x][y] == '1') {
            f2(x, y);
            f3(x, y + 1);
            f1(x + 1, y);
            return ;
        }
        if (s[x + 1][y] == '1') {
            f1(x + 1, y);
            f2(x, y);
            f4(x + 1, y + 1);
            return ;
        }
        if (s[x][y + 1] == '1') {
            f3(x, y + 1);
            f2(x, y);
            f4(x + 1, y + 1);
            return ;
        }
        if (s[x + 1][y + 1] == '1') {
            f4(x + 1, y + 1);
            f3(x, y + 1);
            f1(x + 1, y);
            return ;
        }
    } else if (get(x, y) == 2) {
        
        int ans = 0;
        if (s[x][y] == '1' && s[x + 1][y + 1] == '1') {
            // puts("1!");
            f2(x, y);
            f4(x + 1, y + 1);
            return ;
        }
        if (s[x][y + 1] == '1' && s[x + 1][y] == '1') {
            // puts("2!");
            f1(x + 1, y);
            f3(x, y + 1);
            return ;
        }
        if (s[x][y] == '1' && s[x][y + 1] == '1') {  // 1100
            // puts("3!");
            f2(x, y);
            f1(x + 1, y);
            f2(x, y);
            f4(x + 1, y + 1);
            return ;
        }
        if (s[x + 1][y] == '1' && s[x + 1][y + 1] == '1') {  // 0011
            // puts("4!");
            f1(x + 1, y);
            f2(x, y);
            f3(x, y + 1);
            f1(x + 1, y);
            return ;
        }
        if (s[x][y] == '1' && s[x + 1][y] == '1') {
            // puts("5!");
            f2(x, y);
            f3(x, y + 1);
            f2(x, y);
            f4(x + 1, y + 1);
            return ;
        }
        if (s[x][y + 1] == '1' && s[x + 1][y + 1] == '1') {
            // puts("6!");
            f3(x, y + 1);
            f2(x, y);
            f3(x, y + 1);
            f1(x + 1, y);
            return ;
        }
    } else if (get(x, y) == 3) {
        if (s[x][y] == '0') e[++cnt] = {x, y + 1, x + 1, y, x + 1, y + 1};
        else if (s[x][y + 1] == '0') e[++cnt] = {x, y, x + 1, y, x + 1, y + 1};
        else if (s[x + 1][y] == '0') e[++cnt] = {x + 1, y + 1, x, y, x, y + 1};
        else if (s[x + 1][y + 1] == '0') e[++cnt] = {x, y, x + 1, y, x, y + 1};
    } else if (get(x, y) == 4) {
        f4(x + 1, y + 1);
        f2(x, y);
        f3(x, y + 1);
        f1(x + 1, y);
    }
    return;
}

int main()
{
    #ifndef ONLINE_JUDGE
        freopen("in.txt","r",stdin);
        freopen("out.txt","w",stdout);
    #endif
    
    _ = read();
    while (_--) {
        cnt = 0;
        n = read(); m = read();
        rep(i, 1, n) {
            scanf("%s", s[i] + 1);
        }
        if (n & 1) {
            rep(i, 1, m) {
                if (s[1][i] == '0') continue;
                if (i != m) {
                    f2({1, i});
                } else {
                    f4({2, i});
                }
            }
        }
        if (m & 1) {
            rep(i, 1, n) {
                if (s[i][1] == '0') continue;
                if (i != n) {
                    f1({i + 1, 1});
                } else {
                    f4({n, 2});
                }
            }
        }
        int xx = 1, yy = 1; 
        if (n & 1) xx = 2;
        if (m & 1) yy = 2;

        // for (int i = 1; i <= n; ++i) {
        //     for (int j = 1; j <= n; ++j) cout << s[i][j];
        //     puts("");
        // }
        
        for (int i = xx; i <= n; i += 2) {
            for (int j = yy; j <= m; j += 2) {
                // cout << i << "---" << j << '\n';
                solve(i, j);
            }
        }

        cout << cnt << '\n';
        rep(i, 1, cnt) {
            printf("%d %d %d %d %d %d\n", e[i].x1, e[i].y1, e[i].x2, e[i].y2, e[i].x3, e[i].y3);
        }

        // for (int i = 1; i <= n; ++i) {
        //     for (int j = 1; j <= n; ++j) cout << s[i][j];
        //     puts("");
        // }
    }

    // #ifndef ONLINE_JUDGE
    //     system("pause");
    // #endif
}
View Code

 

D-Graph Subset Problem
不会写好难想不到

 

E - Greedy Shopping

线段树搞一搞

维护一个区间最大、区间最小、区间和

#include <stdio.h>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map> 
#include <stack>
#include <sstream>
#include <set>
// #pragma GCC optimize(2)

//#define int long long
#define ls u<<1
#define rs u<<1|1
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define rush() int T;scanf("%d",&T);for(int Ti=1;Ti<=T;++Ti)
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define mm(i,v) memset(i,v,sizeof i);
#define mp(a, b) make_pair(a, b)
#define pi acos(-1)
#define fi first
#define se second

using namespace std;
typedef long long ll;
typedef double db;
typedef pair<int, int > PII;
priority_queue< PII, vector<PII>, greater<PII> > que;
stringstream ssin; //  ssin << string   while ( ssin >> int)
const ll LINF = 0x7fffffffffffffffll;

const ll N = 2e5 + 5, M = 4e5 + 5, mod = 1e9 + 7, INF = 0x3f3f3f3f;
ll n, q;
ll a[N];

struct node {
    ll l, r;
    ll maxx, minn, lazy, sum;
}tr[N << 2];

inline ll read() {
    char c=getchar();ll x=0,f=1;
    while(c<'0'||c>'9'){if(c=='-')f=-1; c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0'; c=getchar();}
    return x*f;
}

void pushup(int u) {
    tr[u].maxx = max(tr[ls].maxx, tr[rs].maxx);
    tr[u].minn = min(tr[ls].minn, tr[rs].minn);
    tr[u].sum = tr[ls].sum + tr[rs].sum;
}

void change(int u, ll num) {
    tr[u].sum = (tr[u].r - tr[u].l + 1) * num;
    tr[u].minn = tr[u].maxx = num;
    tr[u].lazy = num;
}

void pushdown(int u) {
    change(ls, tr[u].lazy);
    change(rs, tr[u].lazy);
    tr[u].lazy = 0;
}

void build(int u, int l, int r) {
    tr[u].l = l; tr[u].r = r; tr[u].lazy = 0;
    if (l == r) {
        tr[u].minn = a[l];
        tr[u].maxx = a[l];
        tr[u].sum = a[l];
        return ;
    }
    int mid = l + r >> 1;
    build(u << 1, l, mid);
    build(u << 1 | 1, mid + 1, r);
    pushup(u);
}

void upd(int u, int l, int r, ll val) {
    if (tr[u].minn > val) {
        return;
    }
    if (l <= tr[u].l && tr[u].r <= r) {
        if (tr[u].maxx <= val) {
            change(u, val);
            return; 
        }
    }
    if (tr[u].lazy)
        pushdown(u);
    int mid = tr[u].l + tr[u].r >> 1;
    if (mid >= l) upd(ls, l, r, val);
    if (mid + 1 <= r) upd(rs, l, r, val);
    pushup(u);
}

int qry(int u, int l, int r, ll &val) {
    if (tr[u].minn > val) return 0;

    if (tr[u].l >= l && tr[u].r <= r) {
        if (tr[u].sum <= val) {
            val -= tr[u].sum;
            return tr[u].r - tr[u].l + 1;
        }
    }
    if (tr[u].lazy)
        pushdown(u);
    int mid = tr[u].l + tr[u].r >> 1;
    int ans1 = 0, ans2 = 0;
    if (mid >= l) ans1 = qry(ls, l, r, val);
    if (mid + 1 <= r) ans2 = qry(rs, l, r, val);
    return ans1 + ans2;
}

int main()
{
    #ifndef ONLINE_JUDGE
        freopen("in.txt","r",stdin);
        freopen("out.txt","w",stdout);
    #endif
    
    n = read(); q = read();
    rep(i, 1, n) a[i] = read();

    build(1, 1, n);

    while (q--) {
        ll op, x, y;
        op = read(); x = read(); y = read();
        if (op == 1) {
            upd(1, 1, x, y);
        } else if (op == 2) {
            cout << qry(1, x, n, y) << '\n';
        }
    }

    // #ifndef ONLINE_JUDGE
    //     system("pause");
    // #endif
}
View Code

 

 

 
posted @ 2020-11-19 10:56  violet72  阅读(42)  评论(0编辑  收藏  举报