A. Apple

按题意模拟即可。

  1. #include<stdio.h>
  2. #include<iostream>
  3. #include<string.h>
  4. #include<string>
  5. #include<ctype.h>
  6. #include<math.h>
  7. #include<set>
  8. #include<map>
  9. #include<vector>
  10. #include<queue>
  11. #include<bitset>
  12. #include<algorithm>
  13. #include<time.h>
  14. using namespace std;
  15. void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
  16. #define MS(x, y) memset(x, y, sizeof(x))
  17. #define ls o<<1
  18. #define rs o<<1|1
  19. typedef long long LL;
  20. typedef unsigned long long UL;
  21. typedef unsigned int UI;
  22. template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b > a)a = b; }
  23. template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b < a)a = b; }
  24. const int N = 105, M = 0, Z = 1e9 + 7, inf = 0x3f3f3f3f;
  25. template <class T1, class T2>inline void gadd(T1 &a, T2 b) { a = (a + b) % Z; }
  26. int casenum, casei;
  27. int n, m;
  28. vector<int>a[N];
  29.  
  30. int ind1 = 0, ind1num = 0;
  31. int ind2num = 0;
  32. int ind3 = 0, ind3num = 0;
  33. bool vis[N];
  34. int ind[N];
  35.  
  36. int cnt;
  37. bool dfs1(int x, int fa)
  38. {
  39. ++cnt;
  40. vis[x] = 1;
  41. for(auto y : a[x])if(y != fa)
  42. {
  43. if(!vis[y])
  44. {
  45. if(!dfs1(y, x))return 0;
  46. }
  47. else if(y != ind3)return 0;
  48. }
  49. return 1;
  50. }
  51.  
  52. void dfs3(int x, int fa)
  53. {
  54. ++cnt;
  55. vis[x] = 1;
  56. for(auto y : a[x])if(y != fa)
  57. {
  58. if(!vis[y])
  59. {
  60. dfs3(y, x);
  61. }
  62. }
  63. }
  64.  
  65. bool solve()
  66. {
  67. if(ind1num != 1)return 0;
  68. if(ind2num != n - 2)return 0;
  69. if(ind3num != 1)return 0;
  70.  
  71. cnt = 0;
  72. vis[ind3] = true;
  73. if(!dfs1(ind1, 0))return 0;
  74. dfs3(ind3, 0);
  75. if(cnt != n)return 0;
  76.  
  77. return 1;
  78. }
  79. int main()
  80. {
  81. freopen("input.txt", "r", stdin);
  82. freopen("output.txt", "w", stdout);
  83. while(~scanf("%d%d", &n, &m))
  84. {
  85. for(int i = 1; i <= n; ++i)
  86. {
  87. a[i].clear();
  88. ind[i] = 0;
  89. vis[i] = 0;
  90. }
  91. for(int i = 1; i <= m; ++i)
  92. {
  93. int x, y; scanf("%d%d", &x, &y);
  94. a[x].push_back(y);
  95. a[y].push_back(x);
  96. ++ind[x];
  97. ++ind[y];
  98. }
  99. ind1 = 0, ind1num = 0;
  100. ind2num = 0;
  101. ind3 = 0, ind3num = 0;
  102. for(int i = 1; i <= n; ++i)
  103. {
  104. if(ind[i] == 1)
  105. {
  106. ++ind1num;
  107. ind1 = i;
  108. }
  109. else if(ind[i] == 2)
  110. {
  111. ++ind2num;
  112. }
  113. else if(ind[i] == 3)
  114. {
  115. ++ind3num;
  116. ind3 = i;
  117. }
  118. }
  119. puts(solve() ? "Yes" : "No");
  120. }
  121.  
  122. return 0;
  123. }
  124. /*
  125. 【trick&&吐槽】
  126.  
  127. 【题意】
  128.  
  129. 【分析】
  130.  
  131. 【时间复杂度&&优化】
  132. 9 9
  133. 1 2
  134. 2 3
  135. 3 5
  136. 5 6
  137. 6 4
  138. 4 7
  139. 1 7
  140. 7 9
  141. 8 9
  142.  
  143. 5 5
  144. 1 2
  145. 2 3
  146. 1 3
  147. 1 4
  148. 1 5
  149.  
  150. 4 4
  151. 1 2
  152. 2 3
  153. 1 3
  154. 1 4
  155.  
  156. 5 4
  157. 1 2
  158. 2 3
  159. 1 3
  160. 1 4
  161.  
  162. */

  

B. Bar charts

关于序列的前缀和建立差分约束系统,SPFA判断是否存在负环。

  1. #include<cstdio>
  2. #include<cstdlib>
  3. using namespace std;
  4. const int N=12222,M=1000000,E=10000000,inf=~0U>>1;
  5. int n,i,g[N],v[M],w[M],nxt[M],ed,d[N];
  6. int x,h,t,q[E];
  7. int vis[N],in[N];
  8. inline void add(int x,int y,int z){v[++ed]=y;w[ed]=z;nxt[ed]=g[x];g[x]=ed;}
  9. inline void make(int x,int y,int z){
  10. //x-y=z
  11. add(y,x,z);
  12. add(x,y,-z);
  13. }
  14. void gao(){
  15. int k,b,s;
  16. scanf("%d%d%d",&k,&b,&s);
  17. make(b-1,0,0);
  18. for(int i=b+k*s;i<=n;i++)make(i,i-1,0);
  19. for(int i=1;i<=k;i++){
  20. int l=b+(i-1)*s,r=b+i*s;
  21. int o;
  22. scanf("%d",&o);
  23. //s[r-1]-s[l-1]
  24. make(r-1,l-1,o);
  25. }
  26. }
  27. void NO(){
  28. puts("No");
  29. exit(0);
  30. }
  31. inline void ext(int x,int y){
  32. if(y<0)NO();
  33. if(y>=d[x])return;
  34. d[x]=y;
  35. if(!in[x]){
  36. in[x]=1;
  37. vis[x]++;
  38. if(vis[x]>=n)NO();
  39. q[++t]=x;
  40. if(t>=E-10)NO();
  41. }
  42. }
  43. int main(){
  44. freopen("input.txt","r",stdin);
  45. freopen("output.txt","w",stdout);
  46. n=11111;
  47. for(i=0;i<n;i++)add(i+1,i,0);
  48. gao();
  49. gao();
  50. for(i=0;i<=n;i++){
  51. d[i]=inf;
  52. }
  53. h=1,t=0;
  54. ext(0,0);
  55. while(h<=t){
  56. x=q[h++];
  57. for(i=g[x];i;i=nxt[i])ext(v[i],d[x]+w[i]);
  58. in[x]=0;
  59. }
  60. puts("Yes");
  61. }

  

