HDU 6205 card card card(尺取)

解法:不断找起点,由于如果1 2 3这些点是一次遍历的,然后到了第4个点他们的和小于0而退出,那么从2开始再次走肯定没有第一次优,所以可以采用尺取的办法,建立一个环,走一遍即可。

代码如下:

#include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#include<utility>
#include<stack>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#include<set>
#include<map>
using namespace std;
const int maxn = 1e6 + 5;
int n;
int a[maxn<<1], b[maxn<<1], c[maxn<<1];

#define FI(n) FastIO::read(n)
namespace FastIO {
    const int SIZE = 1 << 16;
    char buf[SIZE], obuf[SIZE], str[60];
    int bi = SIZE, bn = SIZE, opt;
    int read(char *s) {
        while (bn) {
            for (; bi < bn && buf[bi] <= ' '; bi++);
            if (bi < bn) break;
            bn = fread(buf, 1, SIZE, stdin);
            bi = 0;
        }
        int sn = 0;
        while (bn) {
            for (; bi < bn && buf[bi] > ' '; bi++) s[sn++] = buf[bi];
            if (bi < bn) break;
            bn = fread(buf, 1, SIZE, stdin);
            bi = 0;
        }
        s[sn] = 0;
        return sn;
    }
    bool read(int& x) {
        int n = read(str), bf;

        if (!n) return 0;
        int i = 0; if (str[i] == '-') bf = -1, i++; else bf = 1;
        for (x = 0; i < n; i++) x = x * 10 + str[i] - '0';
        if (bf < 0) x = -x;
        return 1;
    }
};

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
#endif
	int n;
	while(FI(n)) {
		int len = n + n;
		for(int i = 1; i <= n; i++) 
			FI(a[i]);
		for(int i = 1; i <= n; i++) {
			FI(b[i]);
			c[i] = a[i] - b[i];
		}
		for(int i = n + 1; i < len; i++) {
			a[i] = a[i - n];
			c[i] = c[i - n];
		}
		int Max = 0, ans = 0;
		for(int i = 1, j; i <= n; i++) {
			if(c[i] < 0)
				continue;
			int tmp = c[i], cnt = a[i];
			len = i + n;
			for(j = i + 1; j < len; j++) {
				if(tmp + c[j] < 0)
					break;
				tmp += c[j];
				cnt += a[j];
			}
			if(cnt > Max) {
				Max = cnt;
				ans = i - 1;
			}
			i = j - 1;
		}
		printf("%d\n", ans);
	}
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值