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+, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=%P;for (a%=P;n;a=a*a%P,n>>=)if(n&)r=r*a%P;return r;}
ll inv(ll x){return x<=?:inv(P%x)*(P-P/x)%P;}
inline int rd() {int x=;char p=getchar();while(p<''||p>'')p=getchar();while(p>=''&&p<='')x=x*+p-'',p=getchar();return x;}
//head const int N = 1e6+;
int n,m,a[][];
int op[N], p[N], cnt; void add(int tp, int x) {
++cnt;
op[cnt] = tp, p[cnt] = x;
if (tp==) REP(i,,n) --a[i][x];
else REP(i,,m) --a[x][i];
}
int main() {
scanf("%d%d",&n,&m);
int sum = ;
REP(i,,n) REP(j,,m) scanf("%d",a[i]+j),sum+=a[i][j];
while (sum) {
if (n>m) {
int ok = ;
REP(i,,m) {
int s = 1e9;
REP(j,,n) s=min(s,a[j][i]);
if (s) add(,i),sum-=n,ok=;
}
if (!ok) {
REP(i,,n) {
int s = 1e9;
REP(j,,m) s=min(s,a[i][j]);
if (s) add(,i),sum-=m,ok=;
}
if (!ok) return puts("-1"),;
}
}
else {
int ok = ;
REP(i,,n) {
int s = 1e9;
REP(j,,m) s=min(s,a[i][j]);
if (s) add(,i),ok=,sum-=m;
}
if (!ok) {
REP(i,,m) {
int s = 1e9;
REP(j,,n) s=min(s,a[j][i]);
if (s) add(,i),ok=,sum-=n;
}
if (!ok) return puts("-1"),;
}
}
}
printf("%d\n",cnt);
REP(i,,cnt) printf("%s %d\n",op[i]==?"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+, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=%P;for (a%=P;n;a=a*a%P,n>>=)if(n&)r=r*a%P;return r;}
ll inv(ll x){return x<=?:inv(P%x)*(P-P/x)%P;}
inline int rd() {int x=;char p=getchar();while(p<''||p>'')p=getchar();while(p>=''&&p<='')x=x*+p-'',p=getchar();return x;}
//head const int N = ;
vector<int> f[N][N];
int n, a[N];
vector<int> add(vector<int> a, vector<int> b) {
REP(i,,n) a[i]+=b[i];
return a;
}
vector<int> sub(vector<int> a, vector<int> b) {
REP(i,,n) a[i]-=b[i];
return a;
}
int main() {
scanf("%d",&n);
REP(i,,n) REP(j,,n) f[i][j].resize(n+);
REP(i,,n) f[][i][i] = ;
int cur = ;
REP(i,,n) {
REP(j,,n-i+) {
if (cur) f[i][j]=add(f[i-][j],f[i-][j+]);
else f[i][j]=sub(f[i-][j],f[i-][j+]);
cur ^= ;
}
}
REP(i,,n) printf("%d,",f[n][][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+, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=%P;for (a%=P;n;a=a*a%P,n>>=)if(n&)r=r*a%P;return r;}
ll inv(ll x){return x<=?:inv(P%x)*(P-P/x)%P;}
inline int rd() {int x=;char p=getchar();while(p<''||p>'')p=getchar();while(p>=''&&p<='')x=x*+p-'',p=getchar();return x;}
//head const int N = 1e6+;
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[]=;
REP(i,,N-) fac[i]=(ll)fac[i-]*i%P;
ifac[N-]=inv(fac[N-]);
PER(i,,N-) ifac[i]=(ll)ifac[i+]*(i+)%P;
scanf("%d", &n);
REP(i,,n) scanf("%d",a+i);
if (n==) return printf("%d\n",a[]),;
if (n%==) {
int x = n/-;
REP(i,,n) g[i]=i&?C(x,i/):-C(x,i/-);
}
else if (n%==) {
int x = (n-)/;
REP(i,,n) if (i&) g[i] = C(x,i/);
}
else if (n%==) {
int x = (n-)/;
REP(i,,n) g[i]=C(x,(i-)/);
}
else {
int x = (n-)/;
REP(i,,n-) f[i]=C(x,(i-)/);
REP(i,,n) g[i]=i&?f[i]-f[i-]:f[i]+f[i-];
}
int ans = ;
REP(i,,n) ans = (ans+(ll)g[i]*a[i])%P;
if (ans<) 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+, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=%P;for (a%=P;n;a=a*a%P,n>>=)if(n&)r=r*a%P;return r;}
ll inv(ll x){return x<=?:inv(P%x)*(P-P/x)%P;}
inline int rd() {int x=;char p=getchar();while(p<''||p>'')p=getchar();while(p>=''&&p<='')x=x*+p-'',p=getchar();return x;}
//head const int N = 5e3+;
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:;} void dfs(int x) {
f[x][] = g[x][] = ;
for (int y:a[x]) {
dfs(y);
PER(i,,sz[x]) REP(j,,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,,sz[x]) {
g[x][i] = min(g[x][i],g[x][i-]+c[x]);
f[x][i] = f[x][i-]+c[x]-d[x];
}
} int main() {
memset(f,0x3f,sizeof f);
memset(g,0x3f,sizeof g);
scanf("%d%d%d%d",&n,&b,c+,d+);
REP(i,,n) {
scanf("%d%d%d",c+i,d+i,fa+i);
a[fa[i]].pb(i);
}
dfs();
PER(i,,n) if (f[][i]<=b||g[][i]<=b) return printf("%d\n",i),;
}

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+, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=%P;for (a%=P;n;a=a*a%P,n>>=)if(n&)r=r*a%P;return r;}
ll inv(ll x){return x<=?:inv(P%x)*(P-P/x)%P;}
inline int rd() {int x=;char p=getchar();while(p<''||p>'')p=getchar();while(p>=''&&p<='')x=x*+p-'',p=getchar();return x;}
//head #ifdef ONLINE_JUDGE
const int N = 1e6+;
#else
const int N = 1e2+;
#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<<];
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 = ;
}
void pd(int o, int l, int r) {
if (tr[o].tag) {
tr[lc].upd(tr[o].tag,mid-l+);
tr[rc].upd(tr[o].tag,r-mid);
tr[o].tag = ;
}
}
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+);
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,,n) scanf("%d%d%d",&f[i].a,&f[i].b,&f[i].c);
REP(i,,n) upd(,,p,,f[i].a,f[i].b);
sort(f+,f++n,[](_ a,_ b){return a.c>b.c;});
ll ans = ;
int now = ;
PER(i,,r) {
while (now<=n&&f[now].c>=i) {
upd(,,p,,f[now].a,q);
upd(,,p,f[now].a+,p,f[now].b);
++now;
}
ans += (ll)p*q-tr[].sum;
}
printf("%lld\n", ans);
}