C. Construction sets

二分答案,二进制拆分背包+bitset检验。

  1. #include<stdio.h>
  2. #include<iostream>
  3. #include<string.h>
  4. #include<string>
  5. #include<ctype.h>
  6. #include<math.h>
  7. #include<set>
  8. #include<map>
  9. #include<vector>
  10. #include<queue>
  11. #include<bitset>
  12. #include<algorithm>
  13. #include<time.h>
  14. using namespace std;
  15. void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
  16. #define MS(x, y) memset(x, y, sizeof(x))
  17. #define ls o<<1
  18. #define rs o<<1|1
  19. typedef long long LL;
  20. typedef unsigned long long UL;
  21. typedef unsigned int UI;
  22. template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b > a)a = b; }
  23. template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b < a)a = b; }
  24. const int N = 55, M = 0, Z = 1e9 + 7, inf = 0x3f3f3f3f;
  25. template <class T1, class T2>inline void gadd(T1 &a, T2 b) { a = (a + b) % Z; }
  26. int casenum, casei;
  27. int n, mn, mx;
  28. int m[N], c[N];
  29. bitset<10005>f;
  30. bool check(int K)
  31. {
  32. f.reset(); f[0] = 1;
  33. for(int i = 1; i <= n; ++i)
  34. {
  35. int tot = c[i] / K;
  36. int g = 1;
  37. LL v = m[i];
  38. while(tot)
  39. {
  40. if(v > mx)break;
  41. f |= f << v;
  42. tot -= g;
  43. g = min(g * 2, tot);
  44. v = (LL)m[i] * g;
  45. }
  46. }
  47. for(int j = mn; j <= mx; ++j)
  48. {
  49. if(f[j])return 1;
  50. }
  51. return 0;
  52. }
  53. int main()
  54. {
  55. freopen("input.txt", "r", stdin);
  56. freopen("output.txt", "w", stdout);
  57. while(~scanf("%d%d%d", &n, &mn, &mx))
  58. {
  59. for(int i = 1; i <= n; ++i)
  60. {
  61. scanf("%d%d", &m[i], &c[i]);
  62. }
  63. int l = 0;
  64. int r = 1e6;
  65. while(l < r)
  66. {
  67. int mid = (l + r + 1) >> 1;
  68. if(check(mid))
  69. {
  70. l = mid;
  71. }
  72. else
  73. {
  74. r = mid - 1;
  75. }
  76. }
  77. printf("%d\n", l);
  78. }
  79.  
  80. return 0;
  81. }
  82. /*
  83. 【trick&&吐槽】
  84.  
  85. 【题意】
  86.  
  87. 【分析】
  88.  
  89. 【时间复杂度&&优化】
  90. 3 12 13
  91. 3 8
  92. 4 6
  93. 7 9
  94.  
  95. */

  

D. Dinner party

$f[i][j]$表示面积和为$i$的矩形,周长和为$j$是否有可能,bitset加速。

  1. #include<cstdio>
  2. #include<bitset>
  3. using namespace std;
  4. const int N=1010,M=2010;
  5. int T,n,m,i,j,x,y,cnt;
  6. bitset<M>f[N];
  7. void solve(){
  8. scanf("%d%d",&n,&m);
  9. if(m%2){
  10. puts("No");
  11. return;
  12. }
  13. m/=2;
  14. if(f[n][m]==0){
  15. puts("No");
  16. return;
  17. }
  18. puts("Yes");
  19. int cnt=0;
  20. static int q[100000][2];
  21. while(n){
  22. int X=0,Y=0;
  23. for(x=1;x<=n;x++){
  24. for(y=1;y*x<=n;y++)if(x+y<=m)if(f[n-x*y][m-x-y]){
  25. X=x,Y=y;
  26. break;
  27. }
  28. if(X)break;
  29. }
  30. q[++cnt][0]=X;
  31. q[cnt][1]=Y;
  32. n-=X*Y;
  33. m-=X+Y;
  34. }
  35. printf("%d\n",cnt);
  36. for(int i=1;i<=cnt;i++)printf("%d %d\n",q[i][0],q[i][1]);
  37. }
  38. int main(){
  39. freopen("input.txt","r",stdin);
  40. freopen("output.txt","w",stdout);
  41. //scanf("%d%d",&n,&m);
  42. n=1000;
  43. m=2000;
  44. f[0][0]=1;
  45. for(i=0;i<=n;i++)
  46. for(x=1;x+i<=n;x++)
  47. for(y=1;x*y+i<=n;y++)
  48. f[i+x*y]|=f[i]<<(x+y);
  49. scanf("%d",&T);
  50. while(T--)solve();
  51. }

  

E. Expression on dice

