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. [转]Myeclipse四种方式发布项目

    原文链接: myeclipse四种方式发布项目

  2. 目录窗口多选Multiple Select in Catalog Window or arccatalog

    目录窗口多选Multiple Select in Catalog Window or arccatalog 商务合作,科技咨询,版权转让:向日葵,135-4855__4328,xiexiaokui#q ...

  3. Android命名规范(重点讲解:包名)

    Android程序开发中,使用规范的命名有益于程序的开发和后期阅读.本文主要对Android程序包名的定义做详细介绍,并附带一些简单的命名规则. 一.标识符命名方法1 .小驼峰命名法,除首单词外,其余 ...

  4. Java 读取clob字段的几种方法

    Java 读取clob字段的几种方法 一.第一种 Clob clob = rs.getClob("remark");//Java.sql.Clob String detailinf ...

  5. disruptor 单生产者多消费者

    demo1 单生产者多消费者创建. maven 依赖 <!-- https://mvnrepository.com/artifact/com.lmax/disruptor --> < ...

  6. (转)golang获取当前时间、时间戳和时间字符串及它们之间的相互转换

    原文连接: https://blog.csdn.net/skh2015java/article/details/70051512 1.获取当前时间 currentTime:=time.Now() // ...

  7. 使用 ASP.NET Core 创建 Web API及链接sqlserver数据库

    创建 Web API https://docs.microsoft.com/zh-cn/aspnet/core/tutorials/first-web-api?view=aspnetcore-3.0& ...

  8. Python之Pandas绘图,设置显示中文问题

    # -*- coding: utf-8 -*- # author:baoshan import pandas as pd import matplotlib.pyplot as plt plt.rcP ...

  9. python的argparse模块parse_known_args()方法的使用

    parse_known_args()方法的用处就是有些时候,你的选项配置可能不会在一个函数或包中调用完成 在很多使用,我们可能会需要根据一些输入的选项,比如在深度学习中,我们可能会根据传入的模型设置- ...

  10. Eclipse | 如何修改web项目的访问链接名,项目名

    转: Eclipse | 如何修改web项目的访问链接名,项目名 2018-01-04 17:52:05 Mandsence 阅读数 2180更多 分类专栏: 其他   版权声明:本文为博主原创文章, ...