Codeforces Round #261 (Div. 2)[ABCDE]

ACM

题目地址:Codeforces Round #261 (Div. 2)

A - Pashmak and Garden

题意: 

一个正方形,它的边平行于坐标轴,给出这个正方形的两个点,求出另外两个点。

分析: 

推断下是否平行X轴或平行Y轴,各种if。

代码:

  1. /*
  2. * Author: illuz <iilluzen[at]gmail.com>
  3. * File: A.cpp
  4. * Create Date: 2014-08-15 23:35:17
  5. * Descripton:
  6. */
  7.  
  8. #include <cstdio>
  9. #include <cstring>
  10. #include <iostream>
  11. #include <algorithm>
  12. using namespace std;
  13.  
  14. const int N = 0;
  15.  
  16. int main() {
  17. int x1, y1, x2, y2, x3, y3, x4, y4;
  18. int a;
  19. while (cin >> x1 >> y1 >> x2 >> y2) {
  20. if (x1 == x2) {
  21. a = y1 - y2;
  22. cout << x1 + a << ' ' << y1 << ' ' << x2 + a << ' ' << y2 << endl;
  23. } else if (y1 == y2) {
  24. a = x1 - x2;
  25. cout << x1 << ' ' << y1 + a << ' ' << x2 << ' ' << y2 + a << endl;
  26. } else {
  27. if (abs(x1 - x2) != abs(y1 - y2)) {
  28. cout << -1 << endl;
  29. continue;
  30. }
  31. cout << x1 << ' ' << y2 << ' ' << x2 << ' ' << y1 << endl;
  32. }
  33. }
  34. return 0;
  35. }


B - Pashmak and Flowers

题意: 

在n个数中取出两个数,使得差值最大,问差值和有几种取法。 

两种取法不同当且仅当:两种方法至少有一个不同位置的数。

分析:

非常明显差值就是最大-最小

假设两个数不是同样的,那么取法就是max_cnt * min_cnt了。

假设同样就要注意了,由于max_cnt * min_cnt里面有一些取法一样的数。 

比方:5 1 1 1 1 1。

  1. 那么我们能够这样考虑。第一次能够取5种,第二次能够取(5-1)钟,可是这里面(i,j)和(j,i)都取过,所以得减半。所以结果就是n*(n-1)/2

  2. 或者能够这样考虑。我们为了不要取反复。规定第一次取的位置肯定在第二次前面。假设第一次取pos1,那么下次仅仅能取(n-1)钟;假设第一次取pos2。第二次就(n-2)....累计就是(n-1)*n/2了。

代码:

  1. /*
  2. * Author: illuz <iilluzen[at]gmail.com>
  3. * File: B.cpp
  4. * Create Date: 2014-08-15 23:51:15
  5. * Descripton:
  6. */
  7.  
  8. #include <cstdio>
  9. #include <cstring>
  10. #include <iostream>
  11. #include <algorithm>
  12. using namespace std;
  13.  
  14. #define repf(i,a,b) for(int i=(a);i<=(b);i++)
  15. typedef long long ll;
  16.  
  17. const int N = 2e5 + 10;
  18.  
  19. ll t, mmax, mmin;
  20. ll a[N];
  21.  
  22. int main() {
  23. while (cin >> t) {
  24. repf (i, 0, t - 1) {
  25. cin >> a[i];
  26. }
  27. sort (a, a + t);
  28. if (a[0] == a[t - 1]) {
  29. cout << 0 << ' ' << t * (t - 1) / 2 << endl;
  30. continue;
  31. }
  32.  
  33. mmax = 0;
  34. mmin = 0;
  35. int i = 0;
  36. while (i < t && a[i] == a[0])
  37. mmin++, i++;
  38.  
  39. i = t - 1;
  40. while (i >= 0 && a[i] == a[t - 1])
  41. mmax++, i--;
  42.  
  43. cout << a[t - 1] - a[0] << ' ' << mmin * mmax << endl;
  44. }
  45. return 0;
  46. }


C - Pashmak and Buses

题意: 

n个人坐车,有k辆车带他们去d个地方玩。

问怎么安排使得这d天他们没有一对人一直在一起的(FFF团的胜利)。

分析: 

相当于:d行n列,每一个位置填一个1~k的整数。要求不能有两列全然一样。 

爆搜过去即可。仅仅要有解即可了。