若要构造$=$,则在两侧构造$0$即可,否则随意构造。

  1. //
  2. // main.cpp
  3. // opencup 10378 E
  4. //
  5. // Created by luras on 2017/9/14.
  6. // Copyright © 2017年 luras. All rights reserved.
  7. //
  8.  
  9. #define ms(x, y) memset(x, y, sizeof(x))
  10. #define mc(x, y) memcpy(x, y, sizeof(x))
  11. #define lson o << 1, l, mid
  12. #define rson o << 1 | 1, mid + 1, r
  13. #define ls o << 1
  14. #define rs o << 1 | 1
  15. #include<stdio.h>
  16. #include<string.h>
  17. #include<math.h>
  18. #include<queue>
  19. #include<map>
  20. #include<stack>
  21. #include<time.h>
  22. #include<vector>
  23. #include<list>
  24. #include<set>
  25. #include<iostream>
  26. #include<stdlib.h>
  27. #include<string>
  28. #include<algorithm>
  29. #pragma comment(linker,"/STACK:102400000,102400000")
  30. template <class T> inline void gmax(T &a, T b){if(b > a) a = b;}
  31. template <class T> inline void gmin(T &a, T b){if(b < a) a = b;}
  32. using namespace std;
  33. const int N = 1e6 + 10, M = 2e6 + 10, Z = 1e9 + 7, maxint = 2147483647, ms1 = 16843009, ms31 = 522133279, ms63 = 1061109567, ms127 = 2139062143;
  34. const double PI = acos(-1.0), eps = 1e-8;
  35. typedef long long LL;
  36. void fre()
  37. {
  38. freopen("/Users/luras/Desktop/in.txt", "r", stdin);
  39. freopen("/Users/luras/Desktop/out.txt", "w", stdout);
  40. }
  41. const int INF = 1e9;
  42. int casenum, casei;
  43.  
  44. int priv[N];
  45. double calc(double a, double b, char op)
  46. {
  47. if(op == '+') return a + b;
  48. if(op == '-') return a - b;
  49. if(op == '*') return a * b;
  50. if(op == '/') return a / b;
  51. return 0;
  52. }
  53.  
  54. double calculate(string str)
  55. {
  56. stack<double> num;
  57. stack<char> oper;
  58. priv['+'] = priv['-'] = 3;
  59. priv['*'] = priv['/'] = 2;
  60. priv['('] = 10;
  61. double x, y, t = 0;
  62. int i; char last = 0;
  63. for(i = 0; i < str.length(); i ++){
  64. if(isdigit(str[i])){
  65. num.push(atof(str.c_str() + i));
  66. for(; i + 1 < str.length() && isdigit(str[i + 1]); i ++);
  67. if(i + 1 < str.length() && str[i + 1] == '.')
  68. for(i ++; i + 1 < str.length() && isdigit(str[i + 1]); i ++);
  69. }
  70. else if(str[i] == '('){
  71. oper.push(str[i]);
  72. }
  73. else if(str[i] == ')'){
  74. while(oper.top() != '('){
  75. y = num.top(); num.pop();
  76. x = num.top(); num.pop();
  77. char op = oper.top();
  78. oper.pop();
  79. num.push(calc(x, y, op));
  80. }
  81. oper.pop();
  82. }
  83. else if(str[i] == '-' && (last == 0 || last == '(')){
  84. num.push(0.0);
  85. oper.push('-');
  86. }
  87. else if(priv[str[i]] > 0){
  88. while(oper.size() > 0 && priv[str[i]] >= priv[oper.top()]){
  89. y = num.top(); num.pop();
  90. x = num.top(); num.pop();
  91. char op = oper.top();
  92. oper.pop();
  93. num.push(calc(x, y, op));
  94. }
  95. oper.push(str[i]);
  96. }else continue;
  97. last = str[i];
  98. }
  99. while(oper.size() > 0){
  100. y = num.top(); num.pop();
  101. x = num.top(); num.pop();
  102. char op = oper.top();
  103. oper.pop();
  104. num.push(calc(x, y, op));
  105. }
  106. return num.top();
  107. }
  108.  
  109. map<int, bool> mop;
  110. vector<int> num;
  111. vector<char> sym;
  112. int lft, rgt, zero, divi, typ;
  113.  
  114. const string sta[6][6] ={
  115. {"=", "<", ">", "!=", "<=", ">="},
  116. {"4", "+", "-", "(", "(", ")"},
  117. {"0", "/", "/", "/", "8", "+"},
  118. {"2", "3", "4", "5", "-", ")"},
  119. {"+", "-", "*", "/", "1", "9"},
  120. {"6", "7", "+", "-", "(", ")"}
  121. };
  122.  
  123. void ask(int o)
  124. {
  125. char ch;
  126. if(o == 2){
  127. puts("2");
  128. fflush(stdout);
  129. scanf(" %c", &ch);
  130. //ch = sta[1][rand() % 6][0];
  131. //cout << "A: " << ch;
  132. if(ch == '(') lft ++;
  133. else if(ch == ')') rgt ++;
  134. else if(ch == '4') {
  135. num.push_back(ch);
  136. if(mop[ch] == 0){
  137. mop[ch] = 1;
  138. typ ++;
  139. }
  140. }
  141. else sym.push_back(ch);
  142. }
  143. else if(o == 3){
  144. puts("3");
  145. fflush(stdout);
  146. scanf(" %c", &ch);
  147. //ch = sta[2][rand() % 6][0];
  148. //cout << "A: " << ch;
  149.  
  150. if(ch == '0') zero ++;
  151. else if(ch == '8') {
  152. num.push_back(ch);
  153. if(mop[ch] == 0){
  154. mop[ch] = 1;
  155. typ ++;
  156. }
  157. }
  158. else if(ch == '/') divi ++;
  159. else sym.push_back(ch);
  160. }
  161. else if(o == 4){
  162. puts("4");
  163. fflush(stdout);
  164. //ch = sta[3][rand() % 6][0];
  165. //cout << "A: " << ch;
  166.  
  167. scanf(" %c", &ch);
  168. if(ch == '-') sym.push_back(ch);
  169. else if(ch == ')') rgt ++;
  170. else {
  171. num.push_back(ch);
  172. if(mop[ch] == 0){
  173. mop[ch] = 1;
  174. typ ++;
  175. }
  176. }
  177. }
  178. }
  179.  
  180. string ans;
  181. string s;
  182. string ss;
  183.  
  184. int sgn(double x)
  185. {
  186. if(fabs(x) < eps) return 0;
  187. return x > 0 ? 1 : -1;
  188. }
  189.  
  190. bool choose()
  191. {
  192. s = "";
  193. int DIV = divi, ZERO = zero;
  194. int n = num.size(), m = sym.size() + DIV;
  195. int bas = (n + ZERO) / (m + 1);
  196. int mo = (n + ZERO) - (m + 1) * bas;
  197. int t = 0, j = -1, k = -1;
  198. for(int i = 0; i < m + 1; i ++){
  199. if(mo){
  200. if(j + 1 >= n) return 0; // 必须得放前导0了
  201. s += num[++ j]; ++ t;
  202. while(t <= bas && ZERO){
  203. s += '0'; t ++; ZERO --;
  204. }
  205. while(t <= bas){
  206. s += num[++ j]; t ++;
  207. }
  208. mo --;
  209. t = 0;
  210. }
  211. else{
  212. if(j + 1 >= n) return 0;
  213. s += num[++ j]; ++ t;
  214. while(t < bas && ZERO){
  215. s += '0'; t ++; ZERO --;
  216. }
  217. while(t < bas){
  218. s += num[++ j]; t ++;
  219. }
  220. t = 0;
  221. }
  222. if(DIV){
  223. s += '/';
  224. DIV --;
  225. }
  226. else{
  227. if(k + 1 < sym.size())s += sym[++ k];
  228. }
  229. }
  230. return 1;
  231. }
  232.  
  233. bool check(int o)
  234. {
  235. if(o == 0){
  236. zero -= 2; lft --; rgt --; divi --;
  237. if(!choose()) {zero += 2; lft ++; rgt ++; divi ++; return 0;}
  238. if(calculate(s) != 0){
  239. ans = "";
  240. for(int i = 0; i < lft; i ++) ans += '(';
  241. ans += '0';
  242. for(int i = 0; i < rgt; i ++) ans += ')';
  243. ans += "=0/(" + s + ')';
  244. }
  245. else {zero += 2; lft ++; rgt ++; divi ++; return 0;}
  246. }
  247. else{
  248. zero --;
  249. if(!choose()) {zero ++;return 0;}
  250. double tmp = calculate(s);
  251. if(sgn(tmp) != 0){
  252. if(ss[0] == '<' && sgn(tmp) > 0 || ss[0] == '>' && sgn(tmp) < 0){
  253. ans = "";
  254. for(int i = 0; i < lft; i ++) ans += '(';
  255. ans += '0';
  256. for(int i = 0; i < lft; i ++) ans += ')';
  257. ans += ss + s;
  258. }
  259. else{
  260. ans = "";
  261. for(int i = 0; i < lft; i ++) ans += '(';
  262. ans += s;
  263. for(int i = 0; i < lft; i ++) ans += ')';
  264. ans += ss + '0';
  265. }
  266. }
  267. else if(ss == "!="){
  268. ans = "";
  269. for(int i = 0; i < lft; i ++) ans += '(';
  270. ans += '0';
  271. for(int i = 0; i < lft; i ++) ans += ')';
  272. ans += ss + s;
  273.  
  274. }
  275. else {zero ++; return 0;}
  276. }
  277. return 1;
  278. }
  279.  
  280. int main()
  281. {
  282. srand(time(NULL));
  283. //fre();
  284. puts("1");
  285. fflush(stdout);
  286. cin >> ss;
  287. //ss = sta[0][rand() % 6];
  288. //cout << ss << endl;
  289. lft = rgt = zero = divi = typ = 0;
  290. int tim = 0;
  291. while(1){
  292. if(++ tim == 1000){
  293. int go = 1;
  294. }
  295. if(zero < (ss[0] == '=' ? 2 : 1) || divi == 0){
  296. ask(3);
  297. }
  298. else if(lft < rgt || lft == 0){
  299. ask(2);
  300. }
  301. else if(num.size() - 2 < sym.size() || rgt < lft){
  302. ask(4);
  303. }
  304. else {
  305. if(check(ss[0] == '=' ? 0 : 1)) break;
  306. ask(4);
  307. }
  308. }
  309. printf("0 ");
  310. cout << ans << endl;
  311. fflush(stdout);
  312. return 0;
  313. }
  314.  
  315. /*
  316.  
  317. 题意:
  318.  
  319. 类型:
  320.  
  321. 分析:
  322.  
  323. 优化:
  324.  
  325. trick:
  326.  
  327. 数据:
  328.  
  329. Sample Input
  330.  
  331. Sample Output
  332.  
  333. >=
  334. + / / + 8 0 ) ( ) ( - - 5 5 5 - 4 4 4 - 5 3
  335.  
  336. */

  

