XYK的音游
Problem Description
xyk最近入坑了一个新音游。
游戏界面上有 n n n 个并排的按键,当前这首歌有 m m m 个鼓点。游戏的玩法是在鼓点的时刻移动鼠标到对应的按键上并点击来获取分数。
对于第 i 个鼓点,我们用 t i t_i ti, w i w_i wi, x i x_i xi 来描述,表示在 t i t_i ti 时刻点击第 x i x_i xi 个按键会获得 w i w_i wi 的分数。
由于xyk手速很慢,每个时刻只能移动 p 个按键的距离。也就是说如果 s 时刻xyk的鼠标在第
p
o
s
pos
pos 个按键上,那么
s
+
1
s+1
s+1 时刻他能够把鼠标移动到
[
p
o
s
−
p
,
p
o
s
+
p
]
[pos−p,pos+p]
[pos−p,pos+p] 的按键。
在 0 时刻xyk可以把鼠标放在任意位置。鼓点出现的时刻大于 0 。保证同一时间不会有两个鼓点。由于这款音游并不看重连击,xyk希望分数尽可能高就好。请你帮助他计算他能获得的最高分数。
对于 30% 的数据, p=1 。
对于 50% 的数据, n×m≤106 。
对于 100% 的数据, n,m≤100000,p≤5,ti≤1000000 。
Input
第一行三个正整数 n,m,p。
接下来 m 行每行三个正整数 t_i,w_i,x_i 描述第 i 个鼓点。
Output
一行一个整数表示最大分数。
Sample Input
5 5 1
2 42 4
3 23 4
1 70 4
2 31 5
1 85 5
Sample Output
150
题解:
设第i个按键可以由第j个按键转移的条件为
p
∗
t
i
−
p
∗
t
j
≥
∣
x
i
−
x
j
∣
p*t_i-p*t_j\ge|x_i-x_j|
p∗ti−p∗tj≥∣xi−xj∣
=>
p
∗
t
i
−
p
∗
t
j
≥
x
i
−
x
j
,
p
∗
t
i
−
p
∗
t
j
≥
x
j
−
x
i
p*t_i-p*t_j \ge x_i-x_j,p*t_i-p*t_j \ge x_j-x_i
p∗ti−p∗tj≥xi−xj,p∗ti−p∗tj≥xj−xi
=>
p
∗
t
i
−
x
i
≥
p
∗
t
j
−
x
j
,
p
∗
t
i
+
x
i
≥
p
∗
t
j
+
x
j
p*t_i-x_i \ge p*t_j-x_j , p*t_i+x_i \ge p*t_j+x_j
p∗ti−xi≥p∗tj−xj,p∗ti+xi≥p∗tj+xj
有点像二维偏序,将前面的排序处理,后面部分利用树状数组即可,维护
y
=
p
∗
t
i
+
x
i
y=p*t_i+x_i
y=p∗ti+xi时,得分的最大值。
#include<stdio.h>
#include<iostream>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<set>
#include<vector>
#include<queue>
#include<iterator>
#define dbg(x) cout<<#x<<" = "<<x<<endl;
#define INF 0x3f3f3f3f
#define eps 1e-7
using namespace std;
typedef long long LL;
typedef pair<LL, int> P;
const int maxn = 100100;
const int mod = 998244353;
struct node{
int t, w, x;
}p[maxn];
int q, dp[maxn], a[maxn], b[maxn];
bool cmp(node a, node b);
int lowbit(int x);
int query(int x);
void Update(int x, int y, int n);
int main()
{
int n, m, i, j, k, ans = 0;
scanf("%d %d %d", &n, &m, &q);
for(i=1;i<=m;i++){
scanf("%d %d %d", &p[i].t, &p[i].w, &p[i].x);
a[i] = p[i].t*q+p[i].x;
}
sort(a+1, a+1+m);
sort(p+1, p+1+m, cmp);
for(i=1;i<=m;i++){
int pos = lower_bound(a+1, a+1+m, p[i].t*q+p[i].x)-a;
dp[i] = p[i].w + query(pos);
ans = max(ans, dp[i]);
Update(pos, dp[i], m);
}
printf("%d\n", ans);
return 0;
}
bool cmp(node a, node b)
{
if(a.t*q-a.x != b.t*q-b.x)return a.t*q-a.x < b.t*q-b.x;
else return a.t*q+a.x<b.t*q+b.x;
}
int lowbit(int x)
{
return x&-x;
}
void Update(int x, int y, int n)
{
for(int i=x;i<=n;i+=lowbit(i))
b[i] = max(b[i], y);
}
int query(int x)
{
int mx = 0;
for(int i=x;i>0;i-=lowbit(i))
mx = max(b[i], mx);
return mx;
}