5. 815E Karen and Neighborhood

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

Codeforces Round #419 (Div. 1) (ABCD)的更多相关文章

  1. Codeforces Round #258 (Div. 2)[ABCD]

    Codeforces Round #258 (Div. 2)[ABCD] ACM 题目地址:Codeforces Round #258 (Div. 2) A - Game With Sticks 题意 ...

  2. Codeforces Round #354 (Div. 2) ABCD

    Codeforces Round #354 (Div. 2) Problems     # Name     A Nicholas and Permutation standard input/out ...

  3. Codeforces Round #268 (Div. 2) ABCD

    CF469 Codeforces Round #268 (Div. 2) http://codeforces.com/contest/469 开学了,时间少,水题就不写题解了,不水的题也不写这么详细了 ...

  4. Codeforces Round #449 (Div. 2)ABCD

    又掉分了0 0. A. Scarborough Fair time limit per test 2 seconds memory limit per test 256 megabytes input ...

  5. Codeforces Round #143 (Div. 2) (ABCD 思维场)

    题目连链接:http://codeforces.com/contest/231 A. Team time limit per test:2 seconds memory limit per test: ...

  6. Codeforces Round #248 (Div. 2) (ABCD解决问题的方法)

    比赛链接:http://codeforces.com/contest/433 A. Kitahara Haruki's Gift time limit per test:1 second memory ...

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

    http://codeforces.com/contest/816/problem/E 题意: 去超市买东西,共有m块钱,每件商品有优惠卷可用,前提是xi商品的优惠券被用.问最多能买多少件商品? 思路 ...

  8. Codeforces Round #419 (Div. 2) B. Karen and Coffee(经典前缀和)

    http://codeforces.com/contest/816/problem/B To stay woke and attentive during classes, Karen needs s ...

  9. Codeforces Round #419 (Div. 2) A. Karen and Morning(模拟)

    http://codeforces.com/contest/816/problem/A 题意: 给出一个时间,问最少过多少时间后是回文串. 思路: 模拟,先把小时的逆串计算出来: ① 如果逆串=分钟, ...

随机推荐

  1. NPVariant -js传递给NPAPI插件参数在firefox和chrome需要采用不同的获取方式

    原帖地址:http://blog.sina.com.cn/s/blog_4c6631790102wd1o.html 整数参数 typedef struct _NPVariant { NPVariant ...

  2. NTC3950-10K温度传感器

    一.计算公式 补充: B=3950 R=10K T2=25度 查RT表,25度对应的是10K 电路: 热敏电阻与上拉电阻R813分压,获取温度与Vo电压的关系,在根据Vo折算出与MCU ADC的数值. ...

  3. 完美解决: org.apache.ibatis.binding.BindingException Invalid bound statement (not found)

    异常描述: org.apache.ibatis.binding.BindingException: Invalid bound statement (not found) 原因: springboot ...

  4. LSTM和双向LSTM讲解及实践

    LSTM和双向LSTM讲解及实践 目录 RNN的长期依赖问题LSTM原理讲解双向LSTM原理讲解Keras实现LSTM和双向LSTM 一.RNN的长期依赖问题 在上篇文章中介绍的循环神经网络RNN在训 ...

  5. sqlserver2016 kb补丁

    1. win2012r2 安装时 总是提示: 然后费了半天劲 下载下来又提示 找了一下 需要先安装这么一个补丁才可以 KB2919442 然后才能安装上 KB2919355 然后就可以正常安装了:

  6. B站在微服务治理中的探索与实践

    https://mp.weixin.qq.com/s/_iFe8DO1e-QcYG-CJDTHpg

  7. ArcPy python实例教程-条件平差-测量平差

    ArcPy python实例教程-条件平差-测量平差 商务合作,科技咨询,版权转让:向日葵,135-4855__4328,xiexiaokui#qq.com 输入参数:条件方程的系数,观测值,常数项和 ...

  8. 004-行为型-05-职责链模式(Chain of Responsibility)

    一.概述 为请求创建一个接收此次请求对象的链 该模式构造一系列分别担当不同的职责的类的对象来共同完成一个任务,这些类的对象之间像链条一样紧密相连,所以被称作职责链模式. 在这种模式中,通常每个接收者都 ...

  9. c#写windows服务 小demo

    前段时间做一个数据迁移项目,刚开始用B/S架构做的项目,但B/S要寄存在IIs中,而IIs又不稳定因素,如果重启IIs就要打开页面才能运行项目.有不便之处,就改用Windows服务实现.这篇就总结下, ...

  10. PngOptimizer PNG压缩工具

    好用,非常好用,速度快. 把图片拖入即可,同文件夹备份替换压缩. 点击下载