F. Flight trip

留坑。

G. Glasses with solutions

找出的子集需要满足$b\sum m-a\sum t=0$,折半搜索即可。

  1. #include<cstdio>
  2. #include<map>
  3. using namespace std;
  4. typedef long long ll;
  5. const int N=40;
  6. int n,m,A,B,i,x,y,a[N];map<ll,ll>f;
  7. ll ans;
  8. void dfsl(int x,ll y){
  9. if(x==m){f[y]++;return;}
  10. dfsl(x+1,y+a[x]);
  11. dfsl(x+1,y);
  12. }
  13. void dfsr(int x,ll y){
  14. if(x==n){ans+=f[-y];return;}
  15. dfsr(x+1,y+a[x]);
  16. dfsr(x+1,y);
  17. }
  18. int main(){
  19. freopen("input.txt","r",stdin);
  20. freopen("output.txt","w",stdout);
  21. scanf("%d%d%d",&n,&A,&B);
  22. for(i=0;i<n;i++){
  23. scanf("%d%d",&x,&y);
  24. a[i]=x*B-A*y;
  25. }
  26. m=n/2;
  27. dfsl(0,0);
  28. dfsr(m,0);
  29. ans--;
  30. printf("%lld",ans);
  31. }

  

H. Hamburgers

对于每个汉堡,$O(2^6)$枚举所有它可以满足的口味,然后对于每个人判断是否满足即可。

