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. 后端小白的Bootstrap笔记 一

    栅格系统 下面这张图是Bootstrap对栅格系统有关系数的规定 什么是栅格体统? 栅格系统是Bootstrap提供的移动优先的网格系统, 各个分界点如上: 576px 720px 992px 120 ...

  2. python PIL/cv2/base64相互转换

    PIL和cv2是python中两个常用的图像处理库,PIL一般是anaconda自带的,cv2是opencv的python版本.base64在网络传输图片的时候经常用到. ##PIL读取.保存图片方法 ...

  3. hive匹配中文

    select regexp_extract('ab中文123测试55..', '[\u4e00-\u9fa5]+', 0) 只提出成功第一段中文汉字,结果为: 中文 select regexp_rep ...

  4. PHP实现执行定时任务

    首先用命令检查服务是否在运行 systemctl status crond.service 如果服务器上没有装有crontab ,则可以执行 yum install vixie-cron yum in ...

  5. flutter 右滑返回上一页

    import 'package:flutter/material.dart'; import 'package:flutter_app/pages/SplashScreen.dart'; import ...

  6. WPF-支持异步操作的ObservableCollection-AsyncObservableCollection

    在进行WPF开发过程中,需要从一个新的线程中操作ObservableCollection,结果程序抛出一个NotSupportedException的错误 public class AsyncObse ...

  7. linux安装qt

    1.下载run文件 2../运行 3.修改配置文件 sudo gedit /etc/profile 添加如下: port QTDIR=/home/rainbow/zhuxy/soft/Qt5.9.0/ ...

  8. matlab学习笔记1--matlab工作界面

    一起来学matlab-matlab学习笔记1--matlab工作界面 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考书籍 <matlab 程序设计与综合应用>张德丰等著 感谢 ...

  9. LeetCode_257. Binary Tree Paths

    257. Binary Tree Paths Easy Given a binary tree, return all root-to-leaf paths. Note: A leaf is a no ...

  10. redis和memcache对比

    1.性能方面:没有必要过多的关心性能,因为二者的性能都已经足够高了.由于Redis只使用单核,而Memcached可以使用多核,所以在比较上,平均每一个核上Redis在存储小数据时比Memcached ...