DIV1 250pt

题意:对于图G,有一些点和边,点中有一些点称为特殊点。问在所有特殊点最终不能处于同一个联通块的条件下,最多能给在图G中添加多少条边。

解法:首先,对于图G,处理出它有哪些联通块,然后,不含有特殊点的联通块要连接到某一个含有特殊点的联通块上。连接哪一个能使添加的边最多呢?当然是连接含有点数最多的含特殊点的联通块。

tag:graph, greedy

  1. // BEGIN CUT HERE
  2. /*
  3. * Author: plum rain
  4. * score :
  5. */
  6. /*
  7.  
  8. */
  9. // END CUT HERE
  10. #line 11 "AddElectricalWires.cpp"
  11. #include <sstream>
  12. #include <stdexcept>
  13. #include <functional>
  14. #include <iomanip>
  15. #include <numeric>
  16. #include <fstream>
  17. #include <cctype>
  18. #include <iostream>
  19. #include <cstdio>
  20. #include <vector>
  21. #include <cstring>
  22. #include <cmath>
  23. #include <algorithm>
  24. #include <cstdlib>
  25. #include <set>
  26. #include <queue>
  27. #include <bitset>
  28. #include <list>
  29. #include <string>
  30. #include <utility>
  31. #include <map>
  32. #include <ctime>
  33. #include <stack>
  34.  
  35. using namespace std;
  36.  
  37. #define clr0(x) memset(x, 0, sizeof(x))
  38. #define clr1(x) memset(x, -1, sizeof(x))
  39. #define pb push_back
  40. #define sz(v) ((int)(v).size())
  41. #define all(t) t.begin(),t.end()
  42. #define zero(x) (((x)>0?(x):-(x))<eps)
  43. #define out(x) cout<<#x<<":"<<(x)<<endl
  44. #define tst(a) cout<<a<<" "
  45. #define tst1(a) cout<<#a<<endl
  46. #define CINBEQUICKER std::ios::sync_with_stdio(false)
  47.  
  48. typedef vector<int> vi;
  49. typedef vector<string> vs;
  50. typedef vector<double> vd;
  51. typedef pair<int, int> pii;
  52. typedef long long int64;
  53.  
  54. const double eps = 1e-;
  55. const double PI = atan(1.0)*;
  56. const int inf = / ;
  57.  
  58. class AddElectricalWires
  59. {
  60. public:
  61. int f[];
  62. bool v[];
  63.  
  64. int find(int x)
  65. {
  66. if (x != f[x]) f[x] = find(f[x]);
  67. return f[x];
  68. }
  69.  
  70. int maxNewWires(vector <string> p, vector <int> num){
  71. clr0 (v);
  72. for (int i = ; i < sz(num); ++ i) v[num[i]] = ;
  73. for (int i = ; i < sz(p); ++ i) f[i] = i;
  74. for (int i = ; i < sz(p); ++ i)
  75. for (int j = ; j < sz(p); ++ j) if (p[i][j] == ''){
  76. int t1 = find(i), t2 = find(j);
  77. if ((v[t1] && v[t2]) || (t1 == t2)) continue;
  78. if (!v[t1]) f[t1] = t2;
  79. else f[t2] = t1;
  80.  
  81. }
  82. int ans = , idx = num[];
  83. for (int i = ; i < sz(num); ++ i){
  84. int tmp = ;
  85. for (int j = ; j < sz(p); ++ j)
  86. if (find(j) == num[i]) ++ tmp;
  87. if (tmp > ans)
  88. idx = num[i], ans = tmp;
  89. }
  90. int ret = ;
  91. for (int i = ; i < sz(p); ++ i)
  92. for (int j = i+; j < sz(p); ++ j) if (p[i][j] == ''){
  93. int t1 = find(i), t2 = find(j);
  94. if (t1 == t2){
  95. ++ ret; continue;
  96. }
  97. if (v[t1] && v[t2]) continue;
  98. if (!v[t1] && !v[t2]){
  99. ++ ret; f[t1] = t2; continue;
  100. }
  101. if (t1 == idx || t2 == idx){
  102. ++ ret;
  103. if (t1 == idx) f[t2] = idx;
  104. else f[t1] = idx;
  105. }
  106. }
  107. return ret;
  108. }
  109.  
  110. // BEGIN CUT HERE
  111. public:
  112. void run_test(int Case) { if ((Case == -) || (Case == )) test_case_0(); if ((Case == -) || (Case == )) test_case_1(); if ((Case == -) || (Case == )) test_case_2(); if ((Case == -) || (Case == )) test_case_3(); if ((Case == -) || (Case == )) test_case_4(); }
  113. private:
  114. template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }
  115. void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } }
  116. void test_case_0() { string Arr0[] = {"","",""}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arr1[] = {}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[]))); int Arg2 = ; verify_case(, Arg2, maxNewWires(Arg0, Arg1)); }
  117. void test_case_1() { string Arr0[] = {"","",""}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arr1[] = {,}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[]))); int Arg2 = ; verify_case(, Arg2, maxNewWires(Arg0, Arg1)); }
  118. void test_case_2() { string Arr0[] = {"",""}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arr1[] = {}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[]))); int Arg2 = ; verify_case(, Arg2, maxNewWires(Arg0, Arg1)); }
  119. void test_case_3() { string Arr0[] = {"","","","",""}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arr1[] = {,,,,}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[]))); int Arg2 = ; verify_case(, Arg2, maxNewWires(Arg0, Arg1)); }
  120. void test_case_4() { string Arr0[] = {"","","","",""}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arr1[] = {,}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[]))); int Arg2 = ; verify_case(, Arg2, maxNewWires(Arg0, Arg1)); }
  121.  
  122. // END CUT HERE
  123.  
  124. };
  125.  
  126. // BEGIN CUT HERE
  127. int main()
  128. {
  129. // freopen( "a.out" , "w" , stdout );
  130. AddElectricalWires ___test;
  131. ___test.run_test(-);
  132. return ;
  133. }
  134. // END CUT HERE