时间复杂度$O(m\sum a+2^6\sum b)$。

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<vector>
  4. using namespace std;
  5. const int N=55555;
  6. int n,m,i,j,k,x,ans[N],mx[N],b[N];
  7. vector<int>a[N];
  8. bool v[(1<<26)+5];
  9. inline int get(){
  10. static char s[1000];
  11. scanf("%s",s);
  12. int t=0,len=strlen(s);
  13. for(int i=0;i<len;i++)t|=1<<(s[i]-'a');
  14. return t;
  15. }
  16. inline void make(int x,int y){
  17. for(int i=x;i;i=(i-1)&x)v[i]=y;
  18. }
  19. int main(){
  20. freopen("input.txt","r",stdin);
  21. freopen("output.txt","w",stdout);
  22. scanf("%d",&n);
  23. for(i=1;i<=n;i++){
  24. scanf("%d",&k);
  25. while(k--){
  26. x=get();
  27. a[i].push_back(x);
  28. }
  29. ans[i]=1;
  30. }
  31. scanf("%d",&m);
  32. for(i=1;i<=m;i++){
  33. scanf("%d",&k);
  34. for(j=1;j<=k;j++)b[j]=get();
  35. for(j=1;j<=k;j++)make(b[j],1);
  36. for(j=1;j<=n;j++){
  37. int t=0;
  38. for(x=0;x<a[j].size();x++)if(v[a[j][x]])t++;
  39. if(t>mx[j])mx[j]=t,ans[j]=i;
  40. }
  41. for(j=1;j<=k;j++)make(b[j],0);
  42. }
  43. for(i=1;i<=n;i++)printf("%d\n",ans[i]);
  44. }
  45. /*
  46. 5
  47. 1
  48. a
  49. 3
  50. a
  51. ba
  52. cb
  53. 3
  54. vba
  55. d
  56. ba
  57. 3
  58. ca
  59. da
  60. da
  61. 3
  62. as
  63. ba
  64. af
  65. 2
  66. 4
  67. abc
  68. abcd
  69. a
  70. a
  71. 2
  72. abcdef
  73. abcdev
  74. */

  

I. Intricate path

留坑。

J. Jumps through the Hyperspace

显然到每个点的时间越早越好,对于每种传送门预处理出每种余数下的最优等待时间即可。

  1. #include<cstdio>
  2. #include<algorithm>
  3. #include<vector>
  4. #include<queue>
  5. using namespace std;
  6. typedef pair<int,int>P;
  7. const int N=2010,inf=~0U>>1;
  8. int n,m,st,i,j,a[N],b[N],c[N],d[N],w[N][N];
  9. int f[N];
  10. char g[N][N],op[N];
  11. priority_queue<P,vector<P>,greater<P> >q;
  12. inline void ext(int x,int y){
  13. if(f[x]>y)q.push(P(f[x]=y,x));
  14. }
  15. int main(){
  16. freopen("input.txt","r",stdin);
  17. freopen("output.txt","w",stdout);
  18. scanf("%d%d%d",&n,&m,&st);
  19. while(m--){
  20. scanf("%s",op);
  21. int o=op[0];
  22. scanf("%d%d%d%d",&a[o],&b[o],&c[o],&d[o]);
  23. for(i=0;i<c[o];i++){
  24. w[o][i]=inf;
  25. for(j=0;j<c[o];j++)w[o][i]=min(w[o][i],(a[o]*(i+j)+b[o])%c[o]+d[o]+j);
  26. }
  27. }
  28. for(i=1;i<=n;i++)scanf("%s",g[i]+1);
  29. for(i=1;i<=n;i++)f[i]=inf;
  30. ext(1,st);
  31. while(!q.empty()){
  32. P t=q.top();q.pop();
  33. if(f[t.second]<t.first)continue;
  34. for(i=1;i<=n;i++)if(g[t.second][i]!='.')
  35. ext(i,t.first+w[g[t.second][i]][t.first%c[g[t.second][i]]]);
  36. }
  37. if(f[n]==inf)puts("-1");else
  38. printf("%d",f[n]-st);
  39. }

  

K. King’s island

