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]);
}

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);
}

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;
}

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);
}

5. 815E Karen and Neighborhood

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

原文地址:https://www.cnblogs.com/uid001/p/11615692.html

时间: 2024-10-11 06:03:54

Codeforces Round #419 (Div. 1) (ABCDE)的相关文章

Codeforces Round #261 (Div. 2)[ABCDE]

Codeforces Round #261 (Div. 2)[ABCDE] ACM 题目地址:Codeforces Round #261 (Div. 2) A - Pashmak and Garden 题意: 一个正方形,它的边平行于坐标轴,给出这个正方形的两个点,求出另外两个点. 分析: 推断下是否平行X轴或平行Y轴,各种if. 代码: /* * Author: illuz <iilluzen[at]gmail.com> * File: A.cpp * Create Date: 2014-0

Codeforces Round #264 (Div. 2)[ABCDE]

Codeforces Round #264 (Div. 2)[ABCDE] ACM 题目地址: Codeforces Round #264 (Div. 2) 这场只出了两题TAT,C由于cin给fst了,D想到正解快敲完了却game over了... 掉rating掉的厉害QvQ... A - Caisa and Sugar[模拟] 题意: Caisa拿s美元去超市买sugar,有n种sugar,每种为xi美元yi美分,超市找钱时不会找美分,而是用sweet代替,当然能用美元找就尽量用美元找.他

Codeforces Round #419 (Div. 2) E. Karen and Supermarket(树形DP)

题目链接:Codeforces Round #419 (Div. 2) E. Karen and Supermarket 题意: 有n件物品,每个物品有一个价格,和一个使用优惠券的价格,不过这个优惠券有一个限制,必须要在第x个使用后才可以使用.现在有m的钱,问最多能买多少个物品. 题解: 每个优惠券都只与一个券有关,所以根据这个关系就可以构成一棵树. 考虑树形dp,dp[i][j][k(0|1)]表示第i个节点所构成的子树中买了j个物品,使用优惠券和不使用优惠券的最少钱. 转移方程看代码详细解释

Codeforces Round #260 (Div. 2) ABCDE

A题逗比了,没有看到All ai are distinct. All bi are distinct. 其实很水的.. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 using namespace std; 6 7 #define mnx 100002 8 9 10 struct latop{ 11 int p, q; 12 bo

Codeforces Round #531 (Div. 3) ABCDE题解

Codeforces Round #531 (Div. 3) 题目总链接:https://codeforces.com/contest/1102 A. Integer Sequence Dividing 题意: 给一个数n,然后要求你把1,2.....n分为两个集合,使得两个集合里面元素的和的差的绝对值最小. 题解: 分析可以发现,当n%4==0 或者 n%3==0,答案为0:其余答案为1.之后输出一下就好了. 代码如下: #include <bits/stdc++.h> using name

Codeforces Round #200 (Div. 2) (ABCDE题解)

比赛链接:http://codeforces.com/contest/344 A. Magnets time limit per test:1 second memory limit per test:256 megabytes Mad scientist Mike entertains himself by arranging rows of dominoes. He doesn't need dominoes, though: he uses rectangular magnets inst

Codeforces Round #105 (Div. 2) (ABCDE题解)

比赛链接:http://codeforces.com/contest/148 比较简单的一场,最长的一题也才写了30行多一点 A. Insomnia cure time limit per test:2 seconds memory limit per test:256 megabytes ?One dragon. Two dragon. Three dragon?, - the princess was counting. She had trouble falling asleep, and

Codeforces Round #186 (Div. 2) (ABCDE题解)

比赛链接:http://codeforces.com/contest/313 A. Ilya and Bank Account time limit per test:2 seconds memory limit per test:256 megabytes Ilya is a very clever lion, he lives in an unusual city ZooVille. In this city all the animals have their rights and obl

Codeforces Round #296 (Div. 2) (ABCDE题解)

比赛链接:http://codeforces.com/contest/527 A. Playing with Paper time limit per test:2 seconds memory limit per test:256 megabytes One day Vasya was sitting on a not so interesting Maths lesson and making an origami from a rectangular a mm ?×? b mm sheet