NOIP模拟(10.20)T1 刮刮卡

  刮刮卡

题目背景:

10.20 NOIP模拟T1

分析:前缀和 or 单调队列

我们来看这道有意思的题目,定义数组cc[i] = b[i] - a[i],那么对于以i位置开始的决策,也就是对于一段连续的c[i] + c[i + 1] + c[i + 2] +  + c[j]小于0时,交易停止,那么我们对于c求一个前缀合sum数组,sum[i] = sum[1]+ sum[2] +  + sum[i],那么现在对于i来说,就是找到第一个小于sum[i - 1]sum,这个可以选择将数组倍长一发然后直接单调队列······但是,我们注意到题目还有一个性质,a数组之和是等于b数组之和的,那么其实我们倍长之后的数组求csum,也相当于和前面一模一样的复制了一遍,那么只要找到所有sum里面最小的一个,从他后面开始取,就一定可以取完了······

 

单调队列:

Source

 

/*
	created by scarlyw
*/
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <cctype>
#include <set>
#include <map>
#include <vector>
#include <queue>
#include <ctime>

inline char read() {
	static const int IN_LEN = 1024 * 1024;
	static char buf[IN_LEN], *s, *t;
	if (s == t) {
		t = (s = buf) + fread(buf, 1, IN_LEN, stdin);
		if (s == t) return -1;
	}
	return *s++;
}

///*
template<class T>
inline void R(T &x) {
	static bool iosig;
	static char c;
	for (iosig = false, c = read(); !isdigit(c); c = read()) {
		if (c == -1) return ;
		if (c == '-') iosig = true;
	}
	for (x = 0; isdigit(c); c = read()) x = ((x << 2) + x << 1) + (c ^ '0');
	if (iosig) x = -x;
}
//*/

const int OUT_LEN = 1024 * 1024;
char obuf[OUT_LEN], *oh = obuf;

inline void write_char(char c) {
	if (oh == obuf + OUT_LEN) fwrite(obuf, 1, OUT_LEN, stdout), oh = obuf;
	*oh++ = c;
}

template<class T>
inline void W(T x) {
	static int buf[30], cnt;
	if (x == 0) write_char('0');
	else {
		if (x < 0) write_char('-'), x = -x;
		for (cnt = 0; x; x /= 10) buf[++cnt] = x % 10 + 48;
		while (cnt) write_char(buf[cnt--]);
	}
}

inline void flush() {
	fwrite(obuf, 1, oh - obuf, stdout);
}

/*
template<class T>
inline void R(T &x) {
	static bool iosig;
	static char c;
	for (iosig = false, c = getchar(); !isdigit(c); c = getchar()) {
		if (c == -1) return ;
		if (c == '-') iosig = true;
	}
	for (x = 0; isdigit(c); c = getchar()) x = ((x << 2) + x << 1) + (c ^ '0');
	if (iosig) x = -x;
}
//*/

const int MAXN = 2000000 + 10;
const int INF = ~0u >> 1;

int n;
int a[MAXN], b[MAXN], sum[MAXN], pos[MAXN];

inline void read_in() {
	R(n);
	for (int i = 1; i <= n; ++i) R(b[i]), b[i + n] = b[i];
	for (int i = 1; i <= n; ++i) R(a[i]), a[i + n] = a[i];
	for (int i = 1; i <= 2 * n; ++i) sum[i] = sum[i - 1] + b[i] - a[i];
}

inline void solve() {
	static int q[MAXN], head, tail;
	head = 1, q[tail = 1] = 0, sum[2 * n + 1] = -INF;
	for (int i = 1; i <= 2 * n + 1; ++i) {
		while (head <= tail && q[head] < i - n) 
			pos[q[head]] = q[head] + n, head++;
		while (head <= tail && sum[q[tail]] > sum[i]) 
			pos[q[tail]] = i, tail--;
		q[++tail] = i;
	}
	for (int i = 1; i <= 2 * n; ++i) sum[i] = sum[i - 1] + b[i];
	int ans = 0;
	for (int i = 0; i < n; ++i) 
		if (sum[pos[i]] - sum[i] > sum[pos[ans]] - sum[ans]) ans = i;
	std::cout << ans;
}

int main() {
//	freopen("rock.in", "r", stdin);
//	freopen("rock.out", "w", stdout);
	read_in();
	solve();
	return 0;
}


sum最小(膜拜xehoth dalao):

Source

#include <bits/stdc++.h>

namespace IO {

inline char read() {
    static const int IN_LEN = 1000000;
    static char buf[IN_LEN], *s, *t;
    s == t ? t = (s = buf) + fread(buf, 1, IN_LEN, stdin) : 0;
    return s == t ? -1 : *s++;
}

template <typename T>
inline bool read(T &x) {
    static char c;
    static bool iosig;
    for (c = read(), iosig = false; !isdigit(c); c = read()) {
        if (c == -1) return false;
        c == '-' ? iosig = true : 0;
    }
    for (x = 0; isdigit(c); c = read()) x = x * 10 + (c ^ '0');
    iosig ? x = -x : 0;
    return true;
}

inline void read(char &c) {
    while (c = read(), isspace(c) && c != -1)
        ;
}

inline int read(char *buf) {
    register int s = 0;
    register char c;
    while (c = read(), isspace(c) && c != -1)
        ;
    if (c == -1) {
        *buf = 0;
        return -1;
    }
    do
        buf[s++] = c;
    while (c = read(), !isspace(c) && c != -1);
    buf[s] = 0;
    return s;
}

const int OUT_LEN = 1000000;

char obuf[OUT_LEN], *oh = obuf;

inline void print(char c) {
    oh == obuf + OUT_LEN ? (fwrite(obuf, 1, OUT_LEN, stdout), oh = obuf) : 0;
    *oh++ = c;
}

template <typename T>
inline void print(T x) {
    static int buf[30], cnt;
    if (x == 0) {
        print('0');
    } else {
        x < 0 ? (print('-'), x = -x) : 0;
        for (cnt = 0; x; x /= 10) buf[++cnt] = x % 10 | 48;
        while (cnt) print((char)buf[cnt--]);
    }
}

inline void print(const char *s) {
    for (; *s; s++) print(*s);
}

inline void flush() { fwrite(obuf, 1, oh - obuf, stdout); }

struct InputOutputStream {
    template <typename T>
    inline InputOutputStream &operator>>(T &x) {
        read(x);
        return *this;
    }

    template <typename T>
    inline InputOutputStream &operator<<(const T &x) {
        print(x);
        return *this;
    }

    ~InputOutputStream() { flush(); }
} io;
}

namespace {

using IO::io;

const int MAXN = 1000000;

#define long long long

int a[MAXN * 2 + 2], b[MAXN * 2 + 2];
int r[MAXN * 2 + 2];
long sum[MAXN * 2 + 1];

inline void solve() {
    register int n;
    io >> n;
    for (register int i = 1; i <= n; i++) io >> b[i];
    for (register int i = 1, x; i <= n; i++) io >> x, sum[i] = b[i] - x;
    for (register int i = 1; i <= n; i++) sum[i] += sum[i - 1];
    register int min = INT_MAX, pos = -1;
    for (register int i = 0; i < n; i++) {
        if (sum[i] < min) {
            min = sum[i], pos = i;
        }
    }
    io << pos;
}
}

int main() {
    // freopen("rock.in", "r", stdin);
    // freopen("rock.out", "w", stdout);
    solve();
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值