Advertisement
Guest User

Untitled

a guest
Jul 3rd, 2015
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.46 KB | None | 0 0
  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <cstring>
  4. #include <cmath>
  5. #include <climits>
  6. #include <cfloat>
  7. #include <ctime>
  8. #include <cassert>
  9. #include <map>
  10. #include <utility>
  11. #include <set>
  12. #include <iostream>
  13. #include <memory>
  14. #include <string>
  15. #include <vector>
  16. #include <algorithm>
  17. #include <functional>
  18. #include <sstream>
  19. #include <complex>
  20. #include <stack>
  21. #include <queue>
  22. #include <numeric>
  23. #include <list>
  24. #include <iomanip>
  25. #include <fstream>
  26. #include <bitset>
  27.  
  28. #include <tuple>
  29.  
  30. #define mp make_pair
  31. #define mt make_tuple
  32. #define rep(i, n) for (int i = 0; i < (n); i++)
  33.  
  34. using namespace std;
  35.  
  36. typedef long long ll;
  37. typedef pair<int, int> pii;
  38.  
  39. const int INF = 1 << 29;
  40. const double EPS = 1e-9;
  41. const int MOD = 100000007;
  42.  
  43. const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};
  44. class Coversta {
  45. public:
  46. int N, M;
  47. class State {
  48. public:
  49. int y, x;
  50. int sum;
  51. State(int _y, int _x, int _sum) {
  52. y = _y;
  53. x = _x;
  54. sum = _sum;
  55. }
  56. bool operator<(const State &rst) const { return sum < rst.sum; }
  57. };
  58. bool check(int y, int x) {//x,//y
  59. if (y < 0 || y >= N || x < 0 || x >= M) {
  60. return false;
  61. }
  62. return true;
  63. }
  64. int dp[110][110];
  65. int place(vector<string> a, vector<int> x, vector<int> y) {
  66. int result;
  67. swap(x, y);
  68. N = a.size();//y
  69. M = a[0].length();//x
  70. int L = x.size();
  71. memset(dp, 0, sizeof(dp));
  72. priority_queue<State> que;
  73. for (int i = 0; i < N; i++) {//y
  74. for (int j = 0; j < M; j++) {//x
  75. int tmp = 0;
  76. for (int k = 0; k < L; k++) {
  77. int ny, nx;
  78. ny = i + y[k];//x
  79. nx = j + x[k];//y
  80. if (check(ny, nx)) {//x,y
  81. tmp += (a[ny][nx] - '0');
  82. }
  83. }
  84. dp[i][j] = tmp;
  85. que.push(State(i, j, tmp));
  86. }
  87. }
  88. result = 0;
  89. for (int i = 0; i < N; i++) {
  90. for (int j = 0; j < M; j++) {
  91. int cost1 = dp[i][j];
  92. priority_queue<State> que1 = que;
  93. int cnt = 0;
  94. while (!que1.empty()) {
  95. State pos = que1.top();
  96. que1.pop();
  97. if (cost1 + pos.sum <= result) {
  98. break;
  99. }
  100. if (i == pos.y and j == pos.x)
  101. continue;
  102. set<pii> flag;
  103. for (int k = 0; k < L; k++) {
  104. int ny, nx;
  105. ny = i + y[k];
  106. nx = j + x[k];
  107. if (check(ny, nx)) {
  108. flag.insert(mp(ny, nx));
  109. }
  110. }
  111. int cost2 = dp[pos.y][pos.x];
  112. for (int k = 0; k < L; k++) {
  113. int ny, nx;
  114. ny = pos.y + y[k];
  115. nx = pos.x + x[k];
  116. if (check(ny, nx)) {
  117. if (flag.count(mp(ny, nx))) {
  118. cost2 -= (a[ny][nx] - '0');
  119. }
  120. }
  121. }
  122. result = max(result, cost1 + cost2);
  123. cnt++;
  124. // if (cnt >= 1000)break;
  125. }
  126. }
  127. }
  128. return result;
  129. }
  130. };
  131.  
  132. // CUT begin
  133. ifstream data("Coversta.sample");
  134.  
  135. string next_line() {
  136. string s;
  137. getline(data, s);
  138. return s;
  139. }
  140.  
  141. template <typename T> void from_stream(T &t) {
  142. stringstream ss(next_line());
  143. ss >> t;
  144. }
  145.  
  146. void from_stream(string &s) { s = next_line(); }
  147.  
  148. template <typename T> void from_stream(vector<T> &ts) {
  149. int len;
  150. from_stream(len);
  151. ts.clear();
  152. for (int i = 0; i < len; ++i) {
  153. T t;
  154. from_stream(t);
  155. ts.push_back(t);
  156. }
  157. }
  158.  
  159. template <typename T> string to_string(T t) {
  160. stringstream s;
  161. s << t;
  162. return s.str();
  163. }
  164.  
  165. string to_string(string t) { return "\"" + t + "\""; }
  166.  
  167. bool do_test(vector<string> a, vector<int> x, vector<int> y, int __expected) {
  168. time_t startClock = clock();
  169. Coversta *instance = new Coversta();
  170. int __result = instance->place(a, x, y);
  171. double elapsed = (double)(clock() - startClock) / CLOCKS_PER_SEC;
  172. delete instance;
  173.  
  174. if (__result == __expected) {
  175. cout << "PASSED!"
  176. << " (" << elapsed << " seconds)" << endl;
  177. return true;
  178. } else {
  179. cout << "FAILED!"
  180. << " (" << elapsed << " seconds)" << endl;
  181. cout << " Expected: " << to_string(__expected) << endl;
  182. cout << " Received: " << to_string(__result) << endl;
  183. return false;
  184. }
  185. }
  186.  
  187. int run_test(bool mainProcess, const set<int> &case_set, const string command) {
  188. int cases = 0, passed = 0;
  189. while (true) {
  190. if (next_line().find("--") != 0)
  191. break;
  192. vector<string> a;
  193. from_stream(a);
  194. vector<int> x;
  195. from_stream(x);
  196. vector<int> y;
  197. from_stream(y);
  198. next_line();
  199. int __answer;
  200. from_stream(__answer);
  201.  
  202. cases++;
  203. if (case_set.size() > 0 && case_set.find(cases - 1) == case_set.end())
  204. continue;
  205.  
  206. cout << " Testcase #" << cases - 1 << " ... ";
  207. if (do_test(a, x, y, __answer)) {
  208. passed++;
  209. }
  210. }
  211. if (mainProcess) {
  212. cout << endl << "Passed : " << passed << "/" << cases << " cases" << endl;
  213. int T = time(NULL) - 1433415613;
  214. double PT = T / 60.0, TT = 75.0;
  215. cout << "Time : " << T / 60 << " minutes " << T % 60 << " secs" << endl;
  216. cout << "Score : "
  217. << 250 * (0.3 + (0.7 * TT * TT) / (10.0 * PT * PT + TT * TT))
  218. << " points" << endl;
  219. }
  220. return 0;
  221. }
  222.  
  223. int main(int argc, char *argv[]) {
  224. cout.setf(ios::fixed, ios::floatfield);
  225. cout.precision(2);
  226. set<int> cases;
  227. bool mainProcess = true;
  228. for (int i = 1; i < argc; ++i) {
  229. if (string(argv[i]) == "-") {
  230. mainProcess = false;
  231. } else {
  232. cases.insert(atoi(argv[i]));
  233. }
  234. }
  235. if (mainProcess) {
  236. cout << "Coversta (250 Points)" << endl << endl;
  237. }
  238. return run_test(mainProcess, cases, argv[0]);
  239. }
  240. // CUT end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement