# UOJ#386. 【UNR #3】鸽子固定器(链表)

## Sol

```#include<cstdio>
#include<algorithm>
#include<queue>
#define Pair pair<int, int>
#define MP(x, y) make_pair(x, y)
#define fi first
#define se second
#define LL long long
#define int long long
using namespace std;
const int MAXN = 2 * 1e5 + 10, B = 25000;
char c = getchar(); int x = 0, f = 1;
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
int N, M, ds, dv;
int l[MAXN], r[MAXN];
LL Get(LL x, int opt) {
if(opt == 1) return x;
else return x * x;
}
struct Node {
int s, v;
bool operator < (const Node &rhs) const {
return s == rhs.s ? v < rhs.v : s < rhs.s;
}
}a[MAXN];
main() {
priority_queue<Pair> q;
for(int i = 1; i <= N; i++) {

}
sort(a + 1, a + N + 1);
for(int i = 1; i <= N; i++) {
q.push(MP(-a[i].v, i));
}
int ans = 0;
for(int i = 1; i <= N; i++) {
int sum = 0;
for(int j = i; j <= i + M - 1 && j <= N; j++) {
sum += a[j].v;
ans = max(ans, Get(sum, dv) - Get(a[j].s - a[i].s, ds));
}
}
// printf("%d\n", ans);
for(int i = 1; i <= N; i++) r[i] = i + 1, l[i] = i - 1;
while(!q.empty()) {
//   printf("%d\n", q.top().fi);
int pos = q.top().se; q.pop();
int sum = a[pos].v, x = pos, y = pos;
for(int j = 1; j < M; j++) {
if(l[x]) x = l[x], sum += a[x].v;
else if(r[y] <= N) y = r[y], sum += a[y].v;
}
while(x != r[pos] && y <= N) {
ans = max(ans, Get(sum, dv) - Get(a[y].s - a[x].s, ds));
sum -= a[x].v;
x = r[x]; y = r[y];
sum += a[y].v;
}
r[l[pos]] = r[pos];
l[r[pos]] = l[pos];
}
printf("%lld", ans);

return 0;
}
/*
*/```

1811 篇文章121 人订阅

0 条评论

## 相关文章

30980

14220

34160

35450

12730

37960

### 2488 绿豆蛙的归宿

2488 绿豆蛙的归宿 时间限制: 1 s 空间限制: 64000 KB 题目等级 : 黄金 Gold 题目描述 Description 　　随着...

1.2K70

46690

36670

### POJ 刷题系列：1083. Moving Tables

POJ 刷题系列：1083. Moving Tables 传送门：POJ 1083. Moving Tables 题意： 一条走廊，两栏房间。搬运工每次从房价...

227100