2020.1.11考试总结
Posted wljss
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2020.1.11考试总结相关的知识,希望对你有一定的参考价值。
恭贺 treAKer 在毒瘤之神的考验一题中rank1,成为新一届毒瘤之神!
......
结果今天就考了 treAKer 的毒瘤题...
T1
考场上看到1e6就想O(n)的做法,结果失败了...
正解思路很神奇,就是先对物品按照a来排序,询问按照m来排序,用双指针一起扫,同时维护(f_i) 当物品的和为i时 在所有的方案中,最小的b最大 的方案中的 b值。判一下(f_i)和m+s的大小关系即可。
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cstdio>
using namespace std;
int n, Q, now;
const int N = 1005;
int ans[1000010], f[100010];
struct wu
{
int a, b, c;
friend bool operator <(const wu &a, const wu &b){return a.a < b.a;}
} w[1005];
struct Ask
{
int m, k, s, id;
friend bool operator <(const Ask &a, const Ask &b){return a.m < b.m;}
} q[1000010];
inline int read()
{
int res = 0; char ch = getchar(); bool XX = false;
for (; !isdigit(ch); ch = getchar())(ch == '-') && (XX = true);
for (; isdigit(ch); ch = getchar())res = (res << 3) + (res << 1) + (ch ^ 48);
return XX ? -res : res;
}
int main()
{
cin >> n;
for (int i = 1; i <= n; ++i)w[i].c = read(), w[i].a = read(), w[i].b = read();
cin >> Q;
for (int i = 1; i <= Q; ++i)q[i].m = read(), q[i].k = read(), q[i].s = read(), q[i].id = i;
sort(w + 1, w + 1 + n); sort(q + 1, q + 1 + Q);
now = 1; f[0] = 1 << 30;
for (int i = 1; i <= Q; ++i)
{
while (w[now].a <= q[i].m && now <= n)
{
for (int k = 100000; k >= w[now].c; --k)
f[k] = max(f[k], min(f[k - w[now].c], w[now].b));
++now;
}
ans[q[i].id] = (f[q[i].k] > q[i].m + q[i].s);
}
for (int i = 1; i <= Q; ++i)puts(ans[i] ? "TAK" : "NIE");
return 0;
}
T2
(O(n^2))的DP很好推,f[i]=min(f[j],max(h[j+1...i]))((sum[i]-sum[j] le L))
发现最大值在一定的区间内不会变,并且j越小,f值越小,单调栈优化DP一下...
停,怎么只有95分啊?原来这不是正解啊..会被j单调递减的数据卡成(O(n^2))...
其实这时候看情况只要翻转一下序列就行了,又变成(O(n))了...
但会被h一大一小的数据卡掉...,虽然没有这样的数据,但我还是要讲一下正解。
正解就是用线段树优化DP,同时维护f和max就可以了。
事实证明,暴力还是很重要的.
考场95分代码
#include<iostream>
#include<cstring>
#include<cstdio>
#include<ctime>
#define int long long
using namespace std;
int n, L, top, cnt;
const int N = 100010;
int h[N], w[N], s[N], f[N], l[N], zhan[N];
inline int read()
{
int res = 0; char ch = getchar(); bool XX = false;
for (; !isdigit(ch); ch = getchar())(ch == '-') && (XX = true);
for (; isdigit(ch); ch = getchar())res = (res << 3) + (res << 1) + (ch ^ 48);
return XX ? -res : res;
}
void solve1()
{
int tmp, mx;
memset(f, 0x3f, sizeof(f)); f[0] = 0;
for (int i = 1; i <= n; ++i)
{
tmp = 0; mx = -1;
for (int j = i; j >= 1; --j)
{
tmp += w[j]; mx = max(mx, h[j]);
if (tmp > L)break;
f[i] = min(f[i], f[j - 1] + mx);
}
}
cout << f[n];
}
void solve2()
{
for (int i = 1; i <= n; ++i)
{
while (top && h[zhan[top]] <= h[i])--top;
l[i] = zhan[top]; zhan[++top] = i;
}
for (int i = 1; i <= n; ++i)s[i] = s[i - 1] + w[i];
int tmp, mx, pos;
memset(f, 0x3f, sizeof(f)); f[0] = 0;
for (int i = 1; i <= n; ++i)
{
pos = i + 1;
while (1)
{
mx = h[pos - 1]; pos = l[pos - 1] + 1; tmp = s[i] - s[pos - 1];
if (tmp > L)pos = lower_bound(s + 1, s + 1 + n, s[i] - L) - s;
while (s[i] - s[pos - 1] > L)pos++;
f[i] = min(f[i], f[pos - 1] + mx);
if (pos == 1 || tmp > L)break;
}
}
cout << f[n];
}
int TT;
inline void Swap(int &x, int &y) {TT = x; x = y; y = TT;}
signed main()
{
cin >> n >> L;
h[0] = 1e15;
for (int i = 1; i <= n; ++i)h[i] = read(), w[i] = read(), cnt += (h[i] < h[i - 1]);
if (cnt > n / 2)
for (int i = 1, to = n / 2; i <= to; ++i)Swap(h[i], h[n - i + 1]), Swap(w[i], w[n - i + 1]);
if (n <= 5000)solve1();
else solve2();
fclose(stdin); fclose(stdout);
return 0;
}
正解
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#define int long long
#define lson (k<<1)
#define rson ((k<<1)|1)
using namespace std;
int n, L, top;
const int N = 100010, inf = 1e18;
int h[N], w[N], s[N], l[N], dp[N], zhan[N], sum[N];
int tr[N << 2], f[N << 2], tag[N << 2];
inline int read()
{
int res = 0; char ch = getchar(); bool XX = false;
for (; !isdigit(ch); ch = getchar())(ch == '-') && (XX = true);
for (; isdigit(ch); ch = getchar())res = (res << 3) + (res << 1) + (ch ^ 48);
return XX ? -res : res;
}
void upd(int k)
{
f[k] = min(f[lson], f[rson]); tr[k] = min(tr[lson], tr[rson]);
}
void down(int k)
{
if (tag[k] == inf)return;
tr[lson] = f[lson] + tag[k]; tr[rson] = f[rson] + tag[k];
tag[lson] = tag[rson] = tag[k];
tag[k] = inf;
}
void build(int k, int l, int r)
{
tr[k] = f[k] = tag[k] = inf;
if (l == r)return;
int mid = (l + r) >> 1;
build(lson, l, mid); build(rson, mid + 1, r);
}
void Insert(int k, int l, int r, int pos)
{
if (l == r)
{
f[k] = dp[pos - 1]; tr[k] = inf;
return;
}
down(k);
int mid = (l + r) >> 1;
if (pos <= mid)Insert(lson, l, mid, pos);
else Insert(rson, mid + 1, r, pos);
}
void change(int k, int l, int r, int x, int y, int val)
{
if (x <= l && r <= y)
{
tr[k] = f[k] + val; tag[k] = val;
return;
}
down(k);
int mid = (l + r) >> 1;
if (x <= mid)change(lson, l, mid, x, y, val);
if (mid + 1 <= y)change(rson, mid + 1, r, x, y, val);
upd(k);
}
int ask(int k, int l, int r, int x, int y)
{
if (x <= l && r <= y)return tr[k];
down(k);
int mid = (l + r) >> 1, ans = inf;
if (x <= mid)ans = min(ans, ask(lson, l, mid, x, y));
if (mid + 1 <= y)ans = min(ans, ask(rson, mid + 1, r, x, y));
return ans;
}
signed main()
{
cin >> n >> L;
for (int i = 1; i <= n; ++i)h[i] = read(), w[i] = read(), sum[i] = sum[i - 1] + w[i];
for (int i = 1; i <= n; ++i)
{
while (top && h[zhan[top]] <= h[i])--top;
l[i] = zhan[top]; zhan[++top] = i;
}
build(1, 1, n);
for (int i = 1; i <= n; ++i)
{
Insert(1, 1, n, i);
change(1, 1, n, l[i] + 1, i, h[i]);
int pos = lower_bound(sum, sum + i + 1, sum[i] - L) - sum;
if (pos < i)dp[i] = ask(1, 1, n, pos + 1, i);
}
cout << dp[n];
fclose(stdin); fclose(stdout);
return 0;
}
以上是关于2020.1.11考试总结的主要内容,如果未能解决你的问题,请参考以下文章