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: 题意:有个机器人,从某一点出发,他只有碰到地形边缘或者碰到走过的点时才会改变运动方向,然后接着走,现在给出他的运动轨迹,判断他的运动是否合法,如果合法的话,那么整个地形的最小面 ...
随机推荐
- maven 创建的符号连接命令
E:\Joyplus\src\main\webapp\WEB-INF>mklink /d lib ..\..\..\.\..\target\ediHelperSuite-0.5\WEB-INF\ ...
- Angularjs总结(二)过滤器使用
html页面: <table> <thead> <tr> <td class="td">序号</td> <td c ...
- iOS开发进阶-实现多线程的3种方法
相关文章链接: 1.多线程简介 2.实现多线程的3种方法 ......待续 前言 在多线程简介中,我已经说明过了,为了提高界面的流畅度以及用户体验.我们务必要把耗时的操作放到别的线程中去执行,千万不要 ...
- power shell upload file to azure storage
# Azure subscription-specific variables. $storageAccountName = "storage-account-name" $con ...
- 【AngularJS】——0.分析
[引导分析]1.什么是AngularJS? 2.为什么要使用它? 3.应用场合? 4.基本思想? 5.四大核心特征? 6.优缺点是什么? 1.定义:AngularJS是一个用于设计动态web应用的前端 ...
- 半质数的个数 csdn 英雄会 高校俱乐部
2·14 情人&元宵节专题:半质数的个数. 题目:质数是大家熟知的概念,我们定义一个半质数的概念:如果一个数恰好是两个质数的乘积(可以相同),则称它为半质数.前几个半质数是 4, 6, 9, ...
- 理解MySQL——索引与优化(转)
写 在前面:索引对查询的速度有着至关重要的影响,理解索引也是进行数据库性能调优的起点.考虑如下情况,假设数据库中一个表有10^6条记录,DBMS的页 面大小为4K,并存储100条记录.如果没有索引,查 ...
- 引入的iframe是跨域的, 如何控制其高度
前提是你有编辑这个跨域的iframe的权限!! 1. main-domain.html (main display HTML) <!DOCTYPE html> <html> & ...
- 问题:Maven: missing net.sf.json-lib
问题:Maven: missing net.sf.json-lib 解决: pom.xml里加入json-lib依赖,注意要添加classifier一行,如下: 这里maven地址:http://re ...
- trac的安装和配置
trac是一个很好用的基于python的任务管理系统. 这个软件主要有如下特点: 1)tickit管理,可以设定各种各样的ticket,然后进行修改和完成. 2)wiki管理,可以编写各种文档. 3) ...