6/12

2016 Multi-University Training Contest 7

期望 B Balls and Boxes(BH)

题意:

  n个球放到m个盒子里,xi表示第i个盒子里的球的数量,求V的期望值。

思路:

  官方题解:

代码:

  1. #include <bits/stdc++.h>
  2.  
  3. typedef long long ll;
  4.  
  5. ll GCD(ll a, ll b) {
  6. return b ? GCD (b, a % b) : a;
  7. }
  8.  
  9. int main() {
  10. ll n, m;
  11. while (scanf ("%I64d%I64d", &n, &m) == 2 && n + m) {
  12. ll x = n * (m - 1);
  13. ll y = m * m;
  14. ll gcd = GCD (x, y);
  15. printf ("%I64d/%I64d\n", x / gcd, y / gcd);
  16. }
  17. return 0;
  18. }  

图论 E Elegant Construction(BH)

题意:

  构造一张图,使得第i个点恰好能走到a[i]个点。

思路:

  显然从a[i]=0的点开始,a[i]大的点一定会连向少的点,类似拓扑排序的做法。时间复杂度O(n^2)。

代码:

  1. #include <bits/stdc++.h>
  2.  
  3. const int N = 1e3 + 5;
  4. int n;
  5.  
  6. struct Edge {
  7. int u, v;
  8. };
  9. std::vector<Edge> edges;
  10.  
  11. struct Node {
  12. int v, id;
  13. bool operator < (const Node &rhs) const {
  14. return v < rhs.v;
  15. }
  16. }a[N];
  17.  
  18. bool vis[N];
  19.  
  20. int solve() {
  21. std::sort (a+1, a+1+n);
  22. edges.clear ();
  23. memset (vis, false, sizeof (vis));
  24. for (int i=1; i<=n; ++i) {
  25. if (a[i].v != 0) return -1;
  26. vis[a[i].id] = true;
  27. for (int j=1; j<=n; ++j) {
  28. if (vis[a[j].id]) continue;
  29. if (a[j].v == 0) continue;
  30. edges.push_back ({a[j].id, a[i].id});
  31. a[j].v--;
  32. }
  33. }
  34. return (int) edges.size ();
  35. }
  36.  
  37. int main() {
  38. int T;
  39. scanf ("%d", &T);
  40. for (int cas=1; cas<=T; ++cas) {
  41. scanf ("%d", &n);
  42. for (int i=1; i<=n; ++i) {
  43. scanf ("%d", &a[i].v);
  44. a[i].id = i;
  45. }
  46. int m = solve ();
  47. printf ("Case #%d: %s\n", cas, m != -1 ? "Yes" : "No");
  48. if (m != -1) {
  49. printf ("%d\n", m);
  50. for (int i=0; i<m; ++i) {
  51. printf ("%d %d\n", edges[i].u, edges[i].v);
  52. }
  53. }
  54. }
  55. return 0;
  56. }

优先队列+优化 J Joint Stacks(BH)

题意:

  两个栈,A和B,三种操作:入栈,出栈,以及A,B按照入栈时间合并,输出每次出栈的数字。

思路:

  可以用优先队列来模拟栈操作,合并的话就是按照时间排序后合并,时间复杂度应该是O(nlogn)(应该还要大),结果超时了。后来试过用并查集乱搞可是写搓了,一度想弃疗。。。后来再回到原来的做法,加了一个优化(队列小的合并到大的),就AC了!(也就是想到了并查集按秩合并的思想)

  当然巧妙的做法还是官方的三个栈。

代码:

优先队列

  1. #include <bits/stdc++.h>
  2.  
  3. const int N = 1e5 + 5;
  4. int n;
  5.  
  6. struct Node {
  7. int x, t;
  8. bool operator < (const Node &rhs) const {
  9. return t < rhs.t;
  10. }
  11. };
  12. std::priority_queue<Node> pque[2];
  13.  
  14. int real[2];
  15.  
  16. void merge(int id1, int id2) {
  17. if (pque[id2].size () > pque[id1].size ()) {
  18. std::swap (id1, id2);
  19. std::swap (real[0], real[1]);
  20. }
  21. while (!pque[id2].empty ()) {
  22. pque[id1].push (pque[id2].top ());
  23. pque[id2].pop ();
  24. }
  25. }
  26.  
  27. int main() {
  28. int cas = 0;
  29. while (scanf ("%d", &n) == 1 && n) {
  30. printf ("Case #%d:\n", ++cas);
  31. char op[10], C[2], D[2];
  32. for (int i=0; i<2; ++i) {
  33. while (!pque[i].empty ()) pque[i].pop ();
  34. }
  35. real[0] = 0; real[1] = 1;
  36. for (int i=1; i<=n; ++i) {
  37. scanf ("%s", op);
  38. if (op[0] == 'p') {
  39. scanf ("%s", C);
  40. int id = C[0] - 'A';
  41. id = real[id];
  42. if (op[1] == 'u') {
  43. int x;
  44. scanf ("%d", &x);
  45. pque[id].push ({x, i});
  46. } else {
  47. printf ("%d\n", pque[id].top ().x);
  48. pque[id].pop ();
  49. }
  50. } else {
  51. scanf ("%s%s", C, D);
  52. int id1 = C[0] - 'A';
  53. int id2 = D[0] - 'A';
  54. merge (real[id1], real[id2]);
  55. }
  56. }
  57. }
  58. return 0;
  59. }

