apio2016
28. APIO2016 - Boat
考虑一个
发现
考虑设
于是就可以利用这个来 dp。设
本质不同的
<details> <summary>点击查看代码</summary>
//qoj2
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll P = 1e9 + 7;
const int N = 510, M = 1010;
int n, a[N], b[N], pl[M];
ll f[N][M], g[N][M], h[N][M];
ll fac[N], inv[N];
ll qp(ll x, ll y){
ll ans = 1;
while(y){
if(y & 1){
ans = ans * x % P;
}
x = x * x % P;
y >>= 1;
}
return ans;
}
int main(){
scanf("%d", &n);
fac[0] = 1;
for(int i = 1; i <= n; ++ i){
fac[i] = fac[i-1] * i % P;
scanf("%d%d", &a[i], &b[i]);
pl[i] = a[i];
pl[i+n] = b[i] + 1;
}
inv[n] = qp(fac[n], P-2);
for(int i = n-1; i >= 0; -- i){
inv[i] = inv[i+1] * (i+1) % P;
}
sort(pl + 1, pl + n + n + 1);
int m = unique(pl + 1, pl + n + n + 1) - pl - 1;
for(int i = 1; i <= n; ++ i){
a[i] = lower_bound(pl + 1, pl + m + 1, a[i]) - pl;
b[i] = lower_bound(pl + 1, pl + m + 1, b[i] + 1) - pl - 1;
}
f[0][0] = 1;
for(int j = 0; j < m; ++ j){
g[0][j] = 1;
}
for(int j = 1; j < m; ++ j){
h[0][j] = 1;
ll val = 1;
for(int i = 1; i <= n; ++ i){
val = val * (pl[j+1] - pl[j] + i - 1) % P;
h[i][j] = val * inv[i] % P;
}
}
for(int i = 1; i <= n; ++ i){
for(int j = 1; j < m; ++ j){
if(j < a[i] || j > b[i]){
continue;
}
int cnt = 0;
for(int k = i; k >= 1; -- k){
if(a[k] <= j && j <= b[k]){
++ cnt;
}
f[i][j] = (f[i][j] + g[k-1][j-1] * h[cnt][j]) % P;
}
}
for(int j = 1; j <= m; ++ j){
g[i][j] = (g[i][j-1] + f[i][j]) % P;
}
}
ll ans = 0;
for(int i = 1; i <= n; ++ i){
for(int j = 1; j < m; ++ j){
ans = (ans + f[i][j]) % P;
}
}
printf("%lld\n", ans);
return 0;
}
</details>
29. APIO2016 - Fireworks
设
原因是
那么转移在图像上形如:
- 将
>R 的地方全部换为斜率为=1 的直线; - 将
[L,R] 斜率为0 的直线向右平移l ; - 在
[L,L+l] 之间插入斜率为-1 的直线。
考虑维护每个转折点的横坐标;若相邻斜率差
- 弹出最大的
ind_i-1 个横坐标(其中ind_i 表示i 的儿子个数); - 弹出最大的
2 个横坐标,+l 后插入回去。
然后两个函数合并则直接将维护横坐标的堆合并即可。使用左偏树维护。
最后求答案时,由于
<details> <summary>点击查看代码</summary>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 6e5 + 10;
int n, m, f[N], v[N], ind[N];
ll sum;
int rt[N], top;
struct node{
int ls, rs, ds;
ll x;
} t[N];
int mg(int x, int y){
if(!x || !y){
return x + y;
}
if(t[x].x < t[y].x || (t[x].x == t[y].x && x < y)){
swap(x, y);
}
t[x].rs = mg(t[x].rs, y);
if(t[t[x].ls].ds < t[t[x].rs].ds){
swap(t[x].ls, t[x].rs);
}
t[x].ds = t[t[x].rs].ds + 1;
return x;
}
int pop(int x){
return mg(t[x].ls, t[x].rs);
}
int main(){
scanf("%d%d", &n, &m);
for(int i = 2; i <= n + m; ++ i){
scanf("%d%d", &f[i], &v[i]);
++ ind[f[i]];
sum += v[i];
}
for(int i = n + m; i > 1; -- i){
ll x = 0, y = 0;
if(i <= n){
for(int j = 1; j < ind[i]; ++ j){
rt[i] = pop(rt[i]);
}
y = t[rt[i]].x;
rt[i] = pop(rt[i]);
x = t[rt[i]].x;
rt[i] = pop(rt[i]);
}
t[++top].x = x + v[i];
t[++top].x = y + v[i];
rt[i] = mg(rt[i], mg(top-1, top));
rt[f[i]] = mg(rt[f[i]], rt[i]);
}
for(int j = 1; j < ind[1]; ++ j){
rt[1] = pop(rt[1]);
}
rt[1] = pop(rt[1]);
while(rt[1]){
sum -= t[rt[1]].x;
rt[1] = pop(rt[1]);
}
printf("%lld\n", sum);
return 0;
}
</details>
30. APIO2016 - Gap
Sub1:
询问
Sub2:
首先询问
<details> <summary>点击查看代码</summary>
//qoj4
#include <bits/stdc++.h>
using namespace std;
#include "gap.h"
long long findGap(int T, int n){
static long long a[200010];
memset(a, 0, sizeof(a));
if(T == 1){
a[0] = -3, a[n+1] = 2e18;
for(int i = 1, j = n; i <= j; ++ i, -- j){
MinMax(a[i-1] + 1, a[j+1] - 1, &a[i], &a[j]);
}
long long mx = 0;
for(int i = 1; i < n; ++ i){
mx = max(mx, a[i+1] - a[i]);
}
return mx;
} else {
long long mn, mx;
MinMax(-3, 2e18, &mn, &mx);
long long k = (mx - mn - 1) / (n - 1) + 1;
int cnt = 0;
if(mx - mn <= 1){
return mx - mn;
}
a[++cnt] = mn;
a[++cnt] = mx;
++ mn;
-- mx;
for(long long i = mn; i <= mx; i += k){
long long tx, ty;
MinMax(i, i+k-1, &tx, &ty);
a[++cnt] = tx;
a[++cnt] = ty;
}
sort(a + 1, a + cnt + 1);
mx = 0;
for(int i = 1; i < cnt; ++ i){
mx = max(mx, a[i+1] - a[i]);
}
return mx;
}
}
</details>