DIV1 250pt

题意:用数组A表示置换,由该置换得到数组B(B[0] = 0, B[i] = A[B[i-1]])。给定A,求一个A',使得由A'得到的B为单循环置换且A'与A的差距最小。定义A与A'的差距为,有多少个i满足A[i] != A'[i]。返回最小差距值。A.size() <= 50。

解法:要得到的B为单循环置换,则A'也为单循环置换。如果置换A含有t个循环节,if (t==1)差距为0,否则最小差距为t,原因是可以通过交换某两个数的位置,使得两个循环变为1个循环。

tag:math, permutation

  1. // BEGIN CUT HERE
  2. /*
  3. * Author: plum rain
  4. * score :
  5. */
  6. /*
  7.  
  8. */
  9. // END CUT HERE
  10. #line 11 "PerfectPermutation.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 PerfectPermutation
  59. {
  60. public:
  61. bool v[];
  62. int reorder(vector <int> p){
  63. clr0 (v);
  64. int cnt = ;
  65. for (int i = ; i < sz(p); ++ i) if (!v[p[i]]){
  66. int t = p[i];
  67. while (!v[t])
  68. v[t] = , t = p[t];
  69. ++ cnt;
  70. }
  71. if (cnt == ) return ;
  72. return cnt;
  73. }
  74.  
  75. // BEGIN CUT HERE
  76. public:
  77. 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(); }
  78. private:
  79. 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(); }
  80. 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; } }
  81. void test_case_0() { int Arr0[] = {, , }; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arg1 = ; verify_case(, Arg1, reorder(Arg0)); }
  82. void test_case_1() { int Arr0[] = {, , , , }; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arg1 = ; verify_case(, Arg1, reorder(Arg0)); }
  83. void test_case_2() { int Arr0[] = {, , , }; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arg1 = ; verify_case(, Arg1, reorder(Arg0)); }
  84. void test_case_3() { int Arr0[] = {, , , , , }; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arg1 = ; verify_case(, Arg1, reorder(Arg0)); }
  85. void test_case_4() { int Arr0[] = {, , , , , , , , , }; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arg1 = ; verify_case(, Arg1, reorder(Arg0)); }
  86.  
  87. // END CUT HERE
  88.  
  89. };
  90.  
  91. // BEGIN CUT HERE
  92. int main()
  93. {
  94. // freopen( "a.out" , "w" , stdout );
  95. PerfectPermutation ___test;
  96. ___test.run_test(-);
  97. return ;
  98. }
  99. // END CUT HERE

DIV1 500pt

题意:在一个无向图G中,可以做这样一种操作:

   选取一组点(A,B,C,D),其中AB有边直接连接,CD有边直接连接,AC,AD,BC,BD无直连边。那么毁坏AB,CD相连的边,并重新连接AC和BD,或者重新连接AD和BC。

   对于一张给定图,问最少多少次操作才能使其变为连通图,如果不能变为连通图输出-1。

解法:我YY了一个结论。。。如果G中已经连的边数<G中的点数-1,则输出-1;如果存在某个连通块点数为1,输出-1;否则输出连通块的数目-1。

   下面是证明:称题目所给操作为L操作。

   由题可以推出以下几点:1、L操作不改变边的数量;2、如果去除了AB相连的边,AB还是在同一个连通块中,即AB在某个环上,则L操作可以将两个连通块变为一个;3、如果某个连通块的边数>=点数,即该连通块含有环,那么一定能够通过L操作将它与另一个含有边的连通块融合为一个。而连通块含有边的条件就是点数大于1;4、对于连通块的数目,一次L操作要么不改变连通块的数目,要么使连通快的数目减1。

   由1,2,3可知,当G中边数>=点数且不存在点数为1的连通块时,可通过不断进行L操作来将将连通块数目减少到1,即此时一定可以将G变为连通图。

   由4可知,每次L操作只能使连通块数目减1,则一定需要连通块数目-1次L操作才能将G变为联通图。所以,问题得证。

Ps:官方题解貌似使用记录所有联通块的点数和边数,不断合并联通块的方式来做的。我的代码比它的简单多了^ ^。

