Codeforces Round #322 (Div. 2) E F
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string.h>
#include <vector>
using namespace std;
typedef long long LL;
const int maxn=;
const int INF=;
struct point{
int x,t;
point(int cx=, int ct=){
x=cx; t=ct;
}
bool operator <(const point &rhs)const{
return x<rhs.x;
}
}P[maxn];
int D[][maxn],num[];
int dp[maxn][];
int e,s,n,m;
int bitlook1(int x,int *C, int n)
{
int L=,R=n-,ans=-;
while(L<=R)
{
int mid=(L+R)>>;
int id=C[mid];
if( P[id].x>=x ){
ans=mid;
R=mid-;
}else{
L=mid+;
}
}
return ans;
}
void cmp(int &A, int &B,int &V, int C, int D, int DV)
{
if(A>=C){
if(A==C){
if(B<D) return ;
if(B>D){
B=D;
V=DV;
}else{
V=min(V,DV);
}
}
else {
A=C; B=D;
V=DV;
}
}
}
void solve1(int id)
{
int loc=P[id].x;
int d1=bitlook1(loc+,D[],num[]);
int d2=bitlook1(loc+,D[],num[]);
int d3=bitlook1(loc+,D[],num[]);
if(d3!=-)d3=D[][d3];
if(d2!=-)d2=D[][d2];
if(d1!=-)d1=D[][d1];
if( d3!=- && P[d3].x <= loc+s )
{
int cp1 = dp[d3][]+P[d3].x-loc;
int cp2 = dp[d3][];
cmp(dp[id][],dp[id][],dp[id][],cp1,cp2,P[d3].x-loc);
}
if(d2!=- && P[d2].x<=loc+s)
{
int cp1 = dp[d2][]+P[d2].x-loc;
int cp2 = dp[d2][];
cmp(dp[id][],dp[id][],dp[id][],cp1,cp2,P[d2].x-loc);
}
if( d1!=- && P[d1].x<=loc+s)
{
int cp1 = dp[d1][]+P[d1].x-loc;
int cp2 = dp[d1][];
cmp(dp[id][],dp[id][],dp[id][],cp1,cp2,P[d1].x-loc);
}
}
int bitlook2(int x,int *C, int n)
{
int L=,R=n-,ans=-;
while(L<=R)
{
int mid=(L+R)>>;
int id=C[mid];
if( P[id].x<=x ){
ans=mid;
L=mid+;
}else{
R=mid-;
}
}
return ans;
}
void solve2(int id)
{
int loc=P[id].x;
int d1=bitlook2(loc+s,D[],num[]);
int d2=bitlook1(loc+,D[],num[]);
int d3=bitlook1(loc+,D[],num[]);
if(d3!=-)d3=D[][d3];
if(d2!=-)d2=D[][d2];
if(d1!=-)d1=D[][d1];
if( d3!=- && P[d3].x <= loc+s )
{
int cp1 = dp[d3][];
int cp2 = dp[d3][]+P[d3].x-loc;
cmp(dp[id][],dp[id][],dp[id][],cp1,cp2,P[d3].x-loc);
}
if(d2!=- && P[d2].x<=loc+s)
{
int cp1 = dp[d2][];
int cp2 = dp[d2][]+P[d2].x-loc;
cmp(dp[id][],dp[id][],dp[id][],cp1,cp2,P[d3].x-loc);
} if( d1!=-&&P[d1].x>loc)
{
int cp0=s;
int lit = s-(P[d1].x-loc);
int cp1 = dp[d1][]-min(lit,dp[d1][]);
int cp2 = dp[d1][]+cp0;
cmp(dp[id][],dp[id][],dp[id][],cp1,cp2,cp0);
}
}
void solve3(int id)
{
int loc=P[id].x;
int d1=bitlook2(loc+s,D[],num[]);
int d2=bitlook2(loc+s,D[],num[]);
int d3=bitlook1(loc+,D[],num[]);
if(d3!=-)d3=D[][d3];
if(d2!=-)d2=D[][d2];
if(d1!=-)d1=D[][d1]; if( d3!=- && P[d3].x<=loc+s )
{
cmp(dp[id][],dp[id][],dp[id][],dp[d3][],dp[d3][],);
}
if(d2!=-&&P[d2].x>loc)
{
int lit = s-(P[d2].x-loc);
int cp1 = dp[d2][];
int cp2 = dp[d2][]-min(dp[d2][],lit);
cmp(dp[id][],dp[id][],dp[id][],cp1,cp2,);
}
if(d1!=-&&P[d1].x>loc)
{
int lit = s-(P[d1].x-loc);
int cp1 = dp[d1][]-min(dp[d1][],lit);
int cp2 = dp[d1][];
cmp(dp[id][],dp[id][],dp[id][],cp1,cp2,);
}
}
void solve4(int loc, int &S1,int &S2)
{
int k;
int d1=bitlook2(loc+s,D[],num[]);
int d2=bitlook2(loc+s,D[],num[]);
int d3=bitlook1(loc+,D[],num[]);
if(d3!=-)d3=D[][d3];
if(d2!=-)d2=D[][d2];
if(d1!=-)d1=D[][d1];
if( d3!=- && P[d3].x<=loc+s &&dp[d3][]!=INF)
{
cmp(S1,S2,k,dp[d3][],dp[d3][],);
}
if(d2!=-&&P[d2].x>loc&&dp[d2][]!=INF)
{
int lit = s-(P[d2].x-loc);
int cp1 = dp[d2][];
int cp2 = dp[d2][]-min(dp[d2][],lit);
cmp(S1,S2,k,cp1,cp2,);
}
if(d1!=-&&P[d1].x>loc&&dp[d1][]!=INF)
{
int lit = s-(P[d1].x-loc);
int cp1 = dp[d1][]-min(dp[d1][],lit);
int cp2 = dp[d1][];
cmp(S1,S2,k,cp1,cp2,);
}
}
int main()
{
while(scanf("%d%d%d%d",&e,&s,&n,&m)==)
{
num[]=num[]=num[]=num[]=;
int k=;
for(int i=; i<n; i++) {
scanf("%d%d",&P[k].t,&P[k].x);
if(P[k].x<e)k++;
}
n=k;
sort(P,P+n);
if(P[n-].x!=e)
{
P[n]=point(e,);
n++;
}
for(int i=; i<n; i++)
if(P[i].t==) D[ ][num[]++ ] =i;
else if(P[i].t==) D[][ num[] ++ ]=i;
else D[][ num[]++ ]=i;
dp[n-][]=dp[n-][]=dp[n-][]=;
for(int i=n-; i>=;i--)dp[i][]=dp[i][]=dp[i][]=INF; for(int i=n-; i>=; i--)
{
if(P[i].t==)solve1(i);
else if(P[i].t==)solve2(i);
else solve3(i);
if(dp[i][]==INF)break;
}
for(int i=;i<m; i++)
{
int S1=INF,S2=INF;
int loc;
scanf("%d",&loc);
solve4(loc,S1,S2);
if(S1==INF){
printf("-1 -1\n");
}else{
printf("%d %d\n",S1,S2);
}
}
}
return ;
}
/*
386 20 1 1
2 369
351 */
F. Zublicanes and Mumocrates 树形dp
题意给了一棵树 要求使得这棵树所有的节点分为两个阵营,其中叶子节点要均分,求最少的两个阵营相邻, 也就是求 最少的边使得 这些边两头阵营不同
dp[cur][i][j] 表示当cur为i阵营的时候 有j个和i同阵营的方案
dp[cur][i][j+k] = min{dp[to][i^1][j=(叶子个数-d)]+dp[cur][i][k]+1,dp[to][i][j]+dp[cur][i][k]}
cnt为叶子节点的个数 最后答案是 min{ dp[root][0][cnt/2] ,dp[root][1][cnt/2]}
#include <algorithm>
#include <string.h>
#include <cstdio>
#include <iostream>
using namespace std;
const int maxn=;
int H[maxn],to[maxn*],nx[maxn*],numofE;
void add(int u, int v)
{
numofE++;
to[numofE]=v;
nx[numofE]=H[u];
H[u]=numofE;
}
int dp[maxn][][maxn];
int dp2[][maxn],sz[maxn];
int d[maxn];
void dfs(int cur ,int per)
{
if(d[cur]==){
sz[cur]=;
dp[cur][][]=dp[cur][][]=;
return ;
}
dp[cur][][]=dp[cur][][]=;
for(int i=H[cur]; i ; i=nx[i])
{
int tto = to[i];
if(tto==per)continue;
dfs(tto,cur);
for(int i=; i<=sz[cur]; i++)
for(int u=; u<; u++)
{
if( dp[cur][u][i] == - ) continue; for(int j=; j<=sz[tto]; j++)
for(int v=; v<; v++)
{
if(dp[tto][v][j]==-)continue;
if(u==v)
{
if(dp2[u][i+j]==-)
{
int k=dp[cur][u][i]+dp[tto][v][j];
dp2[u][i+j] = k; }else {
int k=min(dp[cur][u][i]+dp[tto][v][j],dp2[u][i+j]);
dp2[u][i+j]=k; }
}else
{
int nu=sz[tto];
if(dp2[u][i+nu-j]==-)
{
int k=dp[cur][u][i]+dp[tto][v][j]+;
dp2[u][i+nu-j]=k;
}
else
{
int k=min(dp[cur][u][i]+dp[tto][v][j]+,dp2[u][i+nu-j]);
dp2[u][i+nu-j]=k; }
}
}
}
sz[cur]+=sz[tto];
for(int i=; i<=sz[cur]; i++)
for(int j=; j<; j++)
{
dp[cur][j][i]=dp2[j][i];
dp2[j][i]=-;
}
}
}
int main()
{
int n;
while(scanf("%d",&n)==)
{
memset(d,,sizeof(d));
memset(dp,-,sizeof(dp));
memset(sz,,sizeof(sz));
memset(dp2,-,sizeof(dp2));
memset(H,,sizeof(H));
numofE=;
for(int i=; i<n; i++)
{
int a,b;
scanf("%d%d",&a,&b);
add(a,b);
add(b,a);
d[a]++;d[b]++;
}
if(n==){
printf("%d\n",); continue;
}
int root;
int cnt=;
for(int i=; i<=n; i++){
if(d[i]==)cnt++;
else root=i;
}
dfs(root,-);
printf("%d\n",min(dp[root][][cnt/],dp[root][][cnt/]));
}
return ;
}
/*
6
1 2
2 5
2 3
3 4
3 6
*/
Codeforces Round #322 (Div. 2) E F的更多相关文章
- Codeforces Round #573 (Div. 1) 差F
Codeforces Round #573 (Div. 1) E 题意:二维平面上有 n 个点,你可以放至多 m 条直线使得 (0,0) 与每个点的连线至少与一条直线相交.求原点与所有直线的距离最小值 ...
- Codeforces Round #322 (Div. 2) —— F. Zublicanes and Mumocrates
It's election time in Berland. The favorites are of course parties of zublicanes and mumocrates. The ...
- 树形dp - Codeforces Round #322 (Div. 2) F Zublicanes and Mumocrates
Zublicanes and Mumocrates Problem's Link Mean: 给定一个无向图,需要把这个图分成两部分,使得两部分中边数为1的结点数量相等,最少需要去掉多少条边. ana ...
- Codeforces Round #322 (Div. 2) D. Three Logos 暴力
D. Three Logos Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/581/problem ...
- Codeforces Round #322 (Div. 2) C. Developing Skills 优先队列
C. Developing Skills Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/581/p ...
- Codeforces Round #322 (Div. 2) B. Luxurious Houses 水题
B. Luxurious Houses Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/581/pr ...
- Codeforces Round #322 (Div. 2) A. Vasya the Hipster 水题
A. Vasya the Hipster Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/581/p ...
- Codeforces Round #541 (Div. 2) (A~F)
目录 Codeforces 1131 A.Sea Battle B.Draw! C.Birthday D.Gourmet choice(拓扑排序) E.String Multiplication(思路 ...
- Codeforces Round #532 (Div. 2):F. Ivan and Burgers(贪心+异或基)
F. Ivan and Burgers 题目链接:https://codeforces.com/contest/1100/problem/F 题意: 给出n个数,然后有多个询问,每次回答询问所给出的区 ...
随机推荐
- kubernetes微服务部署
1.哪些服务适合单独成为一个pod?哪些服务适合在一个pod中? message消息服务被很多服务调用 单独一个pod dubbo服务和web服务交互很高放在同一个pod里 API网关调用很多服务 ...
- tomcat停止和启动脚本
日常重启tomcat比较麻烦,所以写了2个脚本,在脚本后输入tomcat名称即可 启动或重启tomcat #!/bin/sh TOMCAT_HOME=/usr/java/$1 if [ ! -n &q ...
- [MySQL 5.6] information_schema.innodb_metrics
1. 概括 已关闭/打开的配置 use information_schema select count(*), status from innodb_metrics group by status; ...
- Why does the memory usage increase when I redeploy a web application?
That is because your web application has a memory leak. A common issue are "PermGen" memor ...
- Navigator is deprecated and has been removed from this package
报错:'Navigator is deprecated and has been removed from this package. It can now be installed ' + ...
- Python3学习之路~2.10 修改haproxy配置文件
需求: .查 输入:www.oldboy.org 获取当前backend下的所有记录 .新建 输入: arg = { 'bakend': 'www.oldboy.org', 'record':{ 's ...
- 敏捷开发— —Scrum 学习笔记
敏捷开发模式是一种从1990年代开始逐渐引起广泛关注的一些新型软件开发方法,是一种应对快速变化的需求的一种软件开发能力.它们的具体名称.理念.过程.术语都不尽相同,相对于"非敏捷" ...
- vue.js 兄弟组件传值
另: 在vue里,组件之间的作用域是独立的,父组件跟子组件之间的通讯可以通过prop属性来传参,但是在兄弟组件之间通讯就比较麻烦了,虽然可以使用事件监听的方式,但还是不如vuex专业.比如A组件要告 ...
- sap gui 配置
1: sap gui 安全配置 ,在左下角搜索sap gui configuration.
- Python 全栈开发八 文件处理
一.基本流程 打开文件得到文件句柄 将文件句柄赋值给一个变量 通过文件句柄对文件进行操作 关闭文件 二.基本操作 1.文件句柄 f = open("a.txt",encoding= ...