Codeforces Round #419 (Div. 1) (ABCDE)

1. 815A Karen and Game

Effect: $ nm $ given matrix, each row or column select Save all $ 1 $, so that the minimum required number of full-matrix $ 0 $

Greedy, $ n> m $ when each take an otherwise, it is a line

#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <cstring>
#include <bitset>
#include <functional>
#include <random>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(a) ({REP(__i,1,n) cout<<a[__i]<<',';hr;})
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;}
//head



const int N = 1e6+50;
int n,m,a[111][111];
int op[N], p[N], cnt;

void add(int tp, int x) {
    ++cnt;
    op[cnt] = tp, p[cnt] = x;
    if (tp==1) REP(i,1,n) --a[i][x];
    else REP(i,1,m) --a[x][i];
}
int main() {
    scanf("%d%d",&n,&m);
    int sum = 0;
    REP(i,1,n) REP(j,1,m) scanf("%d",a[i]+j),sum+=a[i][j];
    while (sum) {
        if (n>m) {
            int ok = 0;
            REP(i,1,m) {
                int s = 1e9;
                REP(j,1,n) s=min(s,a[j][i]);
                if (s) add(1,i),sum-=n,ok=1;
            }
            if (!ok) {
                REP(i,1,n) {
                    int s = 1e9;
                    REP(j,1,m) s=min(s,a[i][j]);
                    if (s) add(2,i),sum-=m,ok=1;
                }
                if (!ok) return puts("-1"),0;
            }
        }
        else {
            int ok = 0;
            REP(i,1,n) {
                int s = 1e9;
                REP(j,1,m) s=min(s,a[i][j]);
                if (s) add(2,i),ok=1,sum-=m;
            }
            if (!ok) {
                REP(i,1,m) {
                    int s = 1e9;
                    REP(j,1,n) s=min(s,a[j][i]);
                    if (s) add(1,i),ok=1,sum-=n;
                }
                if (!ok) return puts("-1"),0;
            }
        }
    }
    printf("%d\n",cnt);
    REP(i,1,cnt) printf("%s %d\n",op[i]==1?"col":"row",p[i]);
}
View Code

 

 

2. 815B Karen and Test

Effect: number table has a triangular, a first given row, each of the two lower lines adjacent above and the number or the difference, seeking the lowest number is.

Hit the table to count the contribution of each number, you can find under the law

#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <cstring>
#include <bitset>
#include <functional>
#include <random>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(a) ({REP(__i,1,n) cout<<a[__i]<<',';hr;})
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;}
//head



const int N = 200;
vector<int> f[N][N];
int n, a[N];
vector<int> add(vector<int> a, vector<int> b) {
    REP(i,1,n) a[i]+=b[i];
    return a;
}
vector<int> sub(vector<int> a, vector<int> b) {
    REP(i,1,n) a[i]-=b[i];
    return a;
}
int main() {
    scanf("%d",&n);
    REP(i,1,n) REP(j,1,n) f[i][j].resize(n+1);
    REP(i,1,n) f[1][i][i] = 1;
    int cur = 1;
    REP(i,2,n) {
        REP(j,1,n-i+1) {
            if (cur) f[i][j]=add(f[i-1][j],f[i-1][j+1]);
            else f[i][j]=sub(f[i-1][j],f[i-1][j+1]);
            cur ^= 1;
        }
    }
    REP(i,1,n) printf("%d,",f[n][1][i]);hr;
}
Play table code 
#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <cstring>
#include <bitset>
#include <functional>
#include <random>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(a) ({REP(__i,1,n) cout<<a[__i]<<',';hr;})
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;}
//head



const int N = 1e6+50;
int n, a[N], fac[N], ifac[N];
int f[N], g[N];
ll C(int n, int m) {
    if (n<m) throw;
    return (ll)fac[n]*ifac[m]%P*ifac[n-m]%P;
}
int main() {
    fac[0]=1;
    REP(i,1,N-1) fac[i]=(ll)fac[i-1]*i%P;
    ifac[N-1]=inv(fac[N-1]);
    PER(i,0,N-2) ifac[i]=(ll)ifac[i+1]*(i+1)%P;
    scanf("%d", &n);
    REP(i,1,n) scanf("%d",a+i);
    if (n==1) return printf("%d\n",a[1]),0;
    if (n%4==0) {
        int x = n/2-1;
        REP(i,1,n) g[i]=i&1?C(x,i/2):-C(x,i/2-1);
    }
    else if (n%4==1) {
        int x = (n-1)/2;
        REP(i,1,n) if (i&1) g[i] = C(x,i/2);
    }
    else if (n%4==2) {
        int x = (n-2)/2;
        REP(i,1,n) g[i]=C(x,(i-1)/2);
    }
    else {
        int x = (n-3)/2;
        REP(i,1,n-1) f[i]=C(x,(i-1)/2);
        REP(i,1,n) g[i]=i&1?f[i]-f[i-1]:f[i]+f[i-1];
    }
    int ans = 0;
    REP(i,1,n) ans = (ans+(ll)g[i]*a[i])%P;
    if (ans<0) ans += P;
    printf("%d\n", ans);
}
View Code

 

 

3. 815C Karen and Supermarket

Effect: $ n $ th commodity composition of a tree, the original price of $ c_i $ commodity, a decrease of $ d_i coupon will be $ dollars, but if $ i $ coupon, you must purchase a budget of $ x_i $ $ b $. yuan, seeking to buy the maximum number of goods.

Violence tree to $ $ DP complexity is $ O (n ^ 2) $, since each tuple is enumerated only once to the lca

