Summer training #9
A:树形DP
给出一棵树,但是它的边是有向边,选择一个城市,问最少调整多少条边的方向能使一个选中城市可以到达所有的点,输出最小的调整的边数,和对应的点
要改变的边的权值为1,不需要改变的边的权值为0,
两次dfs 第一次算出以1点为根节点到所有点要改变的边数,第二次以1为根节点向下遍历节点 算出每一个点到达所有点要改变的边数,
dp[son]+=(dp[root]-dp[son])+((tree[i].val)?-1:1),某一点的值是他父节点的值减去他以前的值再考虑他与父节点之间的边的方向。
#include<iostream>
#include<string>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<queue>
#include<stack>
using namespace std;
int n,lne;
int dp[];
int head[];
int city[];
struct node
{
int to,next,val;
}tree[];
void add(int a,int b)
{
tree[lne].to=b;
tree[lne].val=;
tree[lne].next=head[a];
head[a]=lne++;
tree[lne].to=a;
tree[lne].val=;
tree[lne].next=head[b];
head[b]=lne++;
}
void dfs1(int root,int pre)
{
for(int i=head[root];i!=-;i=tree[i].next)
{
int son=tree[i].to;
if(son==pre)
continue;
dfs1(son,root);
dp[root]+=dp[son]+tree[i].val;
}
}
void dfs2(int root,int pre)
{
for(int i=head[root];i!=-;i=tree[i].next)
{
int son=tree[i].to;
if(son==pre)
continue;
dp[son]+=(dp[root]-dp[son])+((tree[i].val)?-:);
dfs2(son,root);
}
}
int main()
{
int a,b;
while(scanf("%d",&n)!=EOF)
{
lne=;
memset(head,-,sizeof(head));
for(int i=;i<n-;i++)
{
scanf("%d%d",&a,&b);
add(a,b);
}
int k=,sum=;
memset(dp,,sizeof(dp));
dfs1(,);
dfs2(,);
for(int i=;i<=n;i++)
{
if(dp[i]<sum)
{
memset(city,,sizeof(city));
k=;
city[k++]=i;
sum=dp[i];
}
else if(dp[i]==sum)
city[k++]=i;
}
printf("%d\n",sum);
for(int i=;i<k;i++)
{
if(i==k-)
printf("%d\n",city[i]);
else printf("%d ",city[i]);
}
}
return ;
}
B:算表面积 先把每个除了四周都加起来 然后减去四周重复的
#include <bits/stdc++.h>
#include <cstring>
#include <iostream>
#include <algorithm>
#define foror(i,a,b) for(i=a;i<b;i++)
#define foror2(i,a,b) for(i=a;i>b;i--)
#define EPS 1.0e-6
#define PI acos(-1.0)
#define INF 3000000000
#define MOD 1000000009
#define mem(a,b) memset((a),b,sizeof(a))
#define TS printf("!!!\n")
#define lson o<<1, l, m
#define rson o<<1|1, m+1, r
//using ll = long long;
//using ull= unsigned long long;
//std::ios::sync_with_stdio(false);
using namespace std;
//priority_queue<int,vector<int>,greater<int>> que;
typedef long long ll;
int a[][];
char s[][];
int dir[][];
int main()
{
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
dir[][]=-,dir[][]=;
dir[][]=,dir[][]=;
dir[][]=,dir[][]=;
dir[][]=,dir[][]=-;
int n,m ;
cin >> n >> m;
mem(a,);
for(int i=;i<=n;i++)
{
scanf("%s",s[i]+);
for(int j=;j<=m;j++)
a[i][j]=s[i][j]-'';
}
int ans=;
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
{
ans+=a[i][j]*+;
if(a[i][j]==)
ans-=;
}
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
{
for(int w=;w<;w++)
{
int dx=i+dir[w][];
int dy=j+dir[w][];
ans-=min(a[i][j],a[dx][dy]);
}
}
cout<<ans<<endl;
return ;
}
C:DFS+二进制压缩
#include <bits/stdc++.h>
#include <cstring>
#include <iostream>
#include <algorithm>
#define foror(i,a,b) for(i=a;i<b;i++)
#define foror2(i,a,b) for(i=a;i>b;i--)
#define EPS 1.0e-6
#define PI acos(-1.0)
#define INF 3000000000
#define MOD 1000000009
#define mem(a,b) memset((a),b,sizeof(a))
#define TS printf("!!!\n")
#define lson o<<1, l, m
#define rson o<<1|1, m+1, r
//using ll = long long;
//using ull= unsigned long long;
//std::ios::sync_with_stdio(false);
using namespace std;
//priority_queue<int,vector<int>,greater<int>> que;
typedef long long ll;
int ans;
char s[][];
int dir[][];
int bit[];
int n,m;
int cal(int n)
{
int ans= ;
for (ans=;n;ans++)
{
n&=(n-);
}
return ans;
}
void dfs(int row,int pass,int now)
{
if(row==n)
{
int cur=max(pass,cal(now));
ans=min(ans,cur);
return ;
}
dfs(row+,pass+,now);
dfs(row+,pass,now|bit[row]);
}
int main()
{
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
dir[][]=-,dir[][]=;
dir[][]=,dir[][]=;
dir[][]=,dir[][]=;
dir[][]=,dir[][]=-;
cin >> n >> m;
ans=min(n,m);
for(int i=;i<n;i++)
{
scanf("%s",s[i]);
for(int j=;j<m;j++)
if(s[i][j]=='*')
bit[i]|=(<<j);
}
dfs(,,);
cout<<ans<<endl;
return ;
}
D:BFS+SG函数 :如果一个点是必胜点 那么他后面和前面的点一定不是必胜点 反之一个点不是必胜点 那么他前面和后面的点的后继如果有必胜点 则他不是必胜点 如果没有 则他是必胜点
while(set.find(x)!=set.end()) x++ 当set为空时 返回1 当set为0-y连续的时 返回的是y+1 当0-y有缺少时 返回的是第一个缺少的数
#include <bits/stdc++.h>
#include <cstring>
#include <iostream>
#include <algorithm>
#define foror(i,a,b) for(i=a;i<b;i++)
#define foror2(i,a,b) for(i=a;i>b;i--)
#define EPS 1.0e-6
#define PI acos(-1.0)
#define INF 3000000000
#define MOD 1000000009
#define mem(a,b) memset((a),b,sizeof(a))
#define TS printf("!!!\n")
#define lson o<<1, l, m
#define rson o<<1|1, m+1, r
//using ll = long long;
//using ull= unsigned long long;
//std::ios::sync_with_stdio(false);
using namespace std;
//priority_queue<int,vector<int>,greater<int>> que;
typedef long long ll;
vector<int> tree[];
//vector<int> newtree[1005];
int visit[];
int n,m;
int sg[];
queue<int> que;
void bfs()
{
visit[]=;
while(!que.empty())
{
int cur=que.front();
que.pop();
for(int i=;i<tree[cur].size();i++)
{
if(visit[tree[cur][i]]==)
{
visit[tree[cur][i]]=visit[cur]+;
que.push(tree[cur][i]);
}
}
}
}
/*void rebuild()
{
for(int i=1;i<=n;i++)
{
for(int j=0;j<tree[i].size();i++)
{
if(visit[tree[i][j]]>visit[i])
newtree[i].push_back(tree[i][j]);
}
}
}*/
int sgdfs(int x)
{
if(sg[x]!=-)
return sg[x];
int zero=;
//one=-1;
set<int> a;
for(int i=;i<tree[x].size();i++)
{
if(visit[tree[x][i]]>visit[x])
a.insert(sgdfs(tree[x][i]));
}
while(a.find(zero)!=a.end())
zero++;
sg[x]=zero;
return sg[x];
}
int main()
{
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
mem(sg,-);
mem(visit,);
cin >> n >> m;
int s,f;
for(int i=;i<=m;i++)
{
scanf("%d %d",&s,&f);
tree[s].push_back(f);
tree[f].push_back(s);
}
que.push();
bfs();
//rebuild();
int ans=sgdfs();
if(ans)
cout<<"Vladimir";
else
cout<<"Nikolay";
//for(int i=1;i<=n;i++)
//ans=max(ans,visit[i]);
return ;
}
F:浮标
因为间距的单调不会使得答案单调,间距过大过小都会使得最后的移动距离不是最优,所以是使用三分而不是二分
对于每次三分,每次在n个点里面假设一个点不动,然后枚举完算一遍,取最优。
while(fabs(r - l) > eps) {
double mid = (l + r) / 2.0;
double midd = (mid + r) / 2.0;
if(cal(mid, ) > cal(midd, )) l = mid;
else r = midd;
}
double ans = cal(l, ) > cal(r, ) ? r : l;
三分
#include <bits/stdc++.h>
using namespace std;
#define N 405
const double eps = 1e-;
double num[N];
int n, id; double cal(double x, int flag) {
double res = , ans = ;
for(int st = ; st <= n; st++) {
res = ;
for(int i = ; i < st; i++)
res += fabs(num[st] - (st - i) * x - num[i]);
for(int i = st + ; i <= n; i++)
res += fabs(num[st] + (i - st) * x - num[i]);
if(ans > res) ans = res, id = st;
}
return ans;
} void solve() {
scanf("%d", &n);
for(int i = ; i <= n; i++) scanf("%lf", &num[i]);
double l = , r = ;
while(fabs(r - l) > eps) {
double mid = (l + r) / 2.0;
double midd = (mid + r) / 2.0;
if(cal(mid, ) > cal(midd, )) l = mid;
else r = midd;
}
double ans = cal(l, ) > cal(r, ) ? r : l;
double res = cal(ans, );
printf("%.4f\n", res);
for(int i = ; i < id; i++)
printf("%.10f ", num[id] - (id - i) * ans);
printf("%.10f ", num[id]);
for(int i = id + ; i <= n; i++)
printf("%.10f ", num[id] + (i - id) * ans);
} int main() {
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
solve();
return ;
}
G:在线-->可持久化线段树(主席树 离线-->权值线段树
#include<bits/stdc++.h>
using namespace std;
const int maxn=;
struct {
int l,r,loca;
}tr[maxn*];
int tot,A[maxn],rt[maxn];
void pushup(int x){
tr[x].loca=min(tr[tr[x].l].loca,tr[tr[x].r].loca);
}
void insert(int &x,int l,int r,int a,int b){
tr[++tot]=tr[x];
x=tot;
if(l==r){
tr[x].loca=b;
return ;
}
int mid=(l+r)>>;
if(a<=mid)
insert(tr[x].l,l,mid,a,b);
else
insert(tr[x].r,mid+,r,a,b);
pushup(x);
}
int query(int x,int l,int r,int a){
if(l==r)return l;
int mid=(l+r)/;
if(tr[tr[x].l].loca<a)
return query(tr[x].l,l,mid,a);
else
return query(tr[x].r,mid+,r,a);
}
int main(){
int n,m;
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%d",&A[i]);
insert(rt[i]=rt[i-],,,A[i],i);
}
scanf("%d",&m);
while(m--){
int a,b;
scanf("%d%d",&a,&b);
printf("%d\n",query(rt[b],,,a));
}
}
I:莫队
K:可持久化并查集
Summer training #9的更多相关文章
- hdu 4946 2014 Multi-University Training Contest 8
Area of Mushroom Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- 2016 Multi-University Training Contests
2016 Multi-University Training Contest 1 2016 Multi-University Training Contest 2 2016 Multi-Univers ...
- 2016 Multi-University Training Contest 2 D. Differencia
Differencia Time Limit: 10000/10000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tot ...
- 2016 Multi-University Training Contest 1 G. Rigid Frameworks
Rigid Frameworks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- ACM: Gym 101047K Training with Phuket's larvae - 思维题
Gym 101047K Training with Phuket's larvae Time Limit:2000MS Memory Limit:65536KB 64bit IO F ...
- The Solution of UESTC 2016 Summer Training #1 Div.2 Problem C
Link http://acm.hust.edu.cn/vjudge/contest/121539#problem/C Description standard input/output After ...
- 2012 Multi-University Training Contest 9 / hdu4389
2012 Multi-University Training Contest 9 / hdu4389 打巨表,实为数位dp 还不太懂 先这样放着.. 对于打表,当然我们不能直接打,这里有技巧.我们可以 ...
- 2014 Multi-University Training Contest 9#11
2014 Multi-University Training Contest 9#11 Killing MonstersTime Limit: 2000/1000 MS (Java/Others) ...
- 2014 Multi-University Training Contest 9#6
2014 Multi-University Training Contest 9#6 Fast Matrix CalculationTime Limit: 2000/1000 MS (Java/Oth ...
- 2016 Multi-University Training Contest 1
8/11 2016 Multi-University Training Contest 1 官方题解 老年选手历险记 最小生成树+线性期望 A Abandoned country(BH) 题意: 1. ...
随机推荐
- 安装heat
在控制节点上执行 controller-heat(){mysql -uroot -p${MYSQL_PASSWD} << EOF DROP DATABASE IF EXISTS heat; ...
- java小技巧:如何分批次导入大量数据
//List 需要导入的数据int count = 1000;//每批次导入的数目int Lastindex = count;List<List<T>> shareList = ...
- [转帖]LINUX网络配置---nmtui&nmcli
LINUX网络配置---nmtui&nmcli https://blog.51cto.com/13625527/2151853?source=dra 两年前曾经打打印过 几页命令 里面就有 n ...
- 【Python】【demo实验24】【练习实例】【打印图样】
原题: 打印出如下图案(菱形): * *** ***** ******* ***** *** * 我的源码: #!/usr/bin/python # encoding=utf-8 # -*- codi ...
- CString VariantToString(VARIANT var);转换
Variant类型转换成CString代码 对数据库的操作常常会用Variant和_bstr_t类型,在进行MFC等程序中,通常会用到CString类.从variant转换成CString代码如下:v ...
- PHP中addslashes()和htmlspecialchars() 函数的区别及应用
addslashes()防sql注入: 定义如下: addslashes() 函数返回在预定义字符之前添加反斜杠的字符串. 预定义字符是: 单引号(') 双引号(") 反斜杠(\) NULL ...
- Jmeter之TCP取样器
1.在线程组中添加“TCP取样器” 2.填写数据 以下截图是必须配置的 TCPClient classname: 填写TCP报文格式(有三类),默认前缀:org.apache.jmeter.prot ...
- SQL With AS Expression
A. Creating a simple common table expression The following example shows the total number of sales o ...
- C#面向对象12 集合
ArrayList和HashTable集合 1.ArrayList集合 ***添加元素 using System; using System.Collections.Generic; using Sy ...
- 【原创】大叔经验分享(61)kudu rebalance报错
kudu rebalance命令报错 terminate called after throwing an instance of 'std::regex_error' what(): regex_e ...