暴力搜索出一个上凸壳,然后翻转拼接起来得到完整的多边形即可。

  1. #include<stdio.h>
  2. #include<iostream>
  3. #include<string.h>
  4. #include<string>
  5. #include<ctype.h>
  6. #include<math.h>
  7. #include<set>
  8. #include<map>
  9. #include<vector>
  10. #include<queue>
  11. #include<bitset>
  12. #include<algorithm>
  13. #include<time.h>
  14. using namespace std;
  15. void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
  16. #define MS(x, y) memset(x, y, sizeof(x))
  17. #define ls o<<1
  18. #define rs o<<1|1
  19. typedef long long LL;
  20. typedef unsigned long long UL;
  21. typedef unsigned int UI;
  22. template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b > a)a = b; }
  23. template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b < a)a = b; }
  24. const int N = 1010, M = 0, Z = 1e9 + 7, inf = 0x3f3f3f3f;
  25. template <class T1, class T2>inline void gadd(T1 &a, T2 b) { a = (a + b) % Z; }
  26. int casenum, casei;
  27. int n;
  28. struct B
  29. {
  30. int x, y;
  31. bool operator < (const B & b)const
  32. {
  33. //y/x > b.y/b.x
  34. return y * b.x > x * b.y;
  35. }
  36. };
  37. B p[100000]; int g;
  38. int f[N][N][62];
  39. struct A
  40. {
  41. int x, y, z;
  42. };
  43. bool bingo[60];
  44. vector<B>ans[60];
  45.  
  46. void getvt(int x, int y, int v, vector<B>&vt, int DX, int DY)
  47. {
  48. if(v == 0)return;
  49. int o = f[x][y][v];
  50. if(f[x - p[o].x][y - p[o].y][v] == o)
  51. {
  52. getvt(x - p[o].x, y - p[o].y, v, vt, DX + p[o].x, DY + p[o].y);
  53. }
  54. else
  55. {
  56. vt.push_back({p[o].x + DX, p[o].y + DY});
  57. getvt(x - p[o].x, y - p[o].y, v - 1, vt, 0, 0);
  58. }
  59. }
  60.  
  61. void init()
  62. {
  63. for(int i = 1; i <= 200; ++i)
  64. {
  65. for(int j = i + 1; j <= 200; ++j)if(__gcd(i, j) == 1)
  66. {
  67. int W = (i * i + j * j);
  68. int w = sqrt(W);
  69. if(w * w == W)
  70. {
  71. p[++g] = {i, j};
  72. p[++g] = {j, i};
  73. }
  74. }
  75. }
  76. sort(p + 1, p + g + 1);
  77. //printf("%d\n", g);
  78.  
  79. MS(f, 0);
  80. f[0][0][0] = 1;
  81. for(int i = 0; i <= 500; ++i)
  82. {
  83. for(int j = 0; j <= 500; ++j)
  84. {
  85. for(int v = 29; v >= 0; --v)if(f[i][j][v])
  86. {
  87. //
  88. int k = f[i][j][v];
  89. auto &it = f[i + p[k].x][j + p[k].y][v];
  90. if(it == 0)it = k;
  91. //
  92. for(int k = f[i][j][v] + 1; k <= g; ++k)
  93. {
  94. auto &it = f[i + p[k].x][j + p[k].y][v + 1];
  95. if(it == 0)it = k;
  96. }
  97. if(i == 0 || j == 0)continue;
  98. for(int u = 1; u + v <= 30; ++u)if(f[i][j][u] && f[i][j][u] != f[i][j][v])
  99. {
  100. if(!bingo[u + v])
  101. {
  102. bingo[u + v] = 1;
  103. vector<B>up, down;
  104. getvt(i, j, u, up, 0, 0);
  105. getvt(i, j, v, down, 0, 0);
  106. int x = i;
  107. int y = j;
  108. //printf("%d %d %d\n", u + v, up.size(), down.size());
  109. //puts("up------------");
  110. for(int r = 0; r < up.size(); ++r)
  111. {
  112. x -= up[r].x;
  113. y -= up[r].y;
  114. ans[u + v].push_back({x, y});
  115. //printf("%d %d\n", up[r].x, up[r].y);
  116. }
  117. //puts("down------------");
  118. for(int r = 0; r < down.size(); ++r)
  119. {
  120. x += down[r].x;
  121. y += down[r].y;
  122. ans[u + v].push_back({x, y});
  123. //printf("%d %d\n", down[r].x, down[r].y);
  124. }
  125. //puts("ans------");
  126. for(auto w : ans[u + v])
  127. {
  128. //printf("%d %d\n", w.x, w.y);
  129. }
  130. int pasue = 1;
  131. }
  132. }
  133. }
  134. }
  135. }
  136. for(int i = 1;i <= 30; ++i)
  137. {
  138. //printf("%d\n", bingo[i]);
  139. }
  140. }
  141. int main()
  142. {
  143. freopen("input.txt", "r", stdin);
  144. freopen("output.txt", "w", stdout);
  145. init();
  146. while(~scanf("%d", &n))
  147. {
  148. if(n == 3)
  149. {
  150. puts("0 0\n4 3\n-20 21");
  151. continue;
  152. }
  153. for(auto it : ans[n])
  154. {
  155. printf("%d %d\n", it.x, it.y);
  156. }
  157. }
  158.  
  159. return 0;
  160. }
  161. /*
  162. 【trick&&吐槽】
  163.  
  164. 【题意】
  165.  
  166. 【分析】
  167.  
  168. 【时间复杂度&&优化】
  169. 28 195
  170. 9 15
  171. -116 178
  172. 4
  173. 20 21
  174. 1 -159
  175. -236 -11
  176. -92 6
  177. 5
  178. 20 50
  179. 3 -94
  180. -16 -274
  181. -236 18
  182. -92 35
  183.  
  184. */

  

L. Lexica

无视同一行/同一列的两个格子的位置关系,只需要在它们不相同时将方案数乘$2$,如此一来只关心每一行/每一列有多少格子没满足。

设$f[i][j][S][k]$表示考虑到$(i,j)$,每一列剩余情况为$S$,第$i$行还有$k$个格子需要填充时的方案数,然后逐格转移即可。

