VK Cup 2017 - Round 1 (CDE)
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)的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- VK Cup 2017 - Round 1
和FallDream组队瞎打一通--B两个人写的都挂了233,最后只剩下FallDream写的A和我写的C,最后我yy了个E靠谱做法结果打挂了,结束之后改了改就A了,难受. AC:AC Rank:18 ...
- Codeforces Round #409 (rated, Div. 2, based on VK Cup 2017 Round 2) 题解【ABCDE】
A. Vicious Keyboard 题意:给你一个字符串,里面只会包含VK,这两种字符,然后你可以改变一个字符,你要求VK这个字串出现的次数最多. 题解:数据范围很小,暴力枚举改变哪个字符,然后c ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- 文献阅读 | The single-cell transcriptional landscape of mammalian organogenesis | 器官形成 | 单细胞转录组
The single-cell transcriptional landscape of mammalian organogenesis 老板已经提了无数遍的文章,确实很nb,这个工作是之前我们无法想 ...
- 微信小程序之自定义底部弹出框动画
最近做小程序时,会经常用到各种弹框.直接做显示和隐藏虽然也能达到效果,但是体验性太差,也比较简单粗暴.想要美美地玩,添加点动画还是非常有必要的.下面做一个底部上滑的弹框. wxml <view ...
- keepalived非争抢机制不生效【原创】
故障现象:俩台服务器设置了keepalived非争抢机制,但是发现优先级高的服务器还是会优先获取vip 利用tcpdump抓包发现只有其中一台服务器,没有另一台 tcpdump -i eth0 vrr ...
- Mysql中如何查看慢查询以及查看线程
一.MySQL数据库有几个配置选项可以帮助我们及时捕获低效SQL语句 1,slow_query_log这个参数设置为ON,可以捕获执行时间超过一定数值的SQL语句. 2,long_query_time ...
- return语句——学习笔记
return,可以提前结束其所在函数. 函数内不写,会自动加上return. 非引用返回: 引用返回:a=3,b=3 注意事项: 两种修改字符串某一位置值的方式:
- Ionic4 Cordova 调用原生硬件 Api 实现扫码功能
QR Scanner 速度快,样式随心所欲,默认只能扫二维码 https://ionicframework.com/docs/native/qr-scanner/ 安装插件 ionic cordova ...
- ISO/IEC 9899:2011 条款6.9——外部定义
6.9 外部定义 语法 1.translation-unit: external-declaration translation-unit external-declaration extern ...
- 深度学习:21天实战caffe学习资源-4-环境安装
使用anaconda3环境下的python2.7, 机器macos mojave 10.14 1.安装Xcode 首先现在app store中安装Xcode: 不然会有” framework not ...
- 如何:创建返回 UI 的外接程序
https://msdn.microsoft.com/zh-cn/library/bb909849(v=vs.100).aspx
- IFC构件位置信息—ObjectPlacement
在IFC标准中,采用相对坐标系对构件定位.如柱(IfcColumn)的定位信息(局部坐标系及参考坐标系)由ObjectPlacement描述.ObjectPlacement由两部分组成: (1)Pla ...