代码:

  1. /*
  2. * Author: illuz <iilluzen[at]gmail.com>
  3. * File: C.cpp
  4. * Create Date: 2014-08-16 00:47:18
  5. * Descripton:
  6. */
  7.  
  8. #include<cmath>
  9. #include<cstdio>
  10. #include<string>
  11. #include<cstring>
  12. #include<iostream>
  13. #include<algorithm>
  14. using namespace std;
  15.  
  16. const int N = 1110;
  17.  
  18. int a[N], sum;
  19. int n, d, k, m[N][N];
  20.  
  21. void dfs(int x) {
  22. if(sum >= n)
  23. return;
  24. if(x >= d) {
  25. for (int i = 0; i < d; i++)
  26. m[i][sum] = a[i];
  27. sum++;
  28. return;
  29. }
  30. for(int i = 1; i <= min(k, 1001); i++) {
  31. a[x] = i;
  32. dfs(x + 1);
  33. }
  34. }
  35.  
  36. int main() {
  37. while (~scanf("%d%d%d", &n, &k, &d)) {
  38. memset(m, 0, sizeof(m));
  39. sum = 0;
  40. dfs(0);
  41. if(sum < n)
  42. puts("-1");
  43. else {
  44. for(int i = 0; i < d; i++) {
  45. for(int j = 0; j < n; j++)
  46. printf("%d ", m[i][j]);
  47. puts("");
  48. }
  49. }
  50. }
  51. return 0;
  52. }


D - Pashmak and Parmida's problem

题意: 

给出一些数a[n]。求(i, j),i<j的数量。使得:f(1,
i, a[i]) > f(j, n, a[j])


f(lhs, rhs, x)指在{
[lhs, rhs]范围中,a[k]的值=x }
的数量。

分析: 

非常明显: 

1. f(1, i, a[i])就是指a[i]前面包含a[i]的数中。有几个值=a[i]。

2. f(j, n, a[j])就是指a[j]后面包含a[j]的数中有几个值=a[j]。

尽管a[x]范围不小。可是n的范围是1000。不是非常大,所以我们能够用map预处理出f(1, i, a[i])f(j,
n, a[j])
。记为s1[n], s2[n]。

这样就变成求满足s1[i] > s[j]。 i < j情况的数量了,你会发现跟求逆序对一样了。

这时就能够用线段树或树状数组求逆序数对的方法解决问题了。不懂线段树怎么解的能够看:HDU
1394 Minimum Inversion Number(线段树求最小逆序数对)

代码:

  1. /*
  2. * Author: illuz <iilluzen[at]gmail.com>
  3. * File: D.cpp
  4. * Create Date: 2014-08-16 00:18:08
  5. * Descripton:
  6. */
  7.  
  8. #include <cstdio>
  9. #include <cstring>
  10. #include <iostream>
  11. #include <algorithm>
  12. #include <map>
  13. using namespace std;
  14.  
  15. #define rep(i,n) for(int i=0;i<(n);i++)
  16. #define repu(i,a,b) for(int i=(a);i<(b);i++)
  17. #define repd(i,a,b) for(int i=(a);i>=(b);i--)
  18. typedef long long ll;
  19. #define lson(x) ((x) << 1)
  20. #define rson(x) ((x) << 1 | 1)
  21.  
  22. const int N = 1e6 + 10;
  23.  
  24. const int ROOT = 1;
  25.  
  26. // below is sement point updated version
  27. struct seg {
  28. ll w;
  29. };
  30.  
  31. struct segment_tree {
  32. seg node[N << 2];
  33.  
  34. void update(int pos) {
  35. node[pos].w = node[lson(pos)].w + node[rson(pos)].w;
  36. }
  37.  
  38. void build(int l, int r, int pos) {
  39. if (l == r) {
  40. node[pos].w = 0;
  41. return;
  42. }
  43. int m = (l + r) >> 1;
  44. build(l, m, lson(pos));
  45. build(m + 1, r, rson(pos));
  46. update(pos);
  47. }
  48.  
  49. // add the point x with y
  50. void modify(int l, int r, int pos, int x, ll y) {
  51. if (l == r) {
  52. node[pos].w += y;
  53. return;
  54. }
  55. int m = (l + r) >> 1;
  56. if (x <= m)
  57. modify(l, m, lson(pos), x, y);
  58. else
  59. modify(m + 1, r, rson(pos), x, y);
  60. update(pos);
  61. }
  62.  
  63. // query the segment [x, y]
  64. ll query(int l, int r, int pos, int x, int y) {
  65. if (x <= l && r <= y)
  66. return node[pos].w;
  67. int m = (l + r) >> 1;
  68. ll res = 0;
  69. if (x <= m)
  70. res += query(l, m, lson(pos), x, y);
  71. if (y > m)
  72. res += query(m + 1, r, rson(pos), x, y);
  73. return res;
  74. }
  75. } sgm;
  76.  
  77. ll t, a[N];
  78. int s1[N], s2[N];
  79.  
  80. map<ll, int> mp;
  81.  
  82. int main() {
  83. while (cin >> t) {
  84. mp.clear();
  85. rep (i, t) {
  86. cin >> a[i];
  87. mp[a[i]]++;
  88. s1[i] = mp[a[i]];
  89. }
  90. mp.clear();
  91. for (int i = t - 1; i >= 0; i--) {
  92. mp[a[i]]++;
  93. s2[i] = mp[a[i]];
  94. }
  95. sgm.build(1, t, ROOT);
  96. ll ans = 0;
  97. rep (i, t) {
  98. ans += sgm.query(1, t, ROOT, s2[i] + 1, t);
  99. sgm.modify(1, t, ROOT, s1[i], 1);
  100. //cout << s1[i] << ' ' << s2[i] << ' ' << ans << endl;
  101. }
  102. cout << ans << endl;
  103. }
  104. return 0;
  105. }