#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <cstring>
#include <bitset>
#include <functional>
#include <random>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(a) ({REP(__i,1,n) cout<<a[__i]<<',';hr;})
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9') P = getchar (); the while (P> = ' 0 ' && P <= ' . 9 ' ) X = X * 10 + p- ' 0 ' , P = getchar (); return X;}
 // head 



const  int N = 5E3 + 10 ;
 int n-, B;
 int C [N], D [N], FA [N], SZ [N];
 int F [N] [N], G [N] [N]; 
Vector < int > a [N];
 // the f [x] [y] = x subtree, x coupon, a minimum total purchase cost y
 // the g [x] [y] = x subtree, x not coupons, bought a total of y minimum spend 
void chkmin ( int & a,int b) {a>b?a=b:0;}

void dfs(int x) {
    f[x][0] = g[x][0] = 0;
    for (int y:a[x]) {
        dfs(y);
        PER(i,0,sz[x]) REP(j,0,sz[y]) {
            chkmin(f[x][i+j],f[x][i]+min(g[y][j],f[y][j]));
            chkmin(g[x][i+j],g[x][i]+g[y][j]);
        }
        sz[x] += sz[y];
    }
    ++sz[x];
    PER(i,1,sz[x]) {
        g[x][i] = min(g[x][i],g[x][i-1]+c[x]);
        f[x][i] = f[x][i-1]+c[x]-d[x];
    }
}

int main() {
    memset(f,0x3f,sizeof f);
    memset(g,0x3f,sizeof g);
    scanf("%d%d%d%d",&n,&b,c+1,d+1);
    REP(i,2,n) { 
        scanf("%d%d%d",c+i,d+i,fa+i);
        a[fa[i]].pb(i);
    }
    dfs(1);
    PER(i,0,n) if (f[1][i]<=b||g[1][i]<=b) return printf("%d\n",i),0;
}
View Code

 

 

4. 815D Karen and Cards

Effect: $ n $ cards, each card three attributes, respectively, the upper limit of $ p, q, r $, if a card is present two attribute values ​​is strictly greater than another card, then the card can defeat another cards. ask how many cards can beat all the other cards.

Sort by $ c $, descending enumerate $ c $, on $ a, b $ to build a two-dimensional plane, it is assumed that a card of $ c_i <c $, then this card is to contribute $ 1 \ le x \ le a_i, 1 \ le y \ le b_i $ rectangular assigned zero. If $ c_i \ ge c $, then the equivalent of $ 1 \ le x \ le p, 1 \ le y \ le b_i $ and $ 1 \ le x \ le a_i, 1 \ le y \ le q $ zero the two rectangular take max segment tree with the interval, the maximum value for the query.

#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <cstring>
#include <bitset>
#include <functional>
#include <random>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(a) ({REP(__i,1,n) cout<<a[__i]<<',';hr;})
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9') P = getchar (); the while (P> = ' 0 ' && P <= ' . 9 ' ) X = X * 10 + p- ' 0 ' , P = getchar (); return X;}
 // head 



#ifdef ONLINE_JUDGE 
const  int N = 1e6 + 50 ;
 #else 
const  int N = 1E2 + 10 ;
 #endif 


int n-, P, Q, R & lt;
 struct _ { int A, B, C; F} [N];
 // interval taken max, section summing
 // maintenance interval min, into assignment section, the summing section 
struct {
     int ma,mi,tag;
    ll sum;
    void upd(int x, int t) {
        ma=mi=tag=x, sum=(ll)t*x;
    }
} tr[N<<2];
void pu(int o) {
    tr[o].ma = max(tr[lc].ma,tr[rc].ma);
    tr[o].mi = min(tr[lc].mi,tr[rc].mi);
    tr[o].sum = tr[lc].sum+tr[rc].sum;
    tr[o].tag = 0;
}
void pd(int o, int l, int r) {
    if (tr[o].tag) {
        tr[lc].upd(tr[o].tag,mid-l+1);
        tr[rc].upd(tr[o].tag,r-mid);
        tr[o].tag = 0;
    }
}
void upd(int o, int l, int r, int ql, int qr, int v) {
    if (ql>qr||tr[o].mi>=v) return;
    if (ql<=l&&r<=qr&&tr[o].ma<=v) return tr[o].upd(v,r-l+1);
    pd(o,l,r);
    if (mid>=ql) upd(ls,ql,qr,v);
    if (mid<qr) upd(rs,ql,qr,v);
    pu(o);
}
int main() {
    scanf("%d%d%d%d", &n, &p, &q, &r);
    REP(i,1,n) scanf("%d%d%d",&f[i].a,&f[i].b,&f[i].c);
    REP(i,1,n) upd(1,1,p,1,f[i].a,f[i].b);
    sort(f+1,f+1+n,[](_ a,_ b){return a.c>b.c;});
    ll ans = 0;
    int now = 1;
    PER(i,1,r) {
        while (now<=n&&f[now].c>=i) {
            upd(1,1,p,1,f[now].a,q);
            upd(1,1,p,f[now].a+1,p,f[now].b);
            ++now;
        }
        ans += (ll)p*q-tr[1].sum;
    }
    printf("%lld\n", ans);
}
View Code

 

 

5. 815E Karen and Neighborhood

Effect: $ n $-room, $ k $ individuals take turns to live, the first person to live $ 1 $ number, then everyone would live in a room farthest from someone's room, there are more words to live smallest number, ask first $ k $ personal room number.

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/uid001/p/11615692.html