2016 Multi-University Training Contest 7
6/12
2016 Multi-University Training Contest 7
期望 B Balls and Boxes(BH)
题意:
n个球放到m个盒子里,xi表示第i个盒子里的球的数量,求V的期望值。
思路:
官方题解:

代码:
#include <bits/stdc++.h>
typedef long long ll;
ll GCD(ll a, ll b) {
return b ? GCD (b, a % b) : a;
}
int main() {
ll n, m;
while (scanf ("%I64d%I64d", &n, &m) == 2 && n + m) {
ll x = n * (m - 1);
ll y = m * m;
ll gcd = GCD (x, y);
printf ("%I64d/%I64d\n", x / gcd, y / gcd);
}
return 0;
}
图论 E Elegant Construction(BH)
题意:
构造一张图,使得第i个点恰好能走到a[i]个点。
思路:
显然从a[i]=0的点开始,a[i]大的点一定会连向少的点,类似拓扑排序的做法。时间复杂度O(n^2)。
代码:
#include <bits/stdc++.h> const int N = 1e3 + 5;
int n; struct Edge {
int u, v;
};
std::vector<Edge> edges; struct Node {
int v, id;
bool operator < (const Node &rhs) const {
return v < rhs.v;
}
}a[N]; bool vis[N]; int solve() {
std::sort (a+1, a+1+n);
edges.clear ();
memset (vis, false, sizeof (vis));
for (int i=1; i<=n; ++i) {
if (a[i].v != 0) return -1;
vis[a[i].id] = true;
for (int j=1; j<=n; ++j) {
if (vis[a[j].id]) continue;
if (a[j].v == 0) continue;
edges.push_back ({a[j].id, a[i].id});
a[j].v--;
}
}
return (int) edges.size ();
} int main() {
int T;
scanf ("%d", &T);
for (int cas=1; cas<=T; ++cas) {
scanf ("%d", &n);
for (int i=1; i<=n; ++i) {
scanf ("%d", &a[i].v);
a[i].id = i;
}
int m = solve ();
printf ("Case #%d: %s\n", cas, m != -1 ? "Yes" : "No");
if (m != -1) {
printf ("%d\n", m);
for (int i=0; i<m; ++i) {
printf ("%d %d\n", edges[i].u, edges[i].v);
}
}
}
return 0;
}
优先队列+优化 J Joint Stacks(BH)
题意:
两个栈,A和B,三种操作:入栈,出栈,以及A,B按照入栈时间合并,输出每次出栈的数字。
思路:
可以用优先队列来模拟栈操作,合并的话就是按照时间排序后合并,时间复杂度应该是O(nlogn)(应该还要大),结果超时了。后来试过用并查集乱搞可是写搓了,一度想弃疗。。。后来再回到原来的做法,加了一个优化(队列小的合并到大的),就AC了!(也就是想到了并查集按秩合并的思想)
当然巧妙的做法还是官方的三个栈。

