日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

中石油训练赛 - Racing Gems(最长不下降子序列)

發(fā)布時間:2024/4/11 编程问答 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 中石油训练赛 - Racing Gems(最长不下降子序列) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

題目描述

You? are? playing? a? racing? game.? Your? character? starts? at? the? X-axis? line? (y=0)? and proceeds up the racetrack, which has a boundary at the line x=0 and x=w.? The finish is at? y=h,? and? the? game? ends? when? you? reach? that? line.? ? You? proceed at a? fixed vertical velocity v, but you can control your horizontal velocity to be any value between -v/r and v/r,? where? r? is? a? fixed? ratio.? You? may? change? your? horizontal velocity at any time, but your vertical velocity must remain fixed.?
There are gems at specific points on the race track. Your job is to collect as many gems as possible (they all have the same value).?
How many gems can you collect? You may start at any horizontal position you want (but your vertical position must be 0 at the start).

輸入

Each input will consist of a single test case. Note that your program may be run multiple times? on? different? inputs.? The? first? line? will? contain? four? integers:? n? (1≤n≤105?)? is? the number? of? gems,? r? (1≤r≤10)? is? the? ratio? of? vertical? velocity? to? maximum? horizontal speed, w (1≤w≤109?) is the width of the track, and h (1≤h≤109?) is the height of the finish line.? ? Following? this? will? be? n? lines,? each? containing? an? integer? x? and? y? coordinate?
(0≤x≤w,1≤y≤h), containing the coordinate of a gem.? All gems will lie on the race track.? None will be on the start line.? ?

輸出

Output? a? single? integer? on? a? line? by? itself? representing the maximum number of gems that you can collect.

樣例輸入

5 1 10 10 8 8 5 1 4 6 4 7 7 9

樣例輸出

3

題目鏈接:點擊查看


題目大意:一個在二維坐標平面開展的跑車游戲,首先規(guī)定賽道邊界:左右邊界分別為x=0和x=w,上下邊界分別為y=0和y=h,現(xiàn)在要求從y=0跑到y(tǒng)=h,現(xiàn)在規(guī)定一個速度比率,也就是,在路上有許多寶石,我們需要盡可能多的去吃到寶石,問我們最后到達終點的時候,吃到寶石數(shù)的最大值是多少?

題目分析:這個題目我們算是分析了好久好久吧,先從網(wǎng)上拿來一張圖,更好理解一下這個題目:

一開始我們大概就分析到了這個地方,先給寶石對于縱坐標排序,然后可以設(shè)一個dp[i]代表在吃到寶石i的情況下獲得的最大值,那么我們很輕松就能看出來,dp[i]=max(dp[i],dp[j]+1)(1<=j<i)?,也就是我們需要在前i-1個寶石中尋找滿足斜率條件的最大值,但這樣一來時間復(fù)雜度就變?yōu)榱薾*n,對于這個題目而言不可行,后來我們討論了半天,最后終于得出了結(jié)論(%yh學(xué)長),就是將rate當一個斜率來看待,有了每一個點和斜率了,就可以求出直線方程,然后求出對于x=0和x=w的交點縱坐標,就像上圖一樣,因為每個寶石所構(gòu)成的一個帶角度的范圍內(nèi)的其余寶石,才能接受當前寶石的狀態(tài)轉(zhuǎn)移,這個應(yīng)該不難想到,隨后我們對于任意一邊的坐標升序排序,然后對于另一邊的坐標找一下最長不下降子序列即可

代碼:

#include<iostream> #include<cstdlib> #include<string> #include<cstring> #include<cstdio> #include<algorithm> #include<climits> #include<cmath> #include<cctype> #include<stack> #include<queue> #include<list> #include<vector> #include<set> #include<map> #include<sstream> #include<unordered_map> using namespace std;typedef long long LL;const int inf=0x3f3f3f3f;const int N=1e5+100;struct Node {LL x,y;bool operator<(const Node& a)const{return x<a.x;} }a[N];LL d[N];int main() { // freopen("input.txt","r",stdin);LL n,rate,w,h;scanf("%lld%lld%lld%lld",&n,&rate,&w,&h);for(int i=1;i<=n;i++){LL x,y;scanf("%lld%lld",&x,&y);a[i].x=rate*x+y;a[i].y=rate*(w-x)+y;}sort(a+1,a+1+n);int len=1;memset(d,0,sizeof(d));d[1]=a[1].y;for(int i=2;i<=n;i++){if(a[i].y>=d[len]){d[++len]=a[i].y;}else{int j=upper_bound(d+1,d+1+len,a[i].y)-d;d[j]=a[i].y;}}cout<<len<<endl;return 0; }

?

總結(jié)

以上是生活随笔為你收集整理的中石油训练赛 - Racing Gems(最长不下降子序列)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。