SRM 410(1-250pt, 1-500pt)
DIV1 250pt
题意:对于图G,有一些点和边,点中有一些点称为特殊点。问在所有特殊点最终不能处于同一个联通块的条件下,最多能给在图G中添加多少条边。
解法:首先,对于图G,处理出它有哪些联通块,然后,不含有特殊点的联通块要连接到某一个含有特殊点的联通块上。连接哪一个能使添加的边最多呢?当然是连接含有点数最多的含特殊点的联通块。
tag:graph, greedy
- // BEGIN CUT HERE
- /*
- * Author: plum rain
- * score :
- */
- /*
- */
- // END CUT HERE
- #line 11 "AddElectricalWires.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 AddElectricalWires
- {
- public:
- int f[];
- bool v[];
- int find(int x)
- {
- if (x != f[x]) f[x] = find(f[x]);
- return f[x];
- }
- int maxNewWires(vector <string> p, vector <int> num){
- clr0 (v);
- for (int i = ; i < sz(num); ++ i) v[num[i]] = ;
- for (int i = ; i < sz(p); ++ i) f[i] = i;
- for (int i = ; i < sz(p); ++ i)
- for (int j = ; j < sz(p); ++ j) if (p[i][j] == ''){
- int t1 = find(i), t2 = find(j);
- if ((v[t1] && v[t2]) || (t1 == t2)) continue;
- if (!v[t1]) f[t1] = t2;
- else f[t2] = t1;
- }
- int ans = , idx = num[];
- for (int i = ; i < sz(num); ++ i){
- int tmp = ;
- for (int j = ; j < sz(p); ++ j)
- if (find(j) == num[i]) ++ tmp;
- if (tmp > ans)
- idx = num[i], ans = tmp;
- }
- int ret = ;
- for (int i = ; i < sz(p); ++ i)
- for (int j = i+; j < sz(p); ++ j) if (p[i][j] == ''){
- int t1 = find(i), t2 = find(j);
- if (t1 == t2){
- ++ ret; continue;
- }
- if (v[t1] && v[t2]) continue;
- if (!v[t1] && !v[t2]){
- ++ ret; f[t1] = t2; continue;
- }
- if (t1 == idx || t2 == idx){
- ++ ret;
- if (t1 == idx) f[t2] = idx;
- else f[t1] = idx;
- }
- }
- 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[] = {"","",""}; 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)); }
- 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)); }
- 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)); }
- 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)); }
- 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)); }
- // END CUT HERE
- };
- // BEGIN CUT HERE
- int main()
- {
- // freopen( "a.out" , "w" , stdout );
- AddElectricalWires ___test;
- ___test.run_test(-);
- return ;
- }
- // 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
- // BEGIN CUT HERE
- /*
- * Author: plum rain
- * score :
- */
- /*
- */
- // END CUT HERE
- #line 11 "ContiguousCache.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 = / ;
- const int64 llinf = 1LL<<;
- class ContiguousCache
- {
- public:
- int64 d[][];
- int64 over(int a, int b, int c, int d, int k)
- {
- if (d < a || c > b) return k;
- return max(b, d) - min(a, c) + - k;
- }
- long long minimumReads(int len, int k, vector <int> pos){
- vi x;
- for (int i = ; i < sz(pos); ++ i){
- x.pb (min(pos[i], len-k));
- x.pb (max(, pos[i]-k+));
- }
- x.erase(unique(x.begin(), x.end()), x.end());
- for (int i = ; i < sz(pos); ++ i)
- for (int j = ; j < sz(x); ++ j) d[i][j] = llinf;
- for (int i = ; i < sz(x); ++ i) if (x[i] <= pos[] && x[i]+k- >= pos[])
- d[][i] = k;
- for (int i = ; i < sz(pos); ++ i)
- for (int j = ; j < sz(x); ++ j) if (x[j] <= pos[i] && x[j]+k- >= pos[i])
- for (int t = ; t < sz(x); ++ t) if (d[i-][t] != llinf)
- d[i][j] = min(d[i][j], d[i-][t] + over(x[t], x[t]+k-, x[j], x[j]+k-, k));
- int64 ret = llinf;
- for (int i = ; i < sz(x); ++ i) ret = min(ret, d[sz(pos)-][i]);
- 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(); }
- 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 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; } }
- 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)); }
- 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)); }
- 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)); }
- 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)); }
- // END CUT HERE
- };
- // BEGIN CUT HERE
- int main()
- {
- // freopen( "a.out" , "w" , stdout );
- ContiguousCache ___test;
- ___test.run_test(-);
- return ;
- }
- // END CUT HERE
SRM 410(1-250pt, 1-500pt)的更多相关文章
- topcoder srm 410 div1
problem1 link 不包含$gridConnections$ 的联通块一定是连在所有包含$gridConnections$的联通块中最大的那一块上. import java.util.*; i ...
- 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: 题意:有个机器人,从某一点出发,他只有碰到地形边缘或者碰到走过的点时才会改变运动方向,然后接着走,现在给出他的运动轨迹,判断他的运动是否合法,如果合法的话,那么整个地形的最小面 ...
随机推荐
- JSP include HTML出现乱码
解决方法:在项目的web.xml中加入下面语句:<jsp-config> <jsp-property-group> <description> ...
- Linux C++服务器程序设计范式
<Unix网络编程>30章详细介绍了几种服务器设计范式.总结了其中的几种,记录一下: 多进程的做法: 1.每次创建一个新的请求,fork一个子进程,处理该连接的数据传输. 2.预先派生一定 ...
- 读书笔记之 - javascript 设计模式 - 装饰者模式
本章讨论的是一种为对象增添特性的技术,它并不使用创建新子类这种手段. 装饰者模式可以透明地把对象包装在具有同样接口的另一对象之中,这样一来,你可以给一些方法添加一些行为,然后将方法调用传递给原始对象. ...
- 使用css3画饼图
CSS3 Gradient介绍参考地址: http://www.cnblogs.com/lhb25/archive/2013/01/30/css3-linear-gradient.html http: ...
- 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 ...
- GIF文件转换为头文件工具
目的: GIF文件转为头文件 举例: 用UE打开GIF文件,如下图所示:图1 test.gif文件将上面文件内容转化为头文件,放到一个数组里面,内容如下:图2 test.h文件 思路: 从上面可知,将 ...
- [BZOJ 1036] [ZJOI2008] 树的统计Count 【Link Cut Tree】
题目链接:BZOJ - 1036 题目分析 这道题可以用树链剖分,块状树等多种方法解决,也可以使用 LCT. 修改某个点的值时,先将它 Splay 到它所在的 Splay 的根,然后修改它的值,再将它 ...
- 如何监控 Nginx?
什么是 Nginx? Nginx("engine-x")是一个 HTTP 和反向代理服务器,同时也是一个邮件代理服务器和通用的 TCP 代理服务器.作为一个免费开源的服务器,Ngi ...
- Junit4学习笔记
一.初始化标注 在老Junit4提供了setUp()和tearDown(),在每个测试函数调用之前/后都会调用. @Before: Method annotated with @Before exec ...
- keil多文件组织方法
方法一 首先新建一个main.c的文件,加入到项目中,该文件中主要写main函数,然后,新建文件,如delay.c,编写内容之后,不要加入到项目,而是在main.c文件的开始写上#include“de ...