Guest User

Untitled

a guest
Jan 22nd, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.52 KB | None | 0 0
  1. #include <cstdio>
  2. #include <iostream>
  3. #include <cstdlib>
  4. #include <cmath>
  5. #include <ctime>
  6. #include <ctype.h>
  7. #include <algorithm>
  8. #include <string>
  9. #include <string.h>
  10. #include <cstring>
  11. #include <vector>
  12. #include <stack>
  13. #include <queue>
  14. #include <map>
  15. #include <set>
  16. using namespace std;
  17. #define FOR(i,a,b) for(int i = a; i <= b; ++i)
  18. #define FORD(i,a,b) for(int i = a; i >= b; --i)
  19. #define REP(x, n) for(int x=0; x < (n); ++x)
  20. #define VAR(v,i) __typeof(i) v=(i)
  21. #define FOREACH(i,c) for(VAR(i,(c).begin());i!=(c).end();++i)
  22. #define SIZE(n) (int)n.size()
  23. #define ALL(c) c.begin(),c.end()
  24. #define PB(n) push_back(n)
  25. typedef long long LL;
  26. typedef pair < int , int > PII;
  27. typedef vector < int > VI;
  28. typedef vector < vector < int > > VVI;
  29. typedef vector < bool > VB;
  30. #define MP make_pair
  31. #define ST first
  32. #define ND second
  33. const int INF = 1000000001;
  34. //~~~~~~~~~~~~~~BigNum~~~~~~~~~~~~~~~~~~~~~//
  35. struct BigNum
  36. {
  37. #define REDUCE() while(len>1 && !cyf[len-1]) len--;
  38. static const int BASE = 1000000000, BD = 9;
  39. int len, al;
  40. LL *cyf;
  41. BigNum ( int v = 0, int l = 2 ) :
  42. len ( 1 ), al ( l ), cyf ( new LL[l] )
  43. {
  44. REP(x, al)
  45. cyf[x] = 0;
  46. if ( (cyf[0] = v) >= BASE ) przen ( 1 );
  47. }
  48. BigNum ( const BigNum & a ) :
  49. len ( a.len ), al ( len ), cyf ( new LL[al] )
  50. {
  51. REP(x, al)
  52. cyf[x] = a.cyf[x];
  53. }
  54. ~BigNum ()
  55. {
  56. delete cyf;
  57. }
  58. void
  59. Res ( int l )
  60. {
  61. if ( l > al )
  62. {
  63. LL *n = new LL[l = max ( l, 2 * al )];
  64. REP(x, l)
  65. n[x] = x >= al ? 0 : cyf[x];
  66. delete cyf;
  67. cyf = n;
  68. al = l;
  69. }
  70. }
  71.  
  72. void
  73. przen ( int p )
  74. {
  75. int x = 0;
  76. for ( ; x < p || cyf[x] < 0 || cyf[x] >= BASE; x++ )
  77. {
  78. Res ( x + 2 );
  79. if ( cyf[x] < 0 )
  80. {
  81. LL i = (- cyf[x] - 1) / BASE + 1;
  82. cyf[x] += i * BASE;
  83. cyf[x + 1] -= i;
  84. }
  85. else
  86.  
  87. if ( cyf[x] >= BASE )
  88. {
  89. LL i = cyf[x] / BASE;
  90. cyf[x] -= i * BASE;
  91. cyf[x + 1] += i;
  92. }
  93. }
  94. len = max ( len, x + 1 );
  95. while ( len > 1 && ! cyf[len - 1] )
  96. len--;
  97. }
  98. #define OPER1(op) bool operator op (const BigNum &a) const
  99. OPER1(<)
  100. {
  101. if ( len != a.len ) return len < a.len;
  102. int x = len - 1;
  103. while ( x && a.cyf[x] == cyf[x] )
  104. x--;
  105. return cyf[x] < a.cyf[x];
  106. }
  107. OPER1(<=)
  108. {
  109. return ! (a < * this);
  110. }
  111. BigNum &
  112. operator= ( int a )
  113. {
  114. REP(x, len)
  115. cyf[x] = 0;
  116. len = 1;
  117. if ( cyf[0] = a >= BASE ) przen ( 1 );
  118. return * this;
  119. }
  120. #define OPER2(op) BigNum& operator op (const BigNum &a)
  121. OPER2(=)
  122. {
  123. Res ( a.len );
  124. FORD(x, len - 1, a.len)
  125. cyf[x] = 0;
  126. REP(x, a.len)
  127. cyf[x] = a.cyf[x];
  128. len = a.len;
  129. return * this;
  130. }
  131. void
  132. write () const
  133. {
  134. printf ( "%d", int(cyf[len - 1]) );
  135. FORD(x, len - 2, 0)
  136. printf ( "%0*d", BD, int(cyf[x]) );
  137. }
  138. BigNum &
  139. operator= ( string a )
  140. {
  141. int s = a.length ();
  142. * this = 0;
  143. Res ( len = s / BD + 1 );
  144. REP(x, s)
  145. cyf[(s - x - 1) / BD] = 10 * cyf[(s - x - 1) / BD] + a[x]
  146. - '0';
  147. REDUCE();
  148. return * this;
  149. }
  150. void
  151. operator+= ( int a )
  152. {
  153. cyf[0] += a;
  154. przen ( 1 );
  155. }
  156. OPER2(+=)
  157. {
  158. Res ( a.len );
  159. REP(x, a.len)
  160. cyf[x] += a.cyf[x];
  161. przen ( a.len );
  162. return * this;
  163. }
  164. OPER1(==)
  165. {
  166. if ( a.len != len ) return 0;
  167. REP(x, len)
  168. if ( cyf[x] != a.cyf[x] ) return 0;
  169. return 1;
  170. }
  171. };
  172. //~~~~~~~~~~~~~~Ford-Bellman~~~~~~~~~~~~~~~~~~~~~//
  173. int V, E, s, t;
  174. struct str
  175. {
  176. int trg, w;
  177. };
  178. vector < str > G[501];
  179. int d[501];
  180. bool
  181. ford_bellman ( int start )
  182. {
  183. FOR(i,1,V)
  184. d[i] = INF;
  185. d[start] = 0;
  186. bool change;
  187. int x;
  188. for ( x = 1; x <= V; ++x )
  189. {
  190. change = true;
  191. FOR(u,1,V)
  192. FOREACH(it,G[u])
  193. {
  194. int v = it->trg, cost = it->w;
  195. if ( d[v] > d[u] + cost )
  196. {
  197. d[v] = d[u] + cost;
  198. change = false;
  199. }
  200. }
  201. if ( change ) return true;
  202. }
  203. if ( x >= V ) return false;
  204. return true;
  205. }
  206. //~~~~~~~~~~~~~~Dijkstra~~~~~~~~~~~~~~~~~~~~~//
  207. int V, E, d[50002];
  208. bool ok[50002];
  209. struct str
  210. {
  211. int trg, w;
  212. str ( int a, int b )
  213. {
  214. trg = a;
  215. w = b;
  216. }
  217. };
  218. vector < str > G[50002];
  219. bool
  220. operator < ( str a, str b )
  221. {
  222. if ( a.w > b.w )
  223. return true;
  224. else
  225. return false;
  226. }
  227. priority_queue < str > Q;
  228.  
  229. void
  230. dijkstra ( int s )
  231. {
  232. str tmp ( s, 0 );
  233. FOR(i,1,V)
  234. {
  235. d[i] = INF;
  236. ok[i] = false;
  237. }
  238. d[s] = 0;
  239. Q.push ( tmp );
  240. while ( ! Q.empty () )
  241. {
  242. str top = Q.top ();
  243. Q.pop ();
  244. if ( ! ok[top.trg] )
  245. {
  246. ok[top.trg] = true;
  247. for ( unsigned int i = 0; i < G[top.trg].size (); ++i )
  248. {
  249. str akt = G[top.trg][i];
  250. if ( d[akt.trg] > d[top.trg] + akt.w )
  251. {
  252. d[akt.trg] = d[top.trg] + akt.w;
  253. str tmp ( akt.trg, d[akt.trg] );
  254. Q.push ( tmp );
  255. }
  256. }
  257. }
  258. }
  259. }
  260. //~~~~~~~~~~~~~~Floyd-Warchall~~~~~~~~~~~~~~~~~~~~~//
  261. void
  262. floyd_warschall ()
  263. {
  264. FOR(k,1,V)
  265. FOR(i,1,V)
  266. FOR(j,1,V)
  267. d[i][j] = min ( d[i][j], d[i][k] + d[k][j] );
  268. }
  269. //~~~~~~~~~~~~~~Find-Union - Kruskal~~~~~~~~~~~~~~~~~~~~~//
  270. int V, E, parent[10010], rank[10010];
  271. struct vertex
  272. {
  273. int from, to, w;
  274. };
  275. bool
  276. operator< ( vertex a, vertex b )
  277. {
  278. if ( a.w < b.w )
  279. return true;
  280. else
  281. return false;
  282. }
  283. vertex Edges[1000010];
  284. int
  285. Find ( int x )
  286. {
  287. if ( parent[x] == x ) return x;
  288. parent[x] = Find ( parent[x] );
  289. return parent[x];
  290. }
  291. void
  292. Union ( int x, int y )
  293. {
  294. int u = Find ( x );
  295. int v = Find ( y );
  296. if ( rank[u] == rank[v] ) rank[u]++;
  297. if ( rank[u] > rank[v] )
  298. parent[v] = u;
  299. else
  300. parent[u] = v;
  301. }
  302. long long
  303. kruskal ()
  304. {
  305. long long cost = 0;
  306. for ( int i = 1; i <= V; ++i )
  307. {
  308. parent[i] = i;
  309. rank[i] = 0;
  310. }
  311. sort ( Edges, Edges + E );
  312. for ( int i = 0; i < E; ++i )
  313. {
  314. int u = Edges[i].from, v = Edges[i].to;
  315. if ( Find ( u ) != Find ( v ) )
  316. {
  317. Union ( u, v );
  318. cost += Edges[i].w;
  319. }
  320. }
  321. return cost;
  322. }
  323. //~~~~~~~~~~~~~~BFS~~~~~~~~~~~~~~~~~~~~~//
  324. int V, E, n;
  325. vector < int > G[500001];
  326. int d[500001];
  327. void
  328. BFS ( int v )
  329. {
  330. d[v] = 0;
  331. queue < int > q;
  332. q.push ( v );
  333. while ( ! q.empty () )
  334. {
  335. int u = q.front ();
  336. q.pop ();
  337. FOREACH(it,G[u])
  338. {
  339. if ( d[* it] == - 1 )
  340. {
  341. d[* it] = d[u] + 1;
  342. q.push ( * it );
  343. }
  344. }
  345. }
  346. }
  347.  
  348. //~~~~~~~~~~~~~~DFS~~~~~~~~~~~~~~~~~~~~~//
  349. #define bialy 0
  350. #define szary 1
  351. #define czarny 2
  352. int V, E, n;
  353. vector < int > G[500001];
  354. stack < int > a;
  355. bool cykl;
  356. char odw[500001] =
  357. { bialy };
  358. void
  359. DFS ( int v )
  360. {
  361. odw[v] = szary;
  362. FOREACH(it,G[v])
  363. {
  364. if ( odw[* it] == bialy ) DFS ( G[v][i] );
  365. if ( odw[* it] == szary ) cykl = true;
  366. }
  367. odw[v] = czarny;
  368. a.push ( v );
  369. }
  370. //~~~~~~~~~~~~~~Longest Common Subsequence~~~~~~~~~~~~~~~~~~~~~//
  371. int
  372. LCS ()
  373. {
  374. short int m[3001][3001];
  375. char s[3001][3001], x[3001];
  376. string a, b;
  377. cin >> a >> b;
  378. int dl_a = a.size (), dl_b = b.size ();
  379. for ( int i = 0; i <= max ( dl_a, dl_b ); ++i )
  380. {
  381. m[0][i] = 0;
  382. m[i][0] = 0;
  383. }
  384. for ( int i = 1; i <= dl_a; ++i )
  385. {
  386. for ( int j = 1; j <= dl_b; ++j )
  387. {
  388. if ( a[i - 1] == b[j - 1] )
  389. {
  390. m[i][j] = m[i - 1][j - 1] + 1;
  391. s[i][j] = '0';
  392. }
  393. else
  394. {
  395. if ( m[i - 1][j] > m[i][j - 1] )
  396. {
  397. m[i][j] = m[i - 1][j];
  398. s[i][j] = '1';
  399. }
  400. else
  401. {
  402. m[i][j] = m[i][j - 1];
  403. s[i][j] = '2';
  404. }
  405. }
  406. }
  407. }
  408. int c = 0, i = dl_a, j = dl_b;
  409. while ( i > 0 && j > 0 )
  410. {
  411. if ( s[i][j] == '0' )
  412. {
  413. x[c++] = a[i - 1];
  414. i--;
  415. j--;
  416. }
  417. else if ( s[i][j] == '2' )
  418. j--;
  419. else
  420. i--;
  421. }
  422. return m[dl_a][dl_b];
  423. }
  424. //~~~~~~~~~~~~~~problem plecakowy~~~~~~~~~~~~~~~~~~~~~//
  425. int
  426. backpack ()
  427. {
  428. int v[1001] =
  429. { 0 }, s[1001] =
  430. { 0 }, p[1001][10001] =
  431. {
  432. { 0 } };
  433. int n, b;
  434. scanf ( "%d %d", & n, & b );
  435. REP(i,n)
  436. scanf ( "%d %d", & s[i], & v[i] );
  437. FOR(i,1,n)
  438. {
  439. FOR(c,1,b)
  440. {
  441. if ( c - s[k] < 0 )
  442. {
  443. p[k][c] = p[k - 1][c];
  444. continue;
  445. }
  446. p[k][c] = max ( p[k - 1][c], v[k] + p[k - 1][c - s[k]] );
  447. }
  448. }
  449. return p[n][b];
  450. }
  451.  
  452. int
  453. main ( int argc, char **argv )
  454. {
  455. return 0;
  456. }
Add Comment
Please, Sign In to add comment