O(n)

  1. void work()
  2. {
  3. top[0] = top[1] = top[2] = 0;
  4. for (int i = 0; i < n; ++i) {
  5. char op[10], s[5];
  6. scanf("%s%s", op, s);
  7. int a = s[0] - 'A';
  8. if (op[1] == 'u') {
  9. scanf("%d", &x[i]);
  10. sta[a][top[a]++] = i;
  11. } else if (op[1] == 'o') {
  12. if (!top[a]) a = 2;
  13. printf("%d\n", x[sta[a][--top[a]]]);
  14. } else {
  15. scanf("%s", s);
  16. top[2] = merge(sta[0], sta[0] + top[0],
  17. sta[1], sta[1] + top[1],
  18. sta[2] + top[2]) - sta[2];
  19. top[0] = top[1] = 0;
  20. }
  21. }
  22. }

2016 Multi-University Training Contest 7的更多相关文章

  1. 2016 Al-Baath University Training Camp Contest-1

    2016 Al-Baath University Training Camp Contest-1 A题:http://codeforces.com/gym/101028/problem/A 题意:比赛 ...

  2. 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 ...

  3. 2016 Al-Baath University Training Camp Contest-1 A

    Description Tourist likes competitive programming and he has his own Codeforces account. He particip ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

  10. 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, ...

随机推荐

  1. 淘宝(阿里百川)手机客户端开发日记第五篇 SharedPreferences使用详解

    我们知道,Android中数据存储技术由于如下几种 1 使用SharedPreferences存储数据 2 文件存储数据 3 SQLite数据库存储数据 4 使用ContentProvider存储数据 ...

  2. Linux下安装配置MongoDB 3.0.x 版本数据库

    说明: 操作系统:CentOS 5.X 64位 IP地址:192.168.21.128 实现目的: 安装配置MongoDB数据库 具体操作: 一.关闭SElinux.配置防火墙 1.vi /etc/s ...

  3. 《OpenCV入门》(三)

    这部分主要讲形态学的,回头把代码跑跑再来说下代码的感受:http://blog.csdn.net/poem_qianmo/article/details/24599073

  4. FOJ 1205

    Problem 1205 小鼠迷宫问题 Accept: 522    Submit: 1679 Time Limit: 1000 mSec    Memory Limit : 32768 KB Pro ...

  5. 中位数与第K小元素

    算法实际上是模仿快速排序算法设计出来的,其基本思想也是对输入数组进行递归划分,与快速排序不同的是,它只对划分出来的子数组之一进行递归处理: int randompartition(int a[],in ...

  6. Spring事务传播、隔离等级

    事务传播 PROPAGATION_REQUIRED 如果当前没有事务,就新建一个事务,如果已经存在一个事务中,加入到这个事务中.这是最常见的选择. PROPAGATION_SUPPORTS 支持当前事 ...

  7. Java字符串split函数的注意事项

    Java字符串的split方法可以分割字符串,但和其他语言不太一样,split方法的参数不是单个字符,而是正则表达式,如果输入了竖线(|)这样的字符作为分割字符串,会出现意想不到的结果, 如, Str ...

  8. localResizeIMG

    http://think2011.net/localResizeIMG/test/ 演示一下 自己试试 点我直接进入演示页面 说明 在客户端压缩好要上传的图片可以节省带宽更快的发送给后端,特别适合在移 ...

  9. 新浪网易淘宝等IP地区信息查询开放API接口调用方法

    通过IP地址获取对应的地区信息通常有两种方法:1)自己写程序,解析IP对应的地区信息,需要数据库.2)根据第三方提供的API查询获取地区信息. 第一种方法,参见文本<通过纯真IP数据库获取IP地 ...

  10. Dan计划:重新定义人生的10000个小时

    一. 1985年,芝加哥大学的Benjamin Bloom教授,出版了一本重要著作<如何培养天才>(Developing Talent in Young People). 他研究的是,如何 ...