看到300,600,900。。。做完300直接奔900了,结果900赛后半小时才搞过。。。。300还resubmit。。。。
300: (1)从大到小枚举low,求和,当大于等于X的时候,更新答案 i (2)从小到大枚举high,求和,当小于等于C-X的时候更新答案 n-i
结果初始值设成10000,枚举的时候又没枚举好,导致选n个的时候更新不到答案。。然后resubmit一发。。。。T_T。。。
600:
900:
最外层枚举minB到maxB {
然后模拟一遍minA的情况,把字符串中会卡住的操作标记一下,记录一下最后的坐标now
然后枚举minA到maxA,每次先找到第一个左边被卡住的位置,相当于这个位置不会被卡住,把他删掉,那么接下来的路径和上轮的路径其实向左偏移一个格子。
那么再找他后面第一个右边被卡住的位置,这个位置也不会被卡住,删掉,然后接下来路径和上轮路径没有偏移,然后再找。。。。。。
}
大概过程需要维护双向链表,删除的时候很奇怪。。。然后随便扯了几句过的
#include <vector> #include <list> #include <map> #include <set> #include <deque> #include <stack> #include <bitset> #include <algorithm> #include <functional> #include <numeric> #include <utility> #include <sstream> #include <iostream> #include <iomanip> #include <cstdio> #include <cmath> #include <cstdlib> #include <ctime> #include <cstring> using namespace std; class OneDimensionalRobot { public: long long theSum(vector<string> , vector<string> , int, int, int, int); }; string s; bool flag[5005]; int preA[5005], nextA[5005], preB[5005], nextB[5005]; int n; void ez(int t) { flag[t] = false; while (preA[t] != -1 && flag[preA[t]] == false) preA[t] = preA[preA[t]]; while (preB[t] != -1 && flag[preB[t]] == false) preB[t] = preB[preB[t]]; while (nextA[t] != n && flag[nextA[t]] == false) nextA[t] = nextA[nextA[t]]; while (nextB[t] != n && flag[nextB[t]] == false) nextB[t] = nextB[nextB[t]]; if (s[t] == 'L') { if (preA[t] != -1) { nextA[preA[t]] = nextA[t]; if (nextB[preA[t]] > t) nextB[preA[t]] = nextB[t]; preB[preA[t]] = min(preB[preA[t]], preB[t]); } if (preB[t] != -1) { nextA[preB[t]] = nextA[t]; if (nextB[preB[t]] > t) nextB[preB[t]] = nextB[t]; } preA[nextA[t]] = preA[t]; preA[nextB[t]] = preA[t]; if (preB[nextA[t]] < t) preB[nextA[t]] = preB[t]; if (preB[nextB[t]] < t) preB[nextB[t]] = preB[t]; nextB[nextA[t]] = max(nextB[nextA[t]],nextB[t]); } else { if (preA[t] != -1) { nextB[preA[t]] = nextB[t]; if (nextA[preA[t]] > t) nextA[preA[t]] = nextA[t]; } if (preB[t] != -1) { nextB[preB[t]] = nextB[t]; if (nextA[preB[t]] > t) nextA[preB[t]] = nextA[t]; preA[preB[t]] = min(preA[preB[t]], preA[t]); } preB[nextA[t]] = preB[t]; preB[nextB[t]] = preB[t]; if (preA[nextA[t]] < t) preA[nextA[t]] = preA[t]; if (preA[nextB[t]] < t) preA[nextB[t]] = preA[t]; nextA[nextB[t]] = max(nextA[nextB[t]],nextA[t]); } } long long OneDimensionalRobot::theSum(vector<string> commands1, vector<string> commands2, int minA, int maxA, int minB, int maxB) { int i, j; long long ans = 0; s.clear(); for (i = 0; i < commands1.size(); ++i) { s = s + commands1[i]; } for (i = 0; i < commands2.size(); ++i) { s = s + commands2[i]; } n = s.size(); for (i = minB; i <= maxB; ++i) { int now = 0; for (j = 0; j < n; ++j) { flag[j] = false; if (s[j] == 'L') { if (now == -minA) flag[j] = true; else now--; } else { if (now == i) flag[j] = true; else now++; } } int lastA = n; int lastB = n; for (j = n - 1; j >= 0; --j) { nextB[j] = lastB; nextA[j] = lastA; if (flag[j]) { if (s[j] == 'L') lastA = j; else lastB = j; } } int firstA = lastA; lastA = -1; lastB = -1; for (j = 0; j < n; ++j) { preB[j] = lastB; preA[j] = lastA; if (flag[j]) { if (s[j] == 'L') lastA = j; else lastB = j; } } for (j = minA; j <= maxA; ++j) { ans += now; int t = firstA; if (t != n && flag[t]) { while (1) { ez(t); if (s[t] == 'L') { int r = nextB[t]; if (r == n) { now--; break; } t = r; } else { int r = nextA[t]; if (r == n) { break; } t = r; } } while (firstA != n && flag[firstA] == false) firstA = nextA[firstA]; } } } return ans; }