本文主要是介绍Codeforces Round #250 (Div. 2),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Codeforces Round #250 (Div. 2)
题目链接
A:水题,暴力判断有没有3个两倍长或两倍短的字符串,注意如果有多个答案也是属于C的情况
B:先打出lowbit的表,然后从大到小去组合即可
C:贪心,权值大的点优先拿掉
D:并查集+贪心,先把边都按权值从大到小排序,然后加边,该边可以被用到的次数为左边集合个数乘上右边集合个数,最后答案在除以C2n即可
代码:
A:
#include <stdio.h>
#include <string.h>char str[4][105];int main() {int i, j, flag = 0;char ans = 'C';for (i = 0; i < 4; i++)scanf("%s", str[i]);for (i = 0; i < 4; i++) {int s = 0, l = 0;for (j = 0; j < 4; j++) {if (i == j) continue;int a = strlen(str[i]) - 2;int b = strlen(str[j]) - 2;if (a * 2 <= b)s++;if (a >= b * 2)l++; }if (s == 3 || l == 3) {ans = i + 'A';flag++;}}if (flag != 1) ans = 'C';printf("%c\n", ans);return 0;
}
B:
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;const int N = 100001;
int sum, limit, ans[N], an = 0;
int i, j;
struct Low {int lowbit;int num;
} l[N];bool cmp(Low a, Low b) {return a.lowbit > b.lowbit;
}int main() {scanf("%d%d", &sum, &limit);for (i = 1; i < N; i++) {l[i].num = i;l[i].lowbit = (i&(-i));}sort(l + 1, l + N, cmp);for (i = 1; i < N; i++) {if (l[i].num <= limit) {if (sum >= l[i].lowbit) {ans[an++] = l[i].num;sum -= l[i].lowbit;}}if (sum == 0) break;}if (sum != 0) printf("-1\n"); else {printf("%d\n", an);for (int i = 0; i < an - 1; i++)printf("%d ", ans[i]);printf("%d\n", ans[an - 1]);}return 0;
}
C:
#include <stdio.h>
#include <string.h>
#include <vector>
#include <algorithm>
#define max(a,b) ((a)>(b)?(a):(b))
using namespace std;const int N = 1005;
int n, m, vis[N], val[N];
vector<int> g[N];
struct Node {int value;int id;
} node[N];bool cmp(Node a, Node b) {return a.value > b.value;
}int main() {int i, j;scanf("%d%d", &n, &m);for (i = 1; i <= n; i++) {scanf("%d", &node[i].value);node[i].id = i;val[i] = node[i].value;}int u, v;while (m--) {scanf("%d%d", &u, &v);g[u].push_back(v);g[v].push_back(u);}int ans = 0;sort(node + 1, node + 1 + n, cmp);for (i = 1; i <= n; i++) {int u = node[i].id;vis[u] = 1;for (j = 0; j < g[u].size(); j++) {int v = g[u][j];if (vis[v]) continue;ans += val[v];}}printf("%d\n", ans);return 0;
}
D:
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;const int N = 100005;
int n, m, node[N], parent[N], sum[N];struct Edge {int u, v, w;Edge(int u = 0, int v = 0, int w = 0) {this->u = u; this->v = v; this->w = w;}
} e[N];bool cmp(Edge a, Edge b) {return a.w > b.w;
}int find(int x) {if (x == parent[x]) return x;return parent[x] = find(parent[x]);
}int main() {scanf("%d%d", &n, &m);for (int i = 1; i <= n; i++) {parent[i] = i;sum[i] = 1;scanf("%d", &node[i]);}int u, v;for (int i = 0; i < m; i++) {scanf("%d%d", &u, &v);e[i] = Edge(u, v, min(node[u], node[v]));}sort(e, e + m, cmp);double ans = 0;for (int i = 0; i < m; i++) {int pa = find(e[i].u);int pb = find(e[i].v);if (pa != pb) {ans += (double)e[i].w * sum[pa] * sum[pb];parent[pb] = pa;sum[pa] += sum[pb];}}printf("%.6lf\n", ans * 2 / (n * 1.0 * (n - 1)));return 0;
}
这篇关于Codeforces Round #250 (Div. 2)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!