Topcoder SRM 648 Div.2 - A(水), B(枚举),C(DP/构造)
来源:互联网 发布:淘宝天猫积分怎么兑换 编辑:程序博客网 时间:2024/05/19 00:16
第一场TC~~ 一道题0.0 Fighting all the time!!
A. KitayutaMart2
水题~求一下公式即可,2^(n+1) = T/k+1.
CODE :
#line 7 "KitayutaMart2.cpp" #include <cstdlib> #include <cctype> #include <cstring> #include <cstdio> #include <cmath> #include <algorithm> #include <vector> #include <string> #include <iostream> #include <sstream> #include <map> #include <set> #include <queue> #include <stack> #include <fstream> #include <numeric> #include <iomanip> #include <bitset> #include <list> #include <stdexcept> #include <functional> #include <utility> #include <ctime> using namespace std; #define PB push_back #define MP make_pair #define REP(i,n) for(i=0;i<(n);++i) #define FOR(i,l,h) for(i=(l);i<=(h);++i) #define FORD(i,h,l) for(i=(h);i>=(l);--i) typedef vector<int> VI; typedef vector<string> VS; typedef vector<double> VD; typedef long long LL; typedef pair<int,int> PII; class KitayutaMart2 { public: int numBought(int K, int T) { //$CARETPOSITION$ int x = T/K+1; int n = 0, s = 1; while(s < x) { s *= 2; n++; } return n; }};B.Fragile2
由于数据量很小,枚举两个删除点, 在dfs一下图,即可。
CODE;
#line 7 "Fragile2.cpp"#include <cstdlib>#include <cctype>#include <cstring>#include <cstdio>#include <cmath>#include <algorithm>#include <vector>#include <string>#include <iostream>#include <sstream>#include <map>#include <set>#include <queue>#include <stack>#include <fstream>#include <numeric>#include <iomanip>#include <bitset>#include <list>#include <stdexcept>#include <functional>#include <utility>#include <ctime>using namespace std;#define PB push_back#define MP make_pair#define REP(i,n) for(i=0;i<(n);++i)#define FOR(i,l,h) for(i=(l);i<=(h);++i)#define FORD(i,h,l) for(i=(h);i>=(l);--i)typedef vector<int> VI;typedef vector<string> VS;typedef vector<double> VD;typedef long long LL;typedef pair<int,int> PII;bool vis[55];int gra[60][60], g[60][60];int n;class Fragile2{ public: void init(VS graph) { for(int i = 0; i < n; ++i) { for(int j = 0; j < n; ++j) { if(graph[i][j] == 'Y') gra[i][j] = 1; else gra[i][j] = 0; } } } int dfs(int st) { for(int i = 0; i < n; ++i) { if(!vis[i] && g[st][i]) { vis[i] = 1; dfs(i); } } } int work(int x, int y) { memset(vis, 0, sizeof(vis)); for(int i = 0; i < n; ++i) { for(int j = 0; j < n; ++j) { g[i][j] = gra[i][j]; } } vis[x] = 1; vis[y] = 1; for(int i = 0; i < n; ++i) { g[x][i] = 0; g[i][x] = 0; g[y][i] = 0; g[i][y] = 0; } int cnt = 0; for(int i = 0; i < n; ++i) { if(!vis[i]) { vis[i] = 1; cnt++; dfs(i); } } return cnt; } int countPairs(vector <string> graph) { int ans = 0; n = graph.size(); memset(gra, 0, sizeof(gra)); memset(g, 0, sizeof(g)); init(graph); int sum = work(n, n); for(int i = 0; i < n; ++i) { for(int j = i + 1; j < n; ++j) { if(sum < work(i, j)) ans++; } } return ans; }};C.ABC
Way1:DP: dp[i][j][k][num] = 第i个位置j个A,k个B得到num对。
Way2:构造。枚举N个A,N个B, N个C, 三个for, 直到(i×(j+k)+ j * k) == K 为止,可以连续输出A,B,C.
CODE :
#line 7 "ABC.cpp"#include <cstdlib>#include <cctype>#include <cstring>#include <cstdio>#include <cmath>#include <algorithm>#include <vector>#include <string>#include <iostream>#include <sstream>#include <map>#include <set>#include <queue>#include <stack>#include <fstream>#include <numeric>#include <iomanip>#include <bitset>#include <list>#include <stdexcept>#include <functional>#include <utility>#include <ctime>using namespace std;short dp[32][35][35][500];class ABC{ public: string cal(int x) { switch(x){ case 0: return "A"; case 1: return "B"; case 2: return "C"; } } string createString(int N, int K) { memset(dp, -1, sizeof(dp)); dp[0][0][0][0] = 0; for(int i = 0; i < N; ++i) { for(int j = 0; j <= i; ++j) { for(int k = 0; k <= i; ++k) { if(j + k > i) continue; for(int num = 0; num <= max(min((i*(i-1))/2, K), 0); ++num) { if(dp[i][j][k][num] < 0) continue; //printf("%d %d %d %d\n", i, j, k, num); for(int op = 0; op < 3; ++op) { if(op == 0) dp[i+1][j+1][k][num+i-j] = op; else if(op == 1) dp[i+1][j][k+1][num+i-(j+k)] = op; else dp[i+1][j][k][num] = op; } } } } } string ans; int k = K; for(int i = 0; i <= N; ++i) { for(int j = 0; i+j <= N; ++j) { if(dp[N][i][j][k] >= 0) { while(N) { int d = dp[N][i][j][k]; string op = cal(d); ans += op; --N; if(d == 0) { --i; k -= (N-i); } else if(d == 1) { --j; k -= (N-i-j); } } return ans; } } } return ans; }};
0 0
- Topcoder SRM 648 Div.2 - A(水), B(枚举),C(DP/构造)
- Topcoder SRM 721 Div.2 B. RememberWordsEasy
- Topcoder SRM 721 Div.2 A. FlightDataRecorder
- Topcoder SRM 721 Div.2 C. ApocalypseEasy
- TopCoder SRM 677 Div. 2 550 - FourStrings (枚举)
- TopCoder SRM 681 Div. 2 Problem 500 - ExplodingRobots (枚举)
- Topcoder srm 653 div.2 1000 - SingingEasy(区间DP)
- Topcoder srm 653 div.2 500 - RockPaperScissorsMagicEasy(DP)
- Topcoder SRM 462 DIV 2
- topcoder srm 518 div 2
- TopCoder SRM 561 Div 2
- Topcoder SRM 565 Div.2
- Topcoder SRM 628 DIV 2
- TopCoder SRM 144 DIV 2
- TopCoder SRM 613 Div.2 C DFS+剪枝
- topcoder SRM 654 DIV2 1000 SuccessiveSubtraction2 题解(dp)
- Topcoder SRM 651 div1 250 题解 (概率dp)
- Topcoder SRM 663 Div2 Hard: CheeseRolling(状压DP)
- 在 Ubuntu 12.04 上安装 Open vSwitch
- spring属性注入
- POJ 1080-Human Gene Functions(dp)
- filter不能过滤struts2中的action
- 滑轮控件研究三、GestureDetector的中手势事件的测试
- Topcoder SRM 648 Div.2 - A(水), B(枚举),C(DP/构造)
- Pascal's Triangle
- Uber 快的 滴滴
- 【转】php,asp与jsp的区别
- superfish的应用
- javascript中prototype方式面向对象编程注意事项
- 初创互联网团队如何利用开源技术和云服务快速建立网站
- 完美解决android scrollview嵌套listview
- linux实操常用命令总结