E - Pashmak and Graph

题意: 

给出一个有向带权值的图,要求出最长递增链路的长度。

也就是当前边的权值要大于前一条边的。

分析: 

刚開始写了个搜索+map记忆化,然后就TLE了QvQ... 

事实上能够用数组的dp来做,先对边从小到大排序。从小到达处理,对于同样的一类边。进行对边dp。然后更新对点dp。

@barty巨巨

将全部边按边权从小到大排序,顺序扫描,假设没有反复边权的话,对于(u, v, d)这条有向边,能够直接用之前求的到u点的最长路径+1来更新到v的最长路径。 

只是题目中没有保证全部边权不同,为了保证严格递增。所以对于同样边权须要做一个缓冲处理。

代码:

  1. /*
  2. * Author: illuz <iilluzen[at]gmail.com>
  3. * Blog: http://blog.csdn.net/hcbbt
  4. * File: E.cpp
  5. * Create Date: 2014-08-16 09:43:59
  6. * Descripton:
  7. */
  8.  
  9. #include <iostream>
  10. #include <cstdio>
  11. #include <cstring>
  12. #include <algorithm>
  13. using namespace std;
  14. #define repf(i,a,b) for(int i=(a);i<=(b);i++)
  15.  
  16. const int N = 3e5 + 10;
  17.  
  18. struct Edge {
  19. int x;
  20. int y;
  21. int w;
  22. bool operator <(const Edge& e) const {
  23. return w < e.w;
  24. }
  25. } e[N];
  26.  
  27. int n, m;
  28. int edge[N], node[N]; // edges and nodes' dp
  29.  
  30. int main() {
  31. while (~scanf("%d%d", &n, &m)) {
  32. memset(edge, 0, sizeof(edge));
  33. memset(node, 0, sizeof(node));
  34.  
  35. repf (i, 1, m) {
  36. scanf("%d%d%d", &e[i].x, &e[i].y, &e[i].w);
  37. }
  38.  
  39. sort(e + 1, e + m + 1);
  40.  
  41. repf (i, 1, m) {
  42. int j = i;
  43. while (j <= m && e[i].w == e[j].w) { // update edges' dp
  44. int x = e[j].x;
  45. edge[j] = max(edge[j], node[x] + 1);
  46. j++;
  47. }
  48.  
  49. j = i;
  50. while (j <= m && e[i].w == e[j].w) { // update nodes' dp
  51. int y = e[j].y;
  52. node[y] = max(edge[j], node[y]);
  53. j++;
  54. }
  55. i = j - 1;
  56. }
  57.  
  58. int ans = 0;
  59. repf (i, 1, m)
  60. ans = max(ans, edge[i]);
  61.  
  62. printf("%d\n", ans);
  63. }
  64. return 0;
  65. }


