card card card HDU-6205

题目传送门

题意:给你一个序列,每一个有两个值,一个代表分数,一个代表惩罚值,你每次只能选择最前面的一个,如果你当前的分数小于惩罚值就停止选择,你能在游戏的开始前将最前面的移到最后面任意次,问你的到最大分数是移动的次数。

思路:这个题,emmmm其实一开始就写对了,但是因为数组开小了给我了tle结果我就懵逼了,写的O(n)时间复杂度的代码超时,后来评测机炸了也没再好好检查这个题目(还是太菜了),思路很简单就是一个和尺取差不多的思想。

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define MAXN 2000010
#define MAXE 5
#define INF 1000000000
#define MOD 998244353
#define LL long long
#define pi acos(-1.0)

using namespace std;

int arr[MAXN], brr[MAXN];

int read() {
    int x = 0;
    char ch = getchar();
    while ('0' > ch || ch > '9')
        ch = getchar();
    while ('0' <= ch && ch <= '9') {
        x = (x << 3) + (x << 1) + ch - '0';
        ch = getchar();
    }
    return x;
}

int main() {
    //std::ios::sync_with_stdio(false);
    int n;
    while (~scanf("%d", &n)) {
        int max_sum = 0;
        int sum = 0;
        int x = 0;
        int cnt = 0;
        for (int i = 1; i <= n; ++i) {
            arr[i] = read();
            arr[i + n] = arr[i];
            sum += arr[i];
        }
        for (int i = 1; i <= n; ++i) {
            brr[i] = read();
            brr[i + n] = brr[i];
        }
        int pos = 1;
        while (true) {
            int Num = 0;
            int ans = 0;
            bool flag = false;
            for (int i = pos; i < pos + n; ++i) {
                ans += arr[i];
                Num += arr[i] - brr[i];
                if (Num < 0) {
                    flag = true;
                    if (ans > max_sum || (sum == max_sum && x > pos - 1)) {
                        max_sum = ans;
                        x = pos - 1;
                    }
                    pos = i + 1;
                    break;
                }
            }
            if (!flag) {
                if (ans == max_sum && x > pos - 1) {
                    x = pos - 1;
                } else if (ans > max_sum) {
                    max_sum = ans;
                    x = pos - 1;
                }
                break;
            }
            if (pos > n) {
                break;
            }
            if (max_sum == sum) {
                break;
            }
        }
        printf("%d\n", x);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值