您的位置:首页 > 其它

Gym100820G/UVALive7374 Racing Gems 二维LIS 思维题

2016-08-02 19:59 363 查看

Gym100820G/UVALive7374  Racing Gems 二维LIS 思维题

题目链接
Vjudge
题意:赛车在宽为W,高度为H的跑道上捡金币。假设赛车垂直速度为V0,水平速度为V1,有 -V0/R<= V1 <= V0/R。现在给定W,H,R和N个金币的坐标,求小车从y=0的任意横坐标出发,到达y=H时,所能获得的最多金币数。
这就是今年的校赛的母题啊!
牢骚:神奇的思维。前几天做过了一个二维LIS,今天做这个题目依旧还是没有与二维的LIS联系起来。唉!
分析:分析(x0, y0)这个位置上的金币。 假如赛车到了这个位置,那么接下来可达的范围就如下图所示了。 每个点都与x=0,与x=W 分别有一个交点, 从图中可以发现,从一个点(X0,Y0)可以到达另外一个点(X1, Y1)当且仅当RX0+Y0<=RX1+Y1 并且 R(W-X0)+Y0 <= R(W-X1)+Y1。 对于每个点(xi,yi),令a=R*xi + yi, b = R*(W-xi)+yi。
只需要计算(a, b) 二维的LIS就可以的。求二维的LIS步骤: 先对其中一维进行排序,然后对另一维求最长不降子序列。



#include <cmath>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <algorithm>
using namespace std;

using namespace std;

#define FIN             freopen("input.txt","r",stdin)
#define FOUT            freopen("output.txt","w",stdout)
#define fst             first
#define snd             second
typedef __int64         LL;
typedef pair<int, int>  PII;
typedef pair<LL, LL>    PLL;
const int MAXN = 1e5 + 5;

int N, R, W, H;
PLL F[MAXN];
LL I[MAXN];
int LIS() {
int len = 0;
I[len ++] = F[0].snd;
for (int i = 1; i < N; i++) {
if (F[i].snd >= I[len - 1]) I[len ++] = F[i].snd;
else {
int pos = upper_bound (I, I + len, F[i].snd) - I;
I[pos] = F[i].snd;
}
}
return len;
}

int main() {
#ifndef ONLINE_JUDGE
FIN;
#endif // ONLINE_JUDGE
while (~scanf ("%d %d %d %d", &N, &R, &W, &H) ) {
int x, y;
for (int i = 0; i < N; i++) {
scanf ("%d %d", &x, &y);
F[i].fst = (LL) R * x + y;
F[i].snd = (LL) R * (W - x) + y;
}
sort (F, F + N);
int len = LIS();
printf ("%d\n", len);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: