贪吃蛇题解
我们理清一下这道题的思路。
初始每个蛇都是有自己的体力值的。
你考虑一下发现这个蛇它们之间是有实力差异的。
两条蛇 \(x\) 和 \(y\) 比较强弱关系的方法是这样的 ,如果 \(a_x>a_y\) 或者 \(a_x=a_y\) 且 \(x>y\) 那么蛇 \(x\) 就是更强的。
接下来这些蛇将会进行觉狗,每一轮中最强的蛇会有选择的权力。
它可以选择吃还是不吃。
吃的话会吃去最弱的蛇,但是同时他自己的体力值要减去最弱的蛇的体力值。
如果不吃就会结束战斗。
然后我们思考一下,其实只要最大的蛇选择不吃了,那么游戏就会结束了。
那么我们考虑蛇在什么时候就不会吃了。
蛇肯定希望自己不被吃啊。
那么我们就是考虑蛇如果吃了最弱的蛇之后会被吃,那肯定他就不吃了。
这边有一个结论,如果当前的蛇吃了最弱的蛇不是最弱的蛇,这个蛇一定会开心的吃。
然后还有一种就是判断如果都是的时候吃不吃,这个反正是给我看的,因为思路清了所以不讲了,去看别的博客把(雾
#include <bits/stdc++.h>
using namespace std;
namespace IO {
int len = 0;
char ibuf[(1 << 20) + 1], *iS, *iT, out[(1 << 25) + 1];
#define gh() \
(iS == iT ? iT = (iS = ibuf) + fread(ibuf, 1, (1 << 20) + 1, stdin), \
(iS == iT ? EOF : *iS++) : *iS++)
inline int read() {
char ch = gh();
int x = 0;
char t = 0;
while (ch < '0' || ch > '9')
t |= ch == '-', ch = gh();
while (ch >= '0' && ch <= '9')
x = x * 10 + (ch ^ 48), ch = gh();
return t ? -x : x;
}
inline void putc(char ch) { out[len++] = ch; }
template <class T> inline void write(T x) {
if (x < 0)
putc('-'), x = -x;
if (x > 9)
write(x / 10);
out[len++] = x % 10 + 48;
}
string getstr(void) {
string s = "";
char c = gh();
while (c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == EOF)
c = gh();
while (!(c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == EOF))
s.push_back(c), c = gh();
return s;
}
void putstr(string str, int begin = 0, int end = -1) {
if (end == -1)
end = str.size();
for (int i = begin; i < end; i++)
putc(str[i]);
return;
}
inline void flush() {
fwrite(out, 1, len, stdout);
len = 0;
}
} // namespace IO
using IO::flush;
using IO::getstr;
using IO::putc;
using IO::putstr;
using IO::read;
using IO::write;
const int N = 2e6;
int T, n, k, fl, a[N], b[N];
set<pair<int,int> > s;
signed main() {
T = read();
while (T--) {
if (!fl) {
n = read();
for (int i = 1; i <= n; i++)
b[i] = read();
fl = 1;
} else {
k = read();
for (int i = 1; i <= k; i++) {
int x = read(), y = read();
b[x] = y;
}
}
int fl = 0, ans;
s.clear();
for (int i = 1; i <= n; i++)
s.insert(make_pair(b[i], i));
while (1) {
if (s.size() == 2) {
if (fl)
ans = fl + (fl - 1) % 2;
else
ans = 1;
break;
}
pair<int, int> L = *s.begin(), R = *--s.end();
s.erase(s.begin()), s.erase(--s.end());
s.insert(make_pair(R.first - L.first, R.second));
if (s.begin()->second != R.second) {
if (fl) {
ans = fl + (fl - s.size()) % 2;
break;
}
} else if(!fl) fl = s.size();
}
write(ans);
putc('\n');
}
flush();
return 0;
}