代码:
优先队列
#include <bits/stdc++.h> const int N = 1e5 + 5;
int n; struct Node {
int x, t;
bool operator < (const Node &rhs) const {
return t < rhs.t;
}
};
std::priority_queue<Node> pque[2]; int real[2]; void merge(int id1, int id2) {
if (pque[id2].size () > pque[id1].size ()) {
std::swap (id1, id2);
std::swap (real[0], real[1]);
}
while (!pque[id2].empty ()) {
pque[id1].push (pque[id2].top ());
pque[id2].pop ();
}
} int main() {
int cas = 0;
while (scanf ("%d", &n) == 1 && n) {
printf ("Case #%d:\n", ++cas);
char op[10], C[2], D[2];
for (int i=0; i<2; ++i) {
while (!pque[i].empty ()) pque[i].pop ();
}
real[0] = 0; real[1] = 1;
for (int i=1; i<=n; ++i) {
scanf ("%s", op);
if (op[0] == 'p') {
scanf ("%s", C);
int id = C[0] - 'A';
id = real[id];
if (op[1] == 'u') {
int x;
scanf ("%d", &x);
pque[id].push ({x, i});
} else {
printf ("%d\n", pque[id].top ().x);
pque[id].pop ();
}
} else {
scanf ("%s%s", C, D);
int id1 = C[0] - 'A';
int id2 = D[0] - 'A';
merge (real[id1], real[id2]);
}
}
}
return 0;
}
O(n)
void work()
{
top[0] = top[1] = top[2] = 0;
for (int i = 0; i < n; ++i) {
char op[10], s[5];
scanf("%s%s", op, s);
int a = s[0] - 'A';
if (op[1] == 'u') {
scanf("%d", &x[i]);
sta[a][top[a]++] = i;
} else if (op[1] == 'o') {
if (!top[a]) a = 2;
printf("%d\n", x[sta[a][--top[a]]]);
} else {
scanf("%s", s);
top[2] = merge(sta[0], sta[0] + top[0],
sta[1], sta[1] + top[1],
sta[2] + top[2]) - sta[2];
top[0] = top[1] = 0;
}
}
}
2016 Multi-University Training Contest 7的更多相关文章
- 2016 Al-Baath University Training Camp Contest-1
2016 Al-Baath University Training Camp Contest-1 A题:http://codeforces.com/gym/101028/problem/A 题意:比赛 ...
- 2016 Al-Baath University Training Camp Contest-1 E
Description ACM-SCPC-2017 is approaching every university is trying to do its best in order to be th ...
- 2016 Al-Baath University Training Camp Contest-1 A
Description Tourist likes competitive programming and he has his own Codeforces account. He particip ...
- 2016 Al-Baath University Training Camp Contest-1 J
Description X is fighting beasts in the forest, in order to have a better chance to survive he's gon ...
- 2016 Al-Baath University Training Camp Contest-1 I
Description It is raining again! Youssef really forgot that there is a chance of rain in March, so h ...
- 2016 Al-Baath University Training Camp Contest-1 H
Description You've possibly heard about 'The Endless River'. However, if not, we are introducing it ...
- 2016 Al-Baath University Training Camp Contest-1 G
Description The forces of evil are about to disappear since our hero is now on top on the tower of e ...
- 2016 Al-Baath University Training Camp Contest-1 F
Description Zaid has two words, a of length between 4 and 1000 and b of length 4 exactly. The word a ...
- 2016 Al-Baath University Training Camp Contest-1 D
Description X is well known artist, no one knows the secrete behind the beautiful paintings of X exc ...
- 2016 Al-Baath University Training Camp Contest-1 C
Description Rami went back from school and he had an easy homework about bitwise operations (and,or, ...
随机推荐
- MYSQL例题合集
一.数学函数 数学函数主要用于处理数字,包括整型.浮点数等. ABS(x) 返回x的绝对值 SELECT ABS(-1) -- 返回1 CEIL(x),CEILING(x) 返回大于或等于x的最小整数 ...
- [BZOJ1789][BZOJ1830][Ahoi2008]Necklace Y型项链
[BZOJ1789][BZOJ1830][Ahoi2008]Necklace Y型项链 试题描述 欢乐岛上众多新奇的游乐项目让小可可他们玩的非常开心.现在他们正在玩比赛串项链的游戏,谁串的最快就能得到 ...
- [BZOJ2303][Apio2011]方格染色
[BZOJ2303][Apio2011]方格染色 试题描述 Sam和他的妹妹Sara有一个包含n × m个方格的 表格.她们想要将其的每个方格都染成红色或蓝色. 出于个人喜好,他们想要表格中每个2 × ...
- WinAPI: ExtCreateRegion - 区域变换
转载:http://www.cnblogs.com/del/archive/2008/06/03/1212534.html 相似函数: SetWorldTransform 本例效果图: 代码文件: u ...
- JDBC之java数据库的连接与简单的sql语句执行
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sq ...
- QEMU 使用的镜像文件:qcow2 与 raw
qcow2 的基本原理 qcow2 镜像格式是 QEMU 模拟器支持的一种磁盘镜像.它也是可以用一个文件的形式来表示一块固定大小的块设备磁盘.与普通的 raw 格式的镜像相比,有以下特性: 更小的空间 ...
- SpringMVC请求处理流程
从web.xml中 servlet的配置开始, 根据servlet拦截的url-parttern,来进行请求转发 Spring MVC工作流程图 图一 图二 Spring工作流程描述 ...
- Python网络编程(2)——socket模块(2)
目录: 1. 异常 2. 地址族 3. 套接字类型 4. 模块方法 5. Socket对象与实例方法 socket模块提供了Python中的低层网络连接接口,用于操作套接字操作. 异常 socket模 ...
- 六间房 繁星 酷我 来疯 秀吧 新浪秀 直播播放器 Live 1.2
适合用于进行录制的时候 特别说明: 安装 falsh play 19 时 不能正常播放 每个按钮都有提示,不详细说明 下载地址 http://pan.baidu.com/s/1i32ETIt 下载地址 ...
- codeforces 483B Friends and Presents 解题报告
题目链接:http://codeforces.com/problemset/problem/483/B 题目意思:有两个 friends,需要将 cnt1 个不能整除 x 的数分给第一个friend, ...