时间复杂度$O(n3^n)$。

  1. #include<cstdio>
  2. typedef unsigned long long ll;
  3. const int N=18,M=1600000;
  4. int n,m,all,i,j,k,x,y,z,A,B,C,S,o,p[N],mask,cnt[N];
  5. char a[N][N];
  6. int e;
  7. ll f[2][M][3],mul;
  8. int w[M];
  9. inline void clr(){
  10. for(int i=0;i<=mask;i++)for(int j=0;j<3;j++)f[e^1][i][j]=0;
  11. }
  12. inline void nxt(){
  13. //for(int i=0;i<=mask;i++)for(int j=0;j<3;j++)f[i][j]=g[i][j];
  14. }
  15. inline int get(int S,int x){
  16. return S/p[x]%3;
  17. }
  18. void dfs(int x,int y,int z){
  19. if(x==n){
  20. w[y]=z;
  21. return;
  22. }
  23. for(int i=0;i<3;i++)dfs(x+1,y*3+i,z*2+(!!i));
  24. }
  25. int main(){
  26. freopen("input.txt","r",stdin);
  27. freopen("output.txt","w",stdout);
  28. scanf("%d",&n);
  29. m=n+2;
  30. for(i=0;i<m;i++)scanf("%s",a[i]);
  31. mul=1;
  32. for(p[0]=i=1;i<N;i++)p[i]=p[i-1]*3;
  33. all=p[n];
  34. for(i=1;i<=n;i++){
  35. if(a[0][i]!='#'&&a[m-1][i]!='#'&&a[0][i]!=a[m-1][i])mul*=2;
  36. if(a[i][0]!='#'&&a[i][m-1]!='#'&&a[i][0]!=a[i][m-1])mul*=2;
  37. if(a[i][0]!='#')cnt[i]++;
  38. if(a[i][m-1]!='#')cnt[i]++;
  39. if(a[0][i]!='#')mask+=p[i-1];
  40. if(a[m-1][i]!='#')mask+=p[i-1];
  41. }
  42. dfs(0,0,0);
  43. f[0][mask][0]=mul;
  44. for(i=1;i<=n;i++){
  45. clr();
  46. int o=cnt[i];
  47. for(A=0;A<=mask;A++)f[e^1][A][o]=f[e][A][0];
  48. e^=1;
  49. for(j=0;j<n;j++)if(a[i][j+1]=='.'){
  50. clr();
  51. for(S=0;S<=mask;S++){
  52. if(w[S]>>j&1)for(k=0;k<=o;k++)if(f[e][S][k])f[e^1][S-p[j]][k]+=f[e][S][k];
  53. for(k=1;k<=o;k++)if(f[e][S][k])f[e^1][S][k-1]+=f[e][S][k];
  54. }
  55. e^=1;
  56. }
  57. }
  58. printf("%llu",f[e][0][0]);
  59. }
  60. /*
  61. 5
  62. ##ARRR#
  63. H.#.##S
  64. Y.....E
  65. O.#.#.E
  66. ##....E
  67. ###.#.E
  68. #XNT#A#
  69. */

  

M. Maximal paths

树形DP求出每个点内部从叶子往上、从上往下的最大数字串即可。需要手写高精度。

  1. #include<cstdio>
  2. #include<algorithm>
  3. using namespace std;
  4. typedef unsigned long long ll;
  5. typedef __int128 lll;
  6. const int N=10000010,M=50;
  7. const ll MO=1000000000000000000ULL;
  8. int n,i;char a[N];
  9. struct P{
  10. ll v[3];
  11. P(){v[0]=v[1]=v[2]=0;}
  12. P operator+(int b){
  13. P c;
  14. for(int i=0;i<3;i++)c.v[i]=v[i];
  15. c.v[0]+=b;
  16. for(int i=0;i<2;i++)if(c.v[i]>=MO){
  17. c.v[i+1]+=c.v[i]/MO;
  18. c.v[i]-=MO;
  19. }
  20. return c;
  21. }
  22. P operator+(const P&b){
  23. P c;
  24. for(int i=0;i<3;i++)c.v[i]=v[i]+b.v[i];
  25. for(int i=0;i<2;i++)if(c.v[i]>=MO){
  26. c.v[i+1]+=c.v[i]/MO;
  27. c.v[i]-=MO;
  28. }
  29. return c;
  30. }
  31. P operator*(int b){
  32. P c;
  33. for(int i=0;i<3;i++)c.v[i]=v[i]*b;
  34. for(int i=0;i<2;i++)c.v[i+1]+=c.v[i]/MO,c.v[i]%=MO;
  35. c.v[2]%=MO;
  36. return c;
  37. }
  38. P operator*(const P&b){
  39. P c;
  40. static lll f[3];
  41. f[0]=f[1]=f[2]=0;
  42. for(int i=0;i<3;i++)if(v[i])for(int j=0;i+j<3;j++)if(b.v[j])f[i+j]+=(lll)v[i]*b.v[j];
  43. for(int i=0;i<2;i++)if(f[i]>=MO)f[i+1]+=f[i]/MO,f[i]%=MO;
  44. if(f[2]>=MO)f[2]%=MO;
  45. for(int i=0;i<3;i++)c.v[i]=f[i];
  46. return c;
  47. }
  48. void up(const P&b){//max=
  49. int i;
  50. for(i=2;~i;i--)if(b.v[i]!=v[i])break;
  51. if(i<0)return;
  52. if(b.v[i]<v[i])return;
  53. for(i=0;i<3;i++)v[i]=b.v[i];
  54. }
  55. void write(){
  56. int i=2;
  57. while(i&&!v[i])i--;
  58. printf("%llu",v[i]);
  59. for(int j=i-1;~j;j--)printf("%018llu",v[j]);
  60. puts("");
  61. }
  62. }f[M],g[M],p[M],ans,ff,gg,mx[M];
  63. int d[M];
  64. unsigned int seed,base;
  65. void dfs(int x,int k){
  66. f[k]=g[k]=mx[k]=P();
  67. d[k]=0;
  68. for(int i=0;i<2;i++){
  69. int y=x<<1|i;
  70. if(y>n)continue;
  71. dfs(y,k+1);
  72. ff=(f[k+1]*10)+((int)a[y]);
  73. gg=g[k+1]+(p[d[k+1]]*((int)a[y]));
  74. mx[k].up((ff*p[d[k]])+g[k]);
  75. mx[k].up((f[k]*p[d[k+1]+1])+gg);
  76. /*if(x==1){
  77. puts("debug");
  78. ff.write();
  79. f[k].write();
  80. }*/
  81. f[k].up(ff);
  82. g[k].up(gg);
  83. d[k]=max(d[k],d[k+1]+1);
  84. /*if(x==1){
  85. puts("debug");
  86. ff.write();
  87. f[k].write();
  88. }*/
  89. }
  90. /* printf("%d:\n",x);
  91. f[k].write();
  92. g[k].write();
  93. mx[k].write();*/
  94. ans=ans+mx[k];
  95. }
  96. int main(){
  97. freopen("input.txt","r",stdin);
  98. freopen("output.txt","w",stdout);
  99. scanf("%d%u",&n,&seed);
  100. base=(1U<<31)-1;
  101. p[0].v[0]=1;
  102. for(i=1;i<M;i++)p[i]=p[i-1]*10;
  103. for(i=1;i<=n;i++){
  104. a[i]=((seed&base)>>16)%9+1;
  105. seed=seed*1103515245+12345;
  106. //printf("%d\n",a[i]); i and i/2
  107. }
  108. dfs(1,0);
  109. ans.write();
  110. }

  

