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

Posted uid001

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces Round #419 (Div. 1) (ABCDE)相关的知识,希望对你有一定的参考价值。

1. 815A Karen and Game

大意: 给定$nm$矩阵, 每次选择一行或一列全部减$1$, 求最少次数使得矩阵全$0$

贪心, $n>m$时每次取一列, 否则取一行

技术图片
#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

大意: 有一个三角形的数表, 给定第一行, 下面每一行是上面相邻两数和或差, 求最底层的数是多少.

打表算一下每个数的贡献, 找下规律即可

技术图片
#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;
打表的代码 
技术图片
#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

大意: $n$个商品构成一棵树, 商品原价$c_i$元, 用优惠券会减少$d_i$元, 但如果$i$使用优惠券, 那么必须购买$x_i$. 预算为$b$元, 求最多买多少商品.

暴力树形$dp$即可, 复杂度是$O(n^2)$, 因为每个二元组只会在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();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];
//f[x][y] = x子树内, x用优惠券, 一共购买y个的最少花费
//g[x][y] = x子树内, x不用优惠券, 一共购买y个的最少花费
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

大意: $n$张卡, 每张卡三个属性, 上限分别为$p,q,r$, 若一张卡存在两个属性值严格大于另一张卡, 那么这张卡能打败另一张卡. 求有多少张卡能打败其他所有卡.

按$c$排序, 从大到小枚举$c$, 对$a,b$建一个二维平面, 那么假设一张卡的$c_i<c$, 那么这张卡贡献就是对$1\le x\le a_i,1\le y\le b_i$的矩形赋零. 若$c_i\ge c$, 那么相当于对$1\le x\le p,1\le y\le b_i$和$1\le x\le a_i,1\le y\le q$的两个矩形赋零. 用线段树区间取max, 查询最大值即可.

技术图片
#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



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


int n, p, q, r;
struct _ int a,b,c; f[N];
//区间取max, 区间求和
//维护区间min, 转化为区间赋值, 区间求和
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

大意: $n$个房间,$k$个人轮流去住, 第一个人住$1$号, 之后每个人会住距离有人的房间最远的房间, 有多个的话住编号最小的, 求第$k$个人房间号.

 

 

 

 

 

以上是关于Codeforces Round #419 (Div. 1) (ABCDE)的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces Round #419 C

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

Codeforces Round #419 B

Codeforces Round #419 (Div. 2) A-E

Codeforces Round #419 (Div. 2)C. Karen and Game

Codeforces Round #419 (Div. 2)B. Karen and Coffee