码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces Round #285 (Div. 2) (A、B、C、D)

时间:2015-01-13 00:08:12      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:

A:就根据题意计算比较一下即可

B:从每个起点往后走一遍走到底,输出即可,字符串直接map映射掉

C:类似拓扑排序,从临接个数为1的入队,那么临接Xor和,其实就是他的上一个结点,因为他只临接了一个结点,这样利用拓扑排序,当一个结点的度数为1的时候入队即可,注意要判断一下度数0的情况,直接continue

D:利用树状数组去求这种大的全排列数,其实一个全排列 ,可以看成a1 * (n - 1)! + a2 * (n - 2)!....,那么其实只要处理出每一项的系数,然后在由系数就可以求出变换后的全排列了,那么求系数这一步,只需要把两个序列的系数加起来,然后进行进位操作,最后在转化回去即可,这个过程要利用树状数组来维护,因为每个位置都要查询,当前剩下数字中,比该数字小的数字个数

代码:

A:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

double a, b, c, d;

int main() {
    scanf("%lf%lf%lf%lf", &a, &b, &c, &d);
    double a1 = max(3 * a / 10, a - a / 250 * c);
    double a2 = max(3 * b / 10, b - b / 250 * d);
    if (a1 < a2) printf("Vasya\n");
    else if (a1 > a2) printf("Misha\n");
    else printf("Tie\n");
    return 0;
}

B:

#include <cstdio>
#include <cstring>
#include <string>
#include <map>
using namespace std;

int n, hn, vis[2005], to[2005];
map<string, int> hash;
char a[25], b[25];
char out[2005][25];

int get(char *str) {
    if (hash.count(str))
	return hash[str];
    strcpy(out[hn], str);
    hash[str] = hn++;
    return hash[str];
}

int dfs(int u) {
    while (1) {
	if (to[u] == -1) break;
	u = to[u];
    }
    return u;
}

int main() {
    scanf("%d", &n);
    hn = 0;
    memset(to, -1, sizeof(to));
    while (n--) {
	scanf("%s%s", a, b);
	int u = get(a), v = get(b);
	to[u] = v;
	vis[v] = 1;
    }
    int tot = 0;
    for (int i = 0; i < hn; i++)
	if (!vis[i]) {
	    tot++;
	}
    printf("%d\n", tot);
    for (int i = 0; i < hn ;i++)
	if (!vis[i]) {
	    printf("%s %s\n", out[i], out[dfs(i)]);
	}
    return 0;
}

C:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;

typedef pair<int, int> pii;

const int N = (1<<16) + 5;
int n, du[N], s[N];

vector<pii> ans;

int main() {
    scanf("%d", &n);
    queue<int> Q;
    for (int i = 0; i < n; i++) {
	scanf("%d%d", &du[i], &s[i]);
	if (du[i] == 1) Q.push(i);
    }
    while (!Q.empty()) {
	int u = Q.front();
	Q.pop();
	if (du[u] != 1) continue;
	ans.push_back(make_pair(u, s[u]));
	s[s[u]] ^= u;
	du[s[u]]--;
	if (du[s[u]] == 1) Q.push(s[u]);
    }
    int tot = ans.size();
    printf("%d\n", tot);
    for (int i = 0; i < tot; i++)
	printf("%d %d\n", ans[i].first, ans[i].second);
    return 0;
}

D:

#include <cstdio>
#include <cstring>

#define lowbit(x) (x&(-x))

const int N = 200005;

int n, a[N];

int bit[N];

void add(int x, int v) {
    while (x <= n) {
	bit[x] += v;
	x += lowbit(x);
    }
}

int query(int x) {
    int ans = 0;
    while (x) {
	ans += bit[x];
	x -= lowbit(x);
    }
    return ans;
}

int find(int x) {
    int l = 1, r = n;
    while (l < r) {
	int mid = (l + r) / 2;
	int tmp = query(mid);
	if (tmp < x) l = mid + 1;
	else r = mid;
    }
    return l;
}

int main() {
    scanf("%d", &n);
    memset(bit, 0, sizeof(bit));
    for (int i = 1; i <= n; i++) add(i, 1);
    int x;
    for (int i = 1; i <= n; i++) {
	scanf("%d", &x); x++;
	int tmp = query(x) - 1;
	add(x, -1);
	a[i] += tmp;
    }
    for (int i = 1; i <= n; i++) add(i, 1);
    for (int i = 1; i <= n; i++) {
	scanf("%d", &x); x++;
	int tmp = query(x) - 1;
	add(x, -1);
	a[i] += tmp;
    }
    for (int i = n; i >= 1; i--) {
	a[i - 1] += a[i] / (n - i + 1);
	a[i] %= (n - i + 1);
    }
    for (int i = 1; i <= n; i++) add(i, 1);
    for (int i = 1; i <= n; i++) {
	int tmp = find(a[i] + 1);
	printf("%d ", tmp - 1);
	add(tmp, -1);
    }
    printf("\n");
    return 0;
}


Codeforces Round #285 (Div. 2) (A、B、C、D)

标签:

原文地址:http://blog.csdn.net/accelerator_/article/details/42656671

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!