DIV1 500pt

题意:给定整数k,n和一个数组A[],要按顺序访问范围为0-(n-1)的数轴上编号为A[i]的点,访问的方法如下。首先,找到一段长度为k的连续的点,(比如编号为x, x+1, x+2...x+k-1),然后查看缓存中的点,有三种可能:某点在缓存中但这次不需要访问,则移出缓存;某点不在缓存中但这次要访问,访问该点并将它加入缓存;某点在缓存中且要访问,不访问该点但将其留在缓存中。要按顺序访问A[i]中的点,最少需要进行多少次访问操作。

  n <= 10^9, k <= n, A.size() <= 50。

  注意,如果A[] = {2, 3, 10},k = 3,n = 1000则只需访问2, 3, 4, 8, 9, 10即可。

解法:本题的关键点在于要注意到一点,要访问A[i],最优解访问的连续k个点只有两类可能:A[j], A[j]+1...A[j]+k-1和A[j]-k+1, A[j]-k+2...A[j]。(*)

   所以,先预处理出可能从哪些点开始访问。然后,由于要按顺序访问A[i],所以只需要做一遍dp即可。

tag:dp, think

  1. // BEGIN CUT HERE
  2. /*
  3. * Author: plum rain
  4. * score :
  5. */
  6. /*
  7.  
  8. */
  9. // END CUT HERE
  10. #line 11 "ContiguousCache.cpp"
  11. #include <sstream>
  12. #include <stdexcept>
  13. #include <functional>
  14. #include <iomanip>
  15. #include <numeric>
  16. #include <fstream>
  17. #include <cctype>
  18. #include <iostream>
  19. #include <cstdio>
  20. #include <vector>
  21. #include <cstring>
  22. #include <cmath>
  23. #include <algorithm>
  24. #include <cstdlib>
  25. #include <set>
  26. #include <queue>
  27. #include <bitset>
  28. #include <list>
  29. #include <string>
  30. #include <utility>
  31. #include <map>
  32. #include <ctime>
  33. #include <stack>
  34.  
  35. using namespace std;
  36.  
  37. #define clr0(x) memset(x, 0, sizeof(x))
  38. #define clr1(x) memset(x, -1, sizeof(x))
  39. #define pb push_back
  40. #define sz(v) ((int)(v).size())
  41. #define all(t) t.begin(),t.end()
  42. #define zero(x) (((x)>0?(x):-(x))<eps)
  43. #define out(x) cout<<#x<<":"<<(x)<<endl
  44. #define tst(a) cout<<a<<" "
  45. #define tst1(a) cout<<#a<<endl
  46. #define CINBEQUICKER std::ios::sync_with_stdio(false)
  47.  
  48. typedef vector<int> vi;
  49. typedef vector<string> vs;
  50. typedef vector<double> vd;
  51. typedef pair<int, int> pii;
  52. typedef long long int64;
  53.  
  54. const double eps = 1e-;
  55. const double PI = atan(1.0)*;
  56. const int inf = / ;
  57. const int64 llinf = 1LL<<;
  58.  
  59. class ContiguousCache
  60. {
  61. public:
  62. int64 d[][];
  63.  
  64. int64 over(int a, int b, int c, int d, int k)
  65. {
  66. if (d < a || c > b) return k;
  67. return max(b, d) - min(a, c) + - k;
  68. }
  69.  
  70. long long minimumReads(int len, int k, vector <int> pos){
  71. vi x;
  72. for (int i = ; i < sz(pos); ++ i){
  73. x.pb (min(pos[i], len-k));
  74. x.pb (max(, pos[i]-k+));
  75. }
  76. x.erase(unique(x.begin(), x.end()), x.end());
  77.  
  78. for (int i = ; i < sz(pos); ++ i)
  79. for (int j = ; j < sz(x); ++ j) d[i][j] = llinf;
  80.  
  81. for (int i = ; i < sz(x); ++ i) if (x[i] <= pos[] && x[i]+k- >= pos[])
  82. d[][i] = k;
  83.  
  84. for (int i = ; i < sz(pos); ++ i)
  85. for (int j = ; j < sz(x); ++ j) if (x[j] <= pos[i] && x[j]+k- >= pos[i])
  86. for (int t = ; t < sz(x); ++ t) if (d[i-][t] != llinf)
  87. d[i][j] = min(d[i][j], d[i-][t] + over(x[t], x[t]+k-, x[j], x[j]+k-, k));
  88.  
  89. int64 ret = llinf;
  90. for (int i = ; i < sz(x); ++ i) ret = min(ret, d[sz(pos)-][i]);
  91. return ret;
  92. }
  93.  
  94. // BEGIN CUT HERE
  95. public:
  96. void run_test(int Case) { if ((Case == -) || (Case == )) test_case_0(); if ((Case == -) || (Case == )) test_case_1(); if ((Case == -) || (Case == )) test_case_2(); if ((Case == -) || (Case == )) test_case_3(); }
  97. private:
  98. template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }
  99. void verify_case(int Case, const long long &Expected, const long long &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } }
  100. void test_case_0() { int Arg0 = ; int Arg1 = ; int Arr2[] = {, , , }; vector <int> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[]))); long long Arg3 = 7LL; verify_case(, Arg3, minimumReads(Arg0, Arg1, Arg2)); }
  101. void test_case_1() { int Arg0 = ; int Arg1 = ; int Arr2[] = {,,,,,}; vector <int> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[]))); long long Arg3 = 29LL; verify_case(, Arg3, minimumReads(Arg0, Arg1, Arg2)); }
  102. void test_case_2() { int Arg0 = ; int Arg1 = ; int Arr2[] = {, , , , , }; vector <int> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[]))); long long Arg3 = 1987654320LL; verify_case(, Arg3, minimumReads(Arg0, Arg1, Arg2)); }
  103. void test_case_3() { int Arg0 = ; int Arg1 = ; int Arr2[] = {}; vector <int> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[]))); long long Arg3 = 2LL; verify_case(, Arg3, minimumReads(Arg0, Arg1, Arg2)); }
  104.  
  105. // END CUT HERE
  106.  
  107. };
  108.  
  109. // BEGIN CUT HERE
  110. int main()
  111. {
  112. // freopen( "a.out" , "w" , stdout );
  113. ContiguousCache ___test;
  114. ___test.run_test(-);
  115. return ;
  116. }
  117. // END CUT HERE

    