Codeforces Round #261 (Div. 2)[ABCDE]的更多相关文章

  1. Codeforces Round #279 (Div. 2) ABCDE

    Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems     # Name     A Team Olympiad standard input/outpu ...

  2. Codeforces Round #261 (Div. 2) B

    链接:http://codeforces.com/contest/459/problem/B B. Pashmak and Flowers time limit per test 1 second m ...

  3. Codeforces Round #261 (Div. 2) E. Pashmak and Graph DP

    http://codeforces.com/contest/459/problem/E 不明确的是我的代码为啥AC不了,我的是记录we[i]以i为结尾的点的最大权值得边,然后wa在第35  36组数据 ...

  4. Codeforces Round #261 (Div. 2)459D. Pashmak and Parmida&#39;s problem(求逆序数对)

    题目链接:http://codeforces.com/contest/459/problem/D D. Pashmak and Parmida's problem time limit per tes ...

  5. Codeforces Round #460 (Div. 2) ABCDE题解

    原文链接http://www.cnblogs.com/zhouzhendong/p/8397685.html 2018-02-01 $A$ 题意概括 你要买$m$斤水果,现在有$n$个超市让你选择. ...

  6. Codeforces Round #546 (Div. 2) ABCDE 题解

    1136A: 题意:一本书有n个章节,每个章节的分别在li到ri页,小明读完书后将书折在第k页,问还有多少章节没有读 题解:控制k在li~ri的范围内后输出n-i即可 #include <set ...

  7. Codeforces Round #261 (Div. 2) - E (459E)

    题目连接:http://codeforces.com/contest/459/problem/E 题目大意:给定一张有向图,无自环无重边,每条边有一个边权,求最长严格上升路径长度.(1≤n,m≤3 * ...

  8. Codeforces Round #261 (Div. 2) B. Pashmak and Flowers 水题

    题目链接:http://codeforces.com/problemset/problem/459/B 题意: 给出n支花,每支花都有一个漂亮值.挑选最大和最小漂亮值得两支花,问他们的差值为多少,并且 ...

  9. Codeforces Round #261 (Div. 2)459A. Pashmak and Garden(数学题)

    题目链接:http://codeforces.com/problemset/problem/459/A A. Pashmak and Garden time limit per test 1 seco ...

随机推荐

  1. git - git命令中文显示乱码

    使用git add添加要提交的文件的时候,如果文件名是中文,会显示形如897\232\350\256...的乱码,解决办法:   git config --global core.quotepath ...

  2. Java学习(final、static关键词)

    final关键词 概念:final的意思为最终,不可变.final是个修饰符,它可以用来修饰类,类的成员,以及局部变量.不能修饰构造方法. 特点: 1.final修饰的类不可以被继承,但可以继承别的类 ...

  3. int类中的方法

    我们知道在python中,一切对象都是类,对象的方法都封装在类中,现在来探讨一下int类中的方法: 我们可以通过help(int)和dir(int)来查看int类中都封装了那些方法:     1.bi ...

  4. React.js学习之理解JSX和组件

    在开启JSX的学习旅程前,我们先了解一下React的基本原理.React本质上是一个"状态机",它只关心两件事:更新DOM和响应事件,React不处理Ajax.路由和数据存储,也不 ...

  5. 微软企业库5.0 学习之路——扩展学习篇、库中的依赖关系注入(重构 Microsoft Enterprise Library)[转]

    这篇文章是我在patterns & practices看到的一篇有关EntLib5.0的文章,主要介绍了EntLib5.0的这次的架构变化由来,觉得很不错,大家可以看一下! 在过去几年中,依赖 ...

  6. 检测浏览器对HTML5新input类型的支持

    HTML5新增加了很多input元素类型,比如color,date,datetime,datetime-local,email,month,number,range,search,tel,time,u ...

  7. Android签名打包详解

    一.      Android签名有什么作用? 应用程序升级:如果你希望用户无缝升级到新的版本,那么你必须用同一个证书进行签名.这是由于只有以同一个证书签名,系统才会允许安装升级的应用程序.如果你采用 ...

  8. 磁盘镜像分析工具TSK

    磁盘镜像分析工具TSK   TSK(The Sleuth Kit)是一款基于命令行的数字取证工具集,用于分析磁盘镜像.该工具支持常见的各种文件系统,如Ext2/Ext3/Ext4.Fat/exFat. ...

  9. 修复mysql

    REPAIR TABLE TABLENAME 或 REPAIR TABLE TABLENAME USE_FRM

  10. React Native 系列(八)

    前言 本系列是基于React Native版本号0.44.3写的.我们都知道,一个App不可能只有一个不变的界面,而是通过多个界面间的跳转来呈现不同的内容.那么这篇文章将介绍RN中的导航. 导航 什么 ...