Codeforces Round #419 (Div. 1) (ABCD)
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)的更多相关文章
- Codeforces Round #258 (Div. 2)[ABCD]
Codeforces Round #258 (Div. 2)[ABCD] ACM 题目地址:Codeforces Round #258 (Div. 2) A - Game With Sticks 题意 ...
- Codeforces Round #354 (Div. 2) ABCD
Codeforces Round #354 (Div. 2) Problems # Name A Nicholas and Permutation standard input/out ...
- Codeforces Round #268 (Div. 2) ABCD
CF469 Codeforces Round #268 (Div. 2) http://codeforces.com/contest/469 开学了,时间少,水题就不写题解了,不水的题也不写这么详细了 ...
- Codeforces Round #449 (Div. 2)ABCD
又掉分了0 0. A. Scarborough Fair time limit per test 2 seconds memory limit per test 256 megabytes input ...
- Codeforces Round #143 (Div. 2) (ABCD 思维场)
题目连链接:http://codeforces.com/contest/231 A. Team time limit per test:2 seconds memory limit per test: ...
- Codeforces Round #248 (Div. 2) (ABCD解决问题的方法)
比赛链接:http://codeforces.com/contest/433 A. Kitahara Haruki's Gift time limit per test:1 second memory ...
- Codeforces Round #419 (Div. 2) E. Karen and Supermarket(树形dp)
http://codeforces.com/contest/816/problem/E 题意: 去超市买东西,共有m块钱,每件商品有优惠卷可用,前提是xi商品的优惠券被用.问最多能买多少件商品? 思路 ...
- 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 ...
- Codeforces Round #419 (Div. 2) A. Karen and Morning(模拟)
http://codeforces.com/contest/816/problem/A 题意: 给出一个时间,问最少过多少时间后是回文串. 思路: 模拟,先把小时的逆串计算出来: ① 如果逆串=分钟, ...
随机推荐
- SQL Server 2012 无人值守安装
方法1,通过指定条个參数安装 setup.exe /Q /IACCEPTSQLSERVERLICENSETERMS /ACTION=install /PID=<validpid> /F ...
- 《Linux设备驱动程序》第三版 scull编译 Ubuntu18.04
0 准备工作. 0.0 系统环境:Ubuntu18.04.1 amd64. 0.1 安装必要软件包 1 sudo apt install build-essential bison flex libs ...
- At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger fo
一.文章前言 本文是亲测有效解决At least one JAR was scanned for TLDs yet contained no TLDs问题,绝对不是为了积分随便粘贴复制然后压根都没有用 ...
- Python爬取所有城市的aqi值
# -*- coding: utf-8 -*- # author:baoshan import requests from bs4 import BeautifulSoup def get_city_ ...
- 将一个多表关联的条件查询中的多表通过 create select 转化成一张单表的sql、改为会话级别临时表 【我】
将一个多表关联的条件查询中的多表通过 create select 转化成一张单表的sql 将结果改为创建一个会话级别的临时表: -- 根据下面这两个sql CREATE TABLE revenu ...
- visual studio code跳转到定义处插件
visual studio code 中使用跳转到定义处的插件 https://marketplace.visualstudio.com/items?itemName=Shan.code-settin ...
- spring 加载属性(properties)文件
在开发的过程中,配置文件往往就是那些属性(properties)文件,比如使用properties文件配置数据库文件,又如database-config.properties 代码清单:databas ...
- Element 'repository' cannot have character [children], because the type's content type is element-only.
出错现象 由于代码是网络上拷贝来的,可能存在特殊字符,在进行maven打包的时候报错 [ERROR] Malformed POM F:\ai开放平台\SRC\web知识产权申请\pom.xml: ex ...
- Top 10 Machine Learning Algorithms For Beginners
Linear Regression Logistic regression KNN Classification Support Vector Machine (SVM) Decision Trees ...
- yarn那些事儿
本篇文章立足于mac. 一.安装yarn 1.通过homebrew brew update brew install yarn 2.通过脚本 curl -o- -L https://yarnpkg.c ...