SRM 410(1-250pt, 1-500pt)的更多相关文章

  1. topcoder srm 410 div1

    problem1 link 不包含$gridConnections$ 的联通块一定是连在所有包含$gridConnections$的联通块中最大的那一块上. import java.util.*; i ...

  2. SRM475 - SRM479(1-250pt,500pt)

    SRM 475 DIV1 300pt 题意:玩游戏.给一个棋盘,它有1×n(1行n列,每列标号分别为0,1,2..n-1)的格子,每个格子里面可以放一个棋子,并且给定一个只含三个字母WBR,长度为n的 ...

  3. SRM468 - SRM469(1-250pt, 500pt)

    SRM 468 DIV1 250pt 题意:给出字典,按照一定要求进行查找. 解法:模拟题,暴力即可. tag:water score: 0.... 这是第一次AC的代码: /* * Author: ...

  4. SRM470 - SRM474(1-250pt,500pt)(471-500pt为最短路,474-500pt未做)

    SRM 470 DIV1 250pt 题意:有n个房间排成一排,相邻两个房间之间有一扇关闭着的门(共n-1扇),每个门上都标有‘A’-‘P’的大写字母.给定一个数n,表示第n个房间.有两个人John和 ...

  5. SRM593(1-250pt,500pt)

    SRM 593 DIV1 250pt 题意:有如下图所示的平面,每个六边形有坐标.将其中一些六边形染色,要求有边相邻的两个六边形不能染同一种颜色.给定哪些六边形需要染色,问最少需要多少种颜色. 解法: ...

  6. topcoder srm 553

    div1 250pt: 题意:... 解法:先假设空出来的位置是0,然后模拟一次看看是不是满足,如果不行的话,我们只需要关心最后栈顶的元素取值是不是受空白处的影响,于是还是模拟一下. // BEGIN ...

  7. topcoder srm 552

    div1 250pt: 题意:用RGB三种颜色的球摆N层的三角形,要求相邻的不同色,给出RGB的数量,问最多能摆几个 解法:三种颜色的数量要么是全一样,要么是两个一样,另外一个比他们多一个,于是可以分 ...

  8. topcoder srm 551

    div1 250pt 题意:一个长度最多50的字符串,每次操作可以交换相邻的两个字符,问,经过最多MaxSwaps次交换之后,最多能让多少个相同的字符连起来 解法:对于每种字符,枚举一个“集结点”,让 ...

  9. topcoder srm 550

    div1 250pt: 题意:有个机器人,从某一点出发,他只有碰到地形边缘或者碰到走过的点时才会改变运动方向,然后接着走,现在给出他的运动轨迹,判断他的运动是否合法,如果合法的话,那么整个地形的最小面 ...

随机推荐

  1. JSP include HTML出现乱码

    解决方法:在项目的web.xml中加入下面语句:<jsp-config>     <jsp-property-group>     <description>    ...

  2. Linux C++服务器程序设计范式

    <Unix网络编程>30章详细介绍了几种服务器设计范式.总结了其中的几种,记录一下: 多进程的做法: 1.每次创建一个新的请求,fork一个子进程,处理该连接的数据传输. 2.预先派生一定 ...

  3. 读书笔记之 - javascript 设计模式 - 装饰者模式

    本章讨论的是一种为对象增添特性的技术,它并不使用创建新子类这种手段. 装饰者模式可以透明地把对象包装在具有同样接口的另一对象之中,这样一来,你可以给一些方法添加一些行为,然后将方法调用传递给原始对象. ...

  4. 使用css3画饼图

    CSS3 Gradient介绍参考地址: http://www.cnblogs.com/lhb25/archive/2013/01/30/css3-linear-gradient.html http: ...

  5. CSS and JavaScript Bundling and Minification in ASP.NET 4.5

    ASP.NET 4.5 includes a new feature to minify and bundle CSS and JavaScript within your web applicati ...

  6. GIF文件转换为头文件工具

    目的: GIF文件转为头文件 举例: 用UE打开GIF文件,如下图所示:图1 test.gif文件将上面文件内容转化为头文件,放到一个数组里面,内容如下:图2 test.h文件 思路: 从上面可知,将 ...

  7. [BZOJ 1036] [ZJOI2008] 树的统计Count 【Link Cut Tree】

    题目链接:BZOJ - 1036 题目分析 这道题可以用树链剖分,块状树等多种方法解决,也可以使用 LCT. 修改某个点的值时,先将它 Splay 到它所在的 Splay 的根,然后修改它的值,再将它 ...

  8. 如何监控 Nginx?

    什么是 Nginx? Nginx("engine-x")是一个 HTTP 和反向代理服务器,同时也是一个邮件代理服务器和通用的 TCP 代理服务器.作为一个免费开源的服务器,Ngi ...

  9. Junit4学习笔记

    一.初始化标注 在老Junit4提供了setUp()和tearDown(),在每个测试函数调用之前/后都会调用. @Before: Method annotated with @Before exec ...

  10. keil多文件组织方法

    方法一 首先新建一个main.c的文件,加入到项目中,该文件中主要写main函数,然后,新建文件,如delay.c,编写内容之后,不要加入到项目,而是在main.c文件的开始写上#include“de ...