题意:有n堆牌,ai表示每堆牌的牌数,bi表示每堆牌的penaltyvalue,操作开始前,可以重复进行将第一堆牌挪到最后一堆这一操作。然后,对于挪完后的牌,从第一堆开始,依次取。对于每一堆牌,首先将这堆牌全拿在手中,然后将其全部面朝上翻开,如果手中当前面朝上的牌<该堆牌的penaltyvalue,则游戏停止,该堆牌是可以全部拿走的;否则,将手中面朝上的penaltyvalue数量的牌翻过来使其面朝下,然后继续操作下一堆牌。问操作开始前,最少重复进行多少次挪的操作可以使手中最终拿的牌的数量最多。
分析:从第一堆开始,边拿边统计,直到游戏停止,然后从停止的下一张牌开始重新边拿边统计,尺取法思想。
#include<cstdio> #include<cstring> #include<cstdlib> #include<cctype> #include<cmath> #include<iostream> #include<sstream> #include<iterator> #include<algorithm> #include<string> #include<vector> #include<set> #include<map> #include<stack> #include<deque> #include<queue> #include<list> #define lowbit(x) (x & (-x)) const double eps = 1e-8; inline int dcmp(double a, double b){ if(fabs(a - b) < eps) return 0; return a > b ? 1 : -1; } typedef long long LL; typedef unsigned long long ULL; const int INT_INF = 0x3f3f3f3f; const int INT_M_INF = 0x7f7f7f7f; const LL LL_INF = 0x3f3f3f3f3f3f3f3f; const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f; const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1}; const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1}; const LL MOD = 998244353; const double pi = acos(-1.0); const int MAXN = 1000000 + 10; const int MAXT = 10000 + 10; using namespace std; int a[MAXN], b[MAXN]; bool vis[MAXN]; int main(){ int n; while(scanf("%d", &n) == 1){ memset(vis, false, sizeof vis); for(int i = 1; i <= n; ++i){ scanf("%d", &a[i]); } for(int i = 1; i <= n; ++i){ scanf("%d", &b[i]); } int st = 1, ma = 0, ans = 0; while(!vis[n]){ int sum = 0; int cnt = 0; int tmp; for(int i = 0; i < n; ++i){ tmp = st + i; if(tmp > n) tmp -= n; vis[tmp] = true; cnt += a[tmp]; sum += a[tmp]; if(sum >= b[tmp]){ sum -= b[tmp]; } else break; } if(cnt > ma){ ma = cnt; ans = st - 1; } st = tmp + 1; } printf("%d ", ans); } return 0; }