XVII Open Cup named after E.V. Pankratiev. XXI Ural Championship的更多相关文章

  1. XVII Open Cup named after E.V. Pankratiev. GP of SPb

    A. Array Factory 将下标按前缀和排序,然后双指针,维护最大的右边界即可. #include<cstdio> #include<algorithm> using ...

  2. XVII Open Cup named after E.V. Pankratiev. GP of Two Capitals

    A. Artifact Guarding 选出的守卫需要满足$\max(a+b)\leq \sum a$,从小到大枚举每个值作为$\max(a+b)$,在权值线段树上找到最大的若干个$a$即可. 时间 ...

  3. XVII Open Cup named after E.V. Pankratiev. GP of Moscow Workshops

    A. Centroid Tree 枚举至多两个重心作为根,检查对于每个点是否都满足$2size[x]\leq size[father[x]]$即可. #include<stdio.h> # ...

  4. XVII Open Cup named after E.V. Pankratiev. Eastern GP, Division 1

    A. Count The Ones $ans=b-c+1$. #include <stdio.h> using namespace std ; int a , b , c ; void s ...

  5. XVII Open Cup named after E.V. Pankratiev. GP of Siberia, Division 1

    1. Ski race 枚举枚举倍数判断即可.时间复杂度$O(n\log m)$. #include<cstdio> #include<algorithm> using nam ...

  6. XVII Open Cup named after E.V. Pankratiev. Grand Prix of America (NAIPC-2017)

    A. Pieces of Parentheses 将括号串排序,先处理会使左括号数增加的串,这里面先处理减少的值少的串:再处理会使左括号数减少的串,这里面先处理差值较大的串.确定顺序之后就可以DP了. ...

  7. XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem A. Arithmetic Derivative

    题目:Problem A. Arithmetic DerivativeInput file: standard inputOutput file: standard inputTime limit: ...

  8. XVII Open Cup named after E.V. Pankratiev Grand Prix of Moscow Workshops, Sunday, April 23, 2017 Problem D. Great Again

    题目: Problem D. Great AgainInput file: standard inputOutput file: standard outputTime limit: 2 second ...

  9. XVII Open Cup named after E.V. Pankratiev Grand Prix of Moscow Workshops, Sunday, April 23, 2017 Problem K. Piecemaking

    题目:Problem K. PiecemakingInput file: standard inputOutput file: standard outputTime limit: 1 secondM ...

随机推荐

  1. Day047--JS BOM介绍, jQuery介绍和使用

    内容回顾 DOM 文档对象模型(model) 一个模型就是一个对象(属性和方法 面向对象的三大特性:封装 继承 多态) 为了可扩展性 DOM操作 标签属性操作 获取值 getAttribute() 设 ...

  2. HDU2859 Phalanx (动态规划)

    Today is army day, but the servicemen are busy with the phalanx for the celebration of the 60th anni ...

  3. 更换gcc工具链

    title: 更换gcc工具链 date: 2019/1/16 19:27:51 toc: true --- 更换gcc工具链 下载后解压到一个临时目录先看看文件结构 mkdir tmp tar xj ...

  4. busybox(二)编译

    title: busybox(二)编译 tag: arm date: 2018-11-13 23:14:58 --- busybox(二)编译 解压,源码包在busybox-1.7.0.tar.bz2 ...

  5. Numpy 系列(十一)- genfromtxt函数

    定义输入 genfromtxt的唯一强制参数是数据的源.它可以是字符串,字符串列表或生成器.如果提供了单个字符串,则假定它是本地或远程文件或具有read方法的打开的类文件对象的名称,例如文件或Stri ...

  6. 分布式监控系统开发【day37】:监控客户端开发(五)

    一.目录结构 二.模块方法调用关系总图 三.入口文件main 1.解决了说明问题 1.客户端就干了一件事情,干什么事情 收集数据汇报给服务端? 但是我这个客户端是插件形式2.首先必须要传一个参数,st ...

  7. 关于微信登录授权获取unionid的方法

    前言:微信登录授权是目前普遍存在于小程序的,还有一种静默授权方式是微信提供的但是不推荐使用,由于不同设备登录openid是不同的那么我们应该怎样拿到一个唯一的ID呢,下面做分享 wxml代码 < ...

  8. Cygwin添加到鼠标右键

    From:http://www.cnblogs.com/killerlegend/p/3960112.html 在cygwin中安装chere 管理员权限执行mintty,输入下列命令即可. cher ...

  9. 深入学习css伪类和伪元素及其用法

    前言 CSS的伪类和伪元素在平时的代码中经常会出现,可是一旦别人问你,什么是伪类,什么是伪元素,可能还是不能完整的表述出来,下面我们来一探究竟. 伪类和伪元素定义 伪类用于在页面中的元素处于某个状态时 ...

  10. 【Java】Java随手记

    System.out.printf() : System.out.printf("%d",x);               输出整数 System.out.printf(&quo ...