771C Bear and Tree Jumps

大意: 给定树,每步能走到距离不超过$k$的任意点,记$f(s,t)$为$s$到$t$的最少步数,求$\sum\limits_{s<t}f(s,t)$

对于路径$(u,v)$, 假设距离为$d$, 那么贡献为$\lceil\frac{d}{k}\rceil=\frac{d+(-d\text{%}k+k)\text{%}k}{k}$

也就是说枚举每条边的贡献算出总路径长, 再$O(nk^2)$的$dp$求出多余部分, 最后除以$k$即为答案

#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, k;
ll ans, dp[N][], sz[N];
vector<int> g[N];
void dfs(int x, int fa) {
dp[x][] = sz[x] = ;
for (int y:g[x]) if (y!=fa) {
dfs(y,x);
sz[x] += sz[y];
REP(i,,k-) REP(j,,k-) {
ll t = (-i-j-)%k;
if (t<) t += k;
ans += t*dp[x][i]*dp[y][j];
}
REP(i,,k-) dp[x][(i+)%k]+=dp[y][i];
}
ans += sz[x]*(n-sz[x]);
} int main() {
scanf("%d%d", &n, &k);
REP(i,,n) {
int u, v;
scanf("%d%d", &u, &v);
g[u].pb(v),g[v].pb(u);
}
dfs(,);
printf("%lld\n",ans/k);
}

771D Bear and Company

大意: 给定字符串, 每次操作任选两个相邻字符交换, 求最少操作次数使得不含子串"VK".

$dp$好题, 记${dp}_{v,k,x,0/1}$表示前$v$个'V',前$k$个'K',前$x$个其余字符,最后一个字符是否是'V'的最少操作次数, 然后暴力枚举字符, 算出排在它前面的字符数转移即可. 数据范围比较小可以直接$O(n^4)$, 也可以预处理一下达到$O(n^3)$

#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 = ;
int n, dp[N][N][N][];
int a[][N], c[];
char s[N];
void chkmin(int &a, int b) {a>b?a=b:;}; int main() {
scanf("%d%s", &n, s+);
REP(i,,n) {
if (s[i]=='V') a[][++c[]] = i;
else if (s[i]=='K') a[][++c[]] = i;
else s[i] = 'X', a[][++c[]] = i;
}
memset(dp,0x3f,sizeof dp);
dp[][][][] = ;
REP(v,,c[]) REP(k,,c[]) REP(x,,c[]) REP(z,,) {
auto calc = [&](int p) {
int vv=v,kk=k,xx=x,tot=p;
REP(i,,p) {
if (s[i]=='V'&&vv) --tot,--vv;
if (s[i]=='K'&&kk) --tot,--kk;
if (s[i]=='X'&&xx) --tot,--xx;
}
return tot;
};
int &r = dp[v][k][x][z];
if (r==INF) continue;
if (v!=c[]) chkmin(dp[v+][k][x][],calc(a[][v+]-)+r);
if (!z&&k!=c[]) chkmin(dp[v][k+][x][],calc(a[][k+]-)+r);
if (x!=c[]) chkmin(dp[v][k][x+][],calc(a[][x+]-)+r);
}
int ans = INF;
REP(i,,) chkmin(ans,dp[c[]][c[]][c[]][i]);
printf("%d\n", ans);
}

771E Bear and Rectangle Strips

大意: 给定2*n棋盘, 求最多选出多少个不相交的和为零的矩形.

区间$dp$的话很容易做, 但是复杂度是$O(n^2)$的, 难以优化.

考虑另一种$dp$, 记${dp}_{i,j}$为第一行到第$i$个,第二行到$j$个时的最大值.

这样状态数同样是$O(n^2)$的, 但是注意到转移时可以贪心, 每个位置只向后取一个最小的矩形, 显然答案不会变差, 那么这样相当于维护一个轮廓线$dp$, 轮廓线状态数是$O(n)$的.

#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], pos[][N], ans[N];
ll s[][N];
map<ll,int> v[];
pii ext[][N]; void chkmax(int &a, int b) {
if (a<b) a = b;
}
void chkmax(pii &a, pii b) {
if (b.y>a.y||b.y==a.y&&b.x<a.x) a = b;
} void upd(int x, int y, int s) {
if (x<y) chkmax(ans[y],s),chkmax(ext[][x],pii(y,s));
else chkmax(ans[x],s),chkmax(ext[][y],pii(x,s));
} //第一行x,第二行y,之前的最优值为s,往后拓展答案
void extend(int x, int y, int s) {
upd(x+,y,s), upd(x,y+,s);
if (pos[][x]) upd(pos[][x],y,s+);
if (pos[][y]) upd(x,pos[][y],s+);
if (x==y&&pos[][x]) upd(pos[][x],pos[][x],s+);
} int main() {
scanf("%d", &n);
REP(i,,) REP(j,,n) scanf("%d",a[i]+j);
REP(i,,) PER(j,,n) {
s[i][j] = s[i][j+]+a[i][j];
s[][j] += s[i][j];
}
REP(i,,) PER(j,,n+) {
pos[i][j] = v[i][s[i][j]];
v[i][s[i][j]] = j;
}
REP(i,,n) {
extend(i,i,ans[i]);
if (ext[][i].y) extend(i,ext[][i].x,ext[][i].y);
if (ext[][i].y) extend(ext[][i].x,i,ext[][i].y);
}
printf("%d\n", ans[n+]);
}