tag:think, graph, good

  1. // BEGIN CUT HERE
  2. /*
  3. * Author: plum rain
  4. * score :
  5. */
  6. /*
  7.  
  8. */
  9. // END CUT HERE
  10. #line 11 "StrangeCountry.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 StrangeCountry
  59. {
  60. public:
  61. int f[], cnt[];
  62. int find (int x)
  63. {
  64. if (x != f[x]) f[x] = find(f[x]);
  65. return f[x];
  66. }
  67. int transform(vector <string> g){
  68. int num = ;
  69. for (int i = ; i < sz(g); ++ i)
  70. for (int j = ; j < sz(g); ++ j)
  71. if (i != j && g[i][j] == 'Y') ++ num;
  72. num /= ;
  73. if (num < sz(g)-) return -;
  74.  
  75. for (int i = ; i < sz(g); ++ i) f[i] = i;
  76. for (int i = ; i < sz(g); ++ i)
  77. for (int j = ; j < sz(g); ++ j) if (g[i][j] == 'Y'){
  78. int t1 = find(i), t2 = find(j);
  79. if (t1 != t2) f[t1] = t2;
  80. }
  81. int ret = ;
  82. clr0 (cnt);
  83. for (int i = ; i < sz(g); ++ i){
  84. int t = find (i);
  85. if (!cnt[t]) ++ ret;
  86. ++ cnt[t];
  87. }
  88. for (int i = ; i < sz(g); ++ i)
  89. if (cnt[i] == ) return -;
  90. return ret - ;
  91. }
  92.  
  93. // BEGIN CUT HERE
  94. public:
  95. 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(); }
  96. private:
  97. 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(); }
  98. 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; } }
  99. void test_case_0() { string Arr0[] = {"NYNNNNNYNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNYYYNN", "YNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNYYYNN", "NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNYNNNNNNNN", "NNNNNNNNNNNNNNNNNNNNNNYNNNNNNNNNNNNNNNNNNNNN", "NNNNNNNNNNNNNNNNNNNNNNNNNYNNNNNNNNNNNNNNNNNN", "NNNNNNNNNNNNNNNNNNNNNNNNNNNNNYNNNNNNNNNNNNNN", "NNNNNNNNNNNNNYNNNNNNNNNNNNNNYNNNNNNNNNNNNNNN", "YNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNYYYNN", "NNNNNNNNNYNNNNNNNNNNNNNNNYYNNNNNNNNNNNNNNNNN", "NNNNNNNNYNNNYNNNNNNNNNNNNYNNNNNNNNNNNNNNNNNN", "NNNNNNNNNNNNNNNNNYNNNNNNNNNNNNNNNNNNNNNNNNNY", "NNNNNNNNNNNNNYNNNNNNNYNNNNNNNNNNNNNNNNNNNNNN", "NNNNNNNNNYNNNNNNNNNNNNNNNYYNNNNNYNNNN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NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNYNNNNNNNNNN", "NNNNNNNNNNNNNNNNNNNNNYNNNNNNNNNNNNNNNNYNNNNN", "NNNNNNNNNNNNNNNNYNNNNNNNNNNNNNNNNNNNNNNNNNNN", "NNNNNNNNNNNNNNNNNNNNNYNNNNNNNNNNNNNNYNNNNNNN", "YYNNNNNYNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNYYNN", "YYNNNNNYNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNYNYNN", "YYNNNNNYNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNYYNNN", "NNNNNNNNNNNNNNNNNNNYNNNNNNNNNNNYNNNNNNNNNNNN", "NNNNNNNNNNYNNNNYNYNNNNNNNNNNNNNNNNNNNNNNNNNN"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arg1 = ; verify_case(, Arg1, transform(Arg0)); }
  100. void test_case_1() { string Arr0[] = {"NYYNN",
  101. "YNYNN",
  102. "YYNNN",
  103. "NNNNY",
  104. "NNNYN"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arg1 = ; verify_case(, Arg1, transform(Arg0)); }
  105. void test_case_2() { string Arr0[] = {"NYYNNNN",
  106. "YNYNNNN",
  107. "YYNNNNN",
  108. "NNNNYYN",
  109. "NNNYNYY",
  110. "NNNYYNY",
  111. "NNNNYYN"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arg1 = ; verify_case(, Arg1, transform(Arg0)); }
  112. void test_case_3() { string Arr0[] = {"NYNYNNNNNNNN",
  113. "YNYNNNNNNNNN",
  114. "NYNYYNNNNNNN",
  115. "YNYNNNNNNNNN",
  116. "NNYNNYYNNNNN",
  117. "NNNNYNYNNNNN",
  118. "NNNNYYNNNNNN",
  119. "NNNNNNNNYYNN",
  120. "NNNNNNNYNYNN",
  121. "NNNNNNNYYNNN",
  122. "NNNNNNNNNNNY",
  123. "NNNNNNNNNNYN"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arg1 = ; verify_case(, Arg1, transform(Arg0)); }
  124. void test_case_4() { string Arr0[] = {"NYNNNN",
  125. "YNYNNN",
  126. "NYNYNN",
  127. "NNYNNN",
  128. "NNNNNY",
  129. "NNNNYN"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arg1 = -; verify_case(, Arg1, transform(Arg0)); }
  130.  
  131. // END CUT HERE
  132.  
  133. };
  134.  
  135. // BEGIN CUT HERE
  136. int main()
  137. {
  138. // freopen( "a.out" , "w" , stdout );
  139. StrangeCountry ___test;
  140. ___test.run_test(-);
  141. return ;
  142. }
  143. // END CUT HERE

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

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

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

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

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

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

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

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

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

  5. topcoder srm 553

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

  6. topcoder srm 552

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

  7. topcoder srm 551

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

  8. topcoder srm 550

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

  9. topcoder srm 610

    div1 250pt: 题意:100*100的01矩阵,找出来面积最大的“类似国际象棋棋盘”的子矩阵. 解法:枚举矩阵宽(水平方向)的起点和终点,然后利用尺取法来找到每个固定宽度下的最大矩阵,不断更新 ...

随机推荐

  1. Python编写相关注意事项

    1.# -*- coding: utf-8 -*-代码首部添加这个,不然会报Non_ASCII charater错误 python闭包:实际应用场景1.保持闭包运行完后的环境: 2.根据外部作用域的局 ...

  2. .NET(C#):获取进程的CPU使用状况

    第一个是通过手动的方法来计算CPU使用比例:CPU使用比例 = 在间隔时间内进程的CPU使用时间 除以 计算机逻辑CPU数量. 使用Process类的UserProcessorTime和Privile ...

  3. Jsp的九个隐含对象

    JSP的9个隐含对象(内置对象) 不需要预先声明,就可以在jsp或者表达式中随意使用 out javax.servlet.jsp.JspWriter类型,代表输出流的对象.作业域:页面的执行期. re ...

  4. 自定义Operation

    1.要自定义一个Operation 首先要创建一个继承于NSOperation的类. 2.在创建好的类的.h文件声明自定义的方法:-(instancetype)initWithDownLoadMess ...

  5. jQuery入门[3]-事件

    jQuery对事件的支持主要包括: bind()--为事件绑定处理程序,如: $("p").bind("mouseenter mouseleave", func ...

  6. wordpress4.0.1源码学习和摘录--函数

    1.根据类型获取当前时间 function current_time( $type, $gmt = 0 ) { switch ( $type ) { case 'mysql': return ( $g ...

  7. [Linux]Vim的安装及使用

    1.安装:$sudo apt-get install vim 2.查看Vim所在路径$whereis vim 3.启动Vim $'/usr/bin/vim.tiny'  4. 退出Vim窗口:Ctrl ...

  8. HTTP 无法注册 URL http://+:80/Temporary_Listen_Addresses/92819ef8-81ea-4bd9-

    今天在练习wcf时,客户端调用服务端方法时出现异常.如下: 未处理System.ServiceModel.AddressAlreadyInUseException Message="HTTP ...

  9. debug(fmt,args...)调试

    1.定义宏(debug.h) #ifndef __DEBUG__H #define __DEBUG__H #include <stdio.h> #ifdef DEBUG #define d ...

  10. TaskMgr C#技术拾遗

    1. DataGridView和ContextMenuStrip的绑定是发生在DataGridView的CellMouseClick事件,在事件中指定右键菜单弹出: 2. DataGridView的列 ...