SRM 441(1-250pt, 1-500pt)
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
- // BEGIN CUT HERE
- /*
- * Author: plum rain
- * score :
- */
- /*
- */
- // END CUT HERE
- #line 11 "PerfectPermutation.cpp"
- #include <sstream>
- #include <stdexcept>
- #include <functional>
- #include <iomanip>
- #include <numeric>
- #include <fstream>
- #include <cctype>
- #include <iostream>
- #include <cstdio>
- #include <vector>
- #include <cstring>
- #include <cmath>
- #include <algorithm>
- #include <cstdlib>
- #include <set>
- #include <queue>
- #include <bitset>
- #include <list>
- #include <string>
- #include <utility>
- #include <map>
- #include <ctime>
- #include <stack>
- using namespace std;
- #define clr0(x) memset(x, 0, sizeof(x))
- #define clr1(x) memset(x, -1, sizeof(x))
- #define pb push_back
- #define sz(v) ((int)(v).size())
- #define all(t) t.begin(),t.end()
- #define zero(x) (((x)>0?(x):-(x))<eps)
- #define out(x) cout<<#x<<":"<<(x)<<endl
- #define tst(a) cout<<a<<" "
- #define tst1(a) cout<<#a<<endl
- #define CINBEQUICKER std::ios::sync_with_stdio(false)
- typedef vector<int> vi;
- typedef vector<string> vs;
- typedef vector<double> vd;
- typedef pair<int, int> pii;
- typedef long long int64;
- const double eps = 1e-;
- const double PI = atan(1.0)*;
- const int inf = / ;
- class PerfectPermutation
- {
- public:
- bool v[];
- int reorder(vector <int> p){
- clr0 (v);
- int cnt = ;
- for (int i = ; i < sz(p); ++ i) if (!v[p[i]]){
- int t = p[i];
- while (!v[t])
- v[t] = , t = p[t];
- ++ cnt;
- }
- if (cnt == ) return ;
- return cnt;
- }
- // BEGIN CUT HERE
- public:
- 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(); }
- private:
- 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(); }
- 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; } }
- void test_case_0() { int Arr0[] = {, , }; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arg1 = ; verify_case(, Arg1, reorder(Arg0)); }
- void test_case_1() { int Arr0[] = {, , , , }; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arg1 = ; verify_case(, Arg1, reorder(Arg0)); }
- void test_case_2() { int Arr0[] = {, , , }; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arg1 = ; verify_case(, Arg1, reorder(Arg0)); }
- void test_case_3() { int Arr0[] = {, , , , , }; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arg1 = ; verify_case(, Arg1, reorder(Arg0)); }
- void test_case_4() { int Arr0[] = {, , , , , , , , , }; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arg1 = ; verify_case(, Arg1, reorder(Arg0)); }
- // END CUT HERE
- };
- // BEGIN CUT HERE
- int main()
- {
- // freopen( "a.out" , "w" , stdout );
- PerfectPermutation ___test;
- ___test.run_test(-);
- return ;
- }
- // 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
- // BEGIN CUT HERE
- /*
- * Author: plum rain
- * score :
- */
- /*
- */
- // END CUT HERE
- #line 11 "StrangeCountry.cpp"
- #include <sstream>
- #include <stdexcept>
- #include <functional>
- #include <iomanip>
- #include <numeric>
- #include <fstream>
- #include <cctype>
- #include <iostream>
- #include <cstdio>
- #include <vector>
- #include <cstring>
- #include <cmath>
- #include <algorithm>
- #include <cstdlib>
- #include <set>
- #include <queue>
- #include <bitset>
- #include <list>
- #include <string>
- #include <utility>
- #include <map>
- #include <ctime>
- #include <stack>
- using namespace std;
- #define clr0(x) memset(x, 0, sizeof(x))
- #define clr1(x) memset(x, -1, sizeof(x))
- #define pb push_back
- #define sz(v) ((int)(v).size())
- #define all(t) t.begin(),t.end()
- #define zero(x) (((x)>0?(x):-(x))<eps)
- #define out(x) cout<<#x<<":"<<(x)<<endl
- #define tst(a) cout<<a<<" "
- #define tst1(a) cout<<#a<<endl
- #define CINBEQUICKER std::ios::sync_with_stdio(false)
- typedef vector<int> vi;
- typedef vector<string> vs;
- typedef vector<double> vd;
- typedef pair<int, int> pii;
- typedef long long int64;
- const double eps = 1e-;
- const double PI = atan(1.0)*;
- const int inf = / ;
- class StrangeCountry
- {
- public:
- int f[], cnt[];
- int find (int x)
- {
- if (x != f[x]) f[x] = find(f[x]);
- return f[x];
- }
- int transform(vector <string> g){
- int num = ;
- for (int i = ; i < sz(g); ++ i)
- for (int j = ; j < sz(g); ++ j)
- if (i != j && g[i][j] == 'Y') ++ num;
- num /= ;
- if (num < sz(g)-) return -;
- for (int i = ; i < sz(g); ++ i) f[i] = i;
- for (int i = ; i < sz(g); ++ i)
- for (int j = ; j < sz(g); ++ j) if (g[i][j] == 'Y'){
- int t1 = find(i), t2 = find(j);
- if (t1 != t2) f[t1] = t2;
- }
- int ret = ;
- clr0 (cnt);
- for (int i = ; i < sz(g); ++ i){
- int t = find (i);
- if (!cnt[t]) ++ ret;
- ++ cnt[t];
- }
- for (int i = ; i < sz(g); ++ i)
- if (cnt[i] == ) return -;
- return ret - ;
- }
- // BEGIN CUT HERE
- public:
- 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(); }
- private:
- 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(); }
- 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; } }
- void test_case_0() { string Arr0[] = {"N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eof(Arr0) / sizeof(Arr0[]))); int Arg1 = ; verify_case(, Arg1, transform(Arg0)); }
- void test_case_1() { string Arr0[] = {"NYYNN",
- "YNYNN",
- "YYNNN",
- "NNNNY",
- "NNNYN"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arg1 = ; verify_case(, Arg1, transform(Arg0)); }
- void test_case_2() { string Arr0[] = {"NYYNNNN",
- "YNYNNNN",
- "YYNNNNN",
- "NNNNYYN",
- "NNNYNYY",
- "NNNYYNY",
- "NNNNYYN"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arg1 = ; verify_case(, Arg1, transform(Arg0)); }
- void test_case_3() { string Arr0[] = {"NYNYNNNNNNNN",
- "YNYNNNNNNNNN",
- "NYNYYNNNNNNN",
- "YNYNNNNNNNNN",
- "NNYNNYYNNNNN",
- "NNNNYNYNNNNN",
- "NNNNYYNNNNNN",
- "NNNNNNNNYYNN",
- "NNNNNNNYNYNN",
- "NNNNNNNYYNNN",
- "NNNNNNNNNNNY",
- "NNNNNNNNNNYN"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arg1 = ; verify_case(, Arg1, transform(Arg0)); }
- void test_case_4() { string Arr0[] = {"NYNNNN",
- "YNYNNN",
- "NYNYNN",
- "NNYNNN",
- "NNNNNY",
- "NNNNYN"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arg1 = -; verify_case(, Arg1, transform(Arg0)); }
- // END CUT HERE
- };
- // BEGIN CUT HERE
- int main()
- {
- // freopen( "a.out" , "w" , stdout );
- StrangeCountry ___test;
- ___test.run_test(-);
- return ;
- }
- // END CUT HERE
SRM 441(1-250pt, 1-500pt)的更多相关文章
- SRM475 - SRM479(1-250pt,500pt)
SRM 475 DIV1 300pt 题意:玩游戏.给一个棋盘,它有1×n(1行n列,每列标号分别为0,1,2..n-1)的格子,每个格子里面可以放一个棋子,并且给定一个只含三个字母WBR,长度为n的 ...
- SRM468 - SRM469(1-250pt, 500pt)
SRM 468 DIV1 250pt 题意:给出字典,按照一定要求进行查找. 解法:模拟题,暴力即可. tag:water score: 0.... 这是第一次AC的代码: /* * Author: ...
- SRM470 - SRM474(1-250pt,500pt)(471-500pt为最短路,474-500pt未做)
SRM 470 DIV1 250pt 题意:有n个房间排成一排,相邻两个房间之间有一扇关闭着的门(共n-1扇),每个门上都标有‘A’-‘P’的大写字母.给定一个数n,表示第n个房间.有两个人John和 ...
- SRM593(1-250pt,500pt)
SRM 593 DIV1 250pt 题意:有如下图所示的平面,每个六边形有坐标.将其中一些六边形染色,要求有边相邻的两个六边形不能染同一种颜色.给定哪些六边形需要染色,问最少需要多少种颜色. 解法: ...
- topcoder srm 553
div1 250pt: 题意:... 解法:先假设空出来的位置是0,然后模拟一次看看是不是满足,如果不行的话,我们只需要关心最后栈顶的元素取值是不是受空白处的影响,于是还是模拟一下. // BEGIN ...
- topcoder srm 552
div1 250pt: 题意:用RGB三种颜色的球摆N层的三角形,要求相邻的不同色,给出RGB的数量,问最多能摆几个 解法:三种颜色的数量要么是全一样,要么是两个一样,另外一个比他们多一个,于是可以分 ...
- topcoder srm 551
div1 250pt 题意:一个长度最多50的字符串,每次操作可以交换相邻的两个字符,问,经过最多MaxSwaps次交换之后,最多能让多少个相同的字符连起来 解法:对于每种字符,枚举一个“集结点”,让 ...
- topcoder srm 550
div1 250pt: 题意:有个机器人,从某一点出发,他只有碰到地形边缘或者碰到走过的点时才会改变运动方向,然后接着走,现在给出他的运动轨迹,判断他的运动是否合法,如果合法的话,那么整个地形的最小面 ...
- topcoder srm 610
div1 250pt: 题意:100*100的01矩阵,找出来面积最大的“类似国际象棋棋盘”的子矩阵. 解法:枚举矩阵宽(水平方向)的起点和终点,然后利用尺取法来找到每个固定宽度下的最大矩阵,不断更新 ...
随机推荐
- Python编写相关注意事项
1.# -*- coding: utf-8 -*-代码首部添加这个,不然会报Non_ASCII charater错误 python闭包:实际应用场景1.保持闭包运行完后的环境: 2.根据外部作用域的局 ...
- .NET(C#):获取进程的CPU使用状况
第一个是通过手动的方法来计算CPU使用比例:CPU使用比例 = 在间隔时间内进程的CPU使用时间 除以 计算机逻辑CPU数量. 使用Process类的UserProcessorTime和Privile ...
- Jsp的九个隐含对象
JSP的9个隐含对象(内置对象) 不需要预先声明,就可以在jsp或者表达式中随意使用 out javax.servlet.jsp.JspWriter类型,代表输出流的对象.作业域:页面的执行期. re ...
- 自定义Operation
1.要自定义一个Operation 首先要创建一个继承于NSOperation的类. 2.在创建好的类的.h文件声明自定义的方法:-(instancetype)initWithDownLoadMess ...
- jQuery入门[3]-事件
jQuery对事件的支持主要包括: bind()--为事件绑定处理程序,如: $("p").bind("mouseenter mouseleave", func ...
- wordpress4.0.1源码学习和摘录--函数
1.根据类型获取当前时间 function current_time( $type, $gmt = 0 ) { switch ( $type ) { case 'mysql': return ( $g ...
- [Linux]Vim的安装及使用
1.安装:$sudo apt-get install vim 2.查看Vim所在路径$whereis vim 3.启动Vim $'/usr/bin/vim.tiny' 4. 退出Vim窗口:Ctrl ...
- HTTP 无法注册 URL http://+:80/Temporary_Listen_Addresses/92819ef8-81ea-4bd9-
今天在练习wcf时,客户端调用服务端方法时出现异常.如下: 未处理System.ServiceModel.AddressAlreadyInUseException Message="HTTP ...
- debug(fmt,args...)调试
1.定义宏(debug.h) #ifndef __DEBUG__H #define __DEBUG__H #include <stdio.h> #ifdef DEBUG #define d ...
- TaskMgr C#技术拾遗
1. DataGridView和ContextMenuStrip的绑定是发生在DataGridView的CellMouseClick事件,在事件中指定右键菜单弹出: 2. DataGridView的列 ...