VK Cup 2017 - Round 1 (CDE)的更多相关文章

  1. Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) 菜鸡只会ABC!

    Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) 全场题解 菜鸡只会A+B+C,呈上题解: A. Bear and ...

  2. Codeforces Round #412 (rated, Div. 2, base on VK Cup 2017 Round 3)(A.B.C,3道暴力题,C可二分求解)

    A. Is it rated? time limit per test:2 seconds memory limit per test:256 megabytes input:standard inp ...

  3. Codeforces Round #412 (rated, Div. 2, base on VK Cup 2017 Round 3) A B C D 水 模拟 二分 贪心

    A. Is it rated? time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  4. Codeforces Round #409 (rated, Div. 2, based on VK Cup 2017 Round 2)(A.思维题,B.思维题)

    A. Vicious Keyboard time limit per test:2 seconds memory limit per test:256 megabytes input:standard ...

  5. VK Cup 2017 - Round 1

    和FallDream组队瞎打一通--B两个人写的都挂了233,最后只剩下FallDream写的A和我写的C,最后我yy了个E靠谱做法结果打挂了,结束之后改了改就A了,难受. AC:AC Rank:18 ...

  6. Codeforces Round #409 (rated, Div. 2, based on VK Cup 2017 Round 2) 题解【ABCDE】

    A. Vicious Keyboard 题意:给你一个字符串,里面只会包含VK,这两种字符,然后你可以改变一个字符,你要求VK这个字串出现的次数最多. 题解:数据范围很小,暴力枚举改变哪个字符,然后c ...

  7. Codeforces Round #409 (rated, Div. 2, based on VK Cup 2017 Round 2) A B C D 暴力 水 二分 几何

    A. Vicious Keyboard time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  8. Codeforces Round #412 (rated, Div. 2, base on VK Cup 2017 Round 3) D - Dynamic Problem Scoring

    地址:http://codeforces.com/contest/807/problem/D 题目: D. Dynamic Problem Scoring time limit per test 2 ...

  9. Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) E

    Description Bear Limak prepares problems for a programming competition. Of course, it would be unpro ...

随机推荐

  1. win7 激活码 秘钥

    019.06最新windows7旗舰版系统激活码: 目前市面上的win7旗舰版激活码大部分都已经过期或失效了,下面来分享一些最新的. win7旗舰版激活密钥: BG2KW-D62DF-P4HY6-6J ...

  2. itop 环境

    iTop,即IT运营门户(IT Operation Portal),是一个开源web应用程序,用于IT环境的日常运营.它基于ITIL最佳实践,而又不拘泥于任何具体流程.它很灵活,可以适应不管是非正 式 ...

  3. Java Web J2EE下的两大框架SSH和SSM对比

    当下流行的两种企业开发MVC开源框架,是我们Java程序猿必备知识能力.MVC,即模型(model)-视图(view)-控制器(controller)的缩写,一种软件设计典范,用一种业务逻辑.数据.界 ...

  4. ByteBuffer: 图解ByteBuffer(转)

    ByteBuffer前前后后看过好几次了,实际使用也用了一些,总觉得条理不够清晰. <程序员的思维修炼>一本书讲过,主动学习,要比单纯看资料效果来的好,所以干脆写个详细点的文章来记录一下. ...

  5. android studio 运行按钮为灰色的解决办法之一

    sync project with gradle files按钮(如下图)同步一下就好了 3.2的  3.3同步按钮变成了一只大象+箭头

  6. ionic生命周期函数

    Ionic4中的生命周期函数和angualr7基本是一样的,下面我们看看Ionic4中的生命周期函数,以及生命周期函数的用法. Ionic4中内置的生命周期函数: ionViewWillEnter — ...

  7. 【转载】 Bill Gates和Elon Musk推荐,人工智能必读的三本书 -《终极算法》,《超级智能》和《终极发明》

    原文地址: https://blog.csdn.net/ztf312/article/details/80761917 ---------------------------------------- ...

  8. ES6深入浅出-4 迭代器与生成器-1.字面量增强

    今天的内容 字面量literal 写出来就是它的值 例如字符串hello.这就是自变量. 一个空对象,也是自变量 写出来就是代表它写出来的那个意思就是自变量. 与其相反的就是构造出来的.例如下面的ne ...

  9. Linux记录-shell 100例(转载)

    1.编写hello world脚本 #!/bin/bash # 编写hello world脚本 echo "Hello World!" 2.通过位置变量创建 Linux 系统账户及 ...

  10. 报错:Exception: org.apache.sqoop.common.SqoopException Message: DRIVER_0002:Given job is already running - Job with id 1

    报错背景: 创建完成job之后,执行job的时候报错. 报错现象: Exception: org.apache.sqoop.common.SqoopException Message: CLIENT_ ...