您的位置:首页 > 其它

NOIP模拟(10.20)T1 刮刮卡

2017-10-20 18:10 211 查看
 刮刮卡

题目背景:

10.20 NOIP模拟T1

分析:前缀和 or 单调队列

我们来看这道有意思的题目,定义数组c,c[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数组之和的,那么其实我们倍长之后的数组求c和sum,也相当于和前面一模一样的复制了一遍,那么只要找到所有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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: