AIM Tech Round 3 (Div. 2) (B C D E) (codeforces 709B 709C 709D 709E)
rating又掉下去了。好不容易蓝了。。。。
A。。没读懂题,wa了好几次,明天问队友补上。。。
题意:一条直线上n个点x1,x2...xn,现在在位置a,求要经过任意n-1个点的最小路程
题解:分类讨论,乱搞搞总会出来的,我大概写的很笨……
#include <bits/stdc++.h>
using namespace std;
typedef long long ll; int x[]; ll cal(ll x, ll a, ll z) {
return min((a-x)*+(z-a) , (z-a)*+(a-x));
} int main() {
int n, a;
scanf("%d%d", &n, &a);
for (int i = ; i < n; ++i) {
scanf("%d", x+i);
}
sort(x, x+n);
ll ans = 1e18, tmp; if (n == ) cout << ;
else if (n == ) cout << min(abs(a-x[]), abs(a-x[]));
else if (a >= x[n-]) cout << a-x[];
else if (a <= x[]) cout << x[n-]-a;
else if (a <= x[]) cout << min(cal(x[], a, x[n-]), (ll)(x[n-]-a));
else if (a > x[n-]) cout << min(cal(x[], a, x[n-]), (ll)(a-x[]));
else cout << min(cal(x[],a,x[n-]), cal(x[],a,x[n-]));
return ;
}
题意:给一个字符串,要求替换任意一个非空字串,使得替换后字串字典序最小。替换的方法是z->y,y->x,....,b->a,a->z
题解:C题怎么会这么简单。。。很容易想到替换尽可能靠前的字符,而且不能替换a,注意题目要求的必须替换一个字串,所以字串中所有字母都是a的时候,只能把最后一个变成z了。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll; char a[];
int main() {
while (cin >> a) {
bool fg = false;
int n = strlen(a);
int cnt = ;
for (int i = ; i < n; ++i) {
if (a[i] != 'a') {a[i]--; fg=true;}
else if(fg) break;
}
if (!fg) a[n-] = 'z';
cout << a << endl;
} return ;
}
题意:对于一个只有0和1的序列,会有很多00,01,10,11的子序列,现在给你00,01,10,11的子序列的数量,求符合要求的子串,没有输出Impossible
题解:注意,子序列不同于子串,不连续。
设子串中0的数量是x,那么00的数量一定是C(2,x),1同理。然后设0的数量是x,1的数量是y,所以所有子序列的个数是C(2,x+y),即C(2,x+y)==a+b+c+d,如果不相等则不可能构成。要注意的地方是当00的数量为0的时候,x既可以为0,也可以为1,需要根据bc判断。y同理。
至于字符串的构造,有点贪心的想法。瞎搞的。。。。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
char ans[];
int main() {
ll a, b, c, d;
while (cin >> a >> b >> c >> d) {
ll x = (ll)sqrt(a*)+;
ll y = (ll)sqrt(d*)+;
if (a+b+c+d == ) {
puts("");
continue;
}
if (a == ) {
if (b || c) x = ; else x = ;
}
if (d == ) {
if (b || c) y = ; else y = ;
}
if (!(x*(x-) == a* && y*(y-) == d*)) {
puts("Impossible");
continue;
}
ll n = x+y;
ll xnt = n*(n-)/-a-d;
if (b+c != xnt) {
puts("Impossible");
continue;
}
int idx = ;
int cnt = x+y;
while (cnt) {
if (b < y) {
ans[idx++] = '';
c -= x;
y--;
} else if (c < x) {
ans[idx++] = '';
b -= y;
x--;
} else if (b >= c) {
ans[idx++] = '';
b -= y;
x--;
} else {
ans[idx++] = '';
c -= x;
y--;
}
cnt--;
}
ans[idx] = ;
printf("%s\n", ans);
}
return ;
}
题意:给一棵树,对于每一个结点,可不可以通过在树上去掉一条边再增加一条边(必须还是一棵树),使这个结点成为这棵树的重心。
题解:%%%明神
对于一个点,如果不能当重心,一定是它有一棵子树u,size[u]>n/2,那么只要在这个子树u中找到一个子树v,满足size[v]<=n/2且size[u]-size[v]<=n/2就可以了。
于是树形dp,求每个点的<=n/2的最大子树的大小。
至于向上思想类似,随便搞搞,具体看代码。
不喜欢用邻接表存图,看起来很麻烦,可是vector确实会慢一些,比赛一定不能用=。=
#include <bits/stdc++.h> using namespace std; typedef long long ll;
const int N = ; vector<int> g[N];
int sz[N], up[N], dn[N]; // up向上的最大的不大于n/2的子树大小 dn向下的
int ans[N];
int n; inline int read()
{
char ch = getchar();
int data = ;
while (ch < '' || ch > '')
ch = getchar();
do {
data = data* + ch-'';
ch = getchar();
} while (ch >= '' && ch <= '');
return data;
} void UP(int &x, int y) { if(y>x) x=y; }
void dfs(int u, int fa) { // 第一遍向下dfs
sz[u] = ;
for (int i = ; i < g[u].size(); ++i) {
int v = g[u][i];
if (v == fa) continue;
dfs(v, u);
if (sz[v] - dn[v] > n/) ans[u] = ;
sz[u] += sz[v];
UP(dn[u], dn[v]);
}
if (sz[u] <= n/) dn[u] = sz[u];
}
void dfs1(int u, int fa) { // 第二遍向上
if (n-sz[u] - up[u] > n/) ans[u] = ;
if (n-sz[u] <= n/) up[u] = n-sz[u]; int mx = ; // 对于每一个结点向上的最大值 可能来自向上的链 也可能来自父节点的其他子树
// mx记录的就父节点其他子树的最大值
for (int i = ; i < g[u].size(); ++i) {
int v = g[u][i];
if (v == fa) continue;
up[v] = max(up[u], mx);
UP(mx, dn[v]);
}
mx = ;
for (int i = g[u].size()-; i >= ; --i) {
int v = g[u][i];
if (v == fa) continue;
UP(up[v], mx);
UP(mx, dn[v]);
dfs1(v, u);
}
} int main() {
scanf("%d", &n);
int u, v;
for (int i = ; i < n; ++i) {
u = read(); v = read();
g[u].push_back(v);
g[v].push_back(u);
}
if (n == ) { printf("1 1"); return ; } for (int i = ; i <= n; ++i) ans[i] = ;
dfs(, );
dfs1(, );
for (int i = ; i <= n; ++i) {
printf("%d", ans[i]);
if (i != n) printf(" ");
}
return ;
}
AIM Tech Round 3 (Div. 2) (B C D E) (codeforces 709B 709C 709D 709E)的更多相关文章
- codeforce AIM tech Round 4 div 2 B rectangles
2017-08-25 15:32:14 writer:pprp 题目: B. Rectangles time limit per test 1 second memory limit per test ...
- AIM Tech Round 3 (Div. 2)
#include <iostream> using namespace std; ]; int main() { int n, b, d; cin >> n >> ...
- AIM Tech Round 3 (Div. 2) A B C D
虽然打的时候是深夜但是状态比较好 但还是犯了好多错误..加分场愣是打成了降分场 ABC都比较水 一会敲完去看D 很快的就想出了求0和1个数的办法 然后一直wa在第四组..快结束的时候B因为低级错误被h ...
- AIM Tech Round 3 (Div. 2) B
Description Vasya takes part in the orienteering competition. There are n checkpoints located along ...
- AIM Tech Round 3 (Div. 2) A
Description Kolya is going to make fresh orange juice. He has n oranges of sizes a1, a2, ..., an. Ko ...
- AIM Tech Round 3 (Div. 2) B 数学+贪心
http://codeforces.com/contest/709 题目大意:给一个一维的坐标轴,上面有n个点,我们刚开始在位置a,问,从a点开始走,走n-1个点所需要的最小路程. 思路:我们知道,如 ...
- AIM Tech Round 3 (Div. 2)D. Recover the String(贪心+字符串)
D. Recover the String time limit per test 1 second memory limit per test 256 megabytes input standar ...
- AIM Tech Round 4 (Div. 2)ABCD
A. Diversity time limit per test 1 second memory limit per test 256 megabytes input standard input o ...
- AIM Tech Round 4 (Div. 2)(A,暴力,B,组合数,C,STL+排序)
A. Diversity time limit per test:1 second memory limit per test:256 megabytes input:standard input o ...
随机推荐
- 2013流行Python项目汇总
2013流行Python项目汇总 转自:http://www.kankanews.com/ICkengine/archives/102963.shtml Python作为程序员的宠儿,越来越得到人们的 ...
- How to Cope with Deadlocks
http://dev.mysql.com/doc/refman/5.0/en/innodb-deadlocks.html How to Cope with Deadlocks This section ...
- Ubuntu环境下手动配置openSSH
配置openSSH 1.手动下载压缩文件(.tar.gz) zlib-1.2.7.tar.gz openssl-1.0.1j.tar.gz openssh-6.0p1.tar.gz 2.安装zlib ...
- 【BZOJ 1045】 1045: [HAOI2008] 糖果传递
1045: [HAOI2008] 糖果传递 Description 有n个小朋友坐成一圈,每人有ai个糖果.每人只能给左右两人传递糖果.每人每次传递一个糖果代价为1. Input 第一行一个正整数n& ...
- Android 监听EditView中的文本改变事件
android中的编辑框EditText也比较常用,那比如在搜索框中,没输入一个字,下面的搜索列表就显示有包含输入关键字的选项,这个输入监听怎么实现的呢? 我们可以建一个例子,效果图如下: 我们可以监 ...
- ADT开发中的一些优化设置:代码背景色、代码字体大小、代码自动补全
初学Android开发,在网上找到一些ADT工具的优化,自己设置好了,截图保存下来.免得以后忘了. 1. 设置背景颜色: 色调85.饱和度90.亮度205 RGB:199.237.204 2. 设置代 ...
- Python图
从一位前辈的博客看到了一张图,先转过来,稍后再细看
- git log
http://git-scm.com/book/zh/v2 https://backlogtool.com/git-guide/tw/contents/ http://gitbook.liuh ...
- sdut 1570 c旅行
用搜索(bfs,dfs)做了半天,都超时,原来是dp; 参考博客:http://www.cnblogs.com/liuzezhuang/archive/2012/07/29/2613820.html ...
- ehcache 分布式集群同步数据实例
本文使用rmi方式,借鉴百度能搜到的文章,但是均不能做到数据同步,做了些改动完全没问题,更详细说明介绍百度即可.直奔主题,可运行的demo实例! 创建一个maven项目,配置pom pom.xml & ...