http://m.blog.youkuaiyun.com/c20190102/article/details/78550025
http://m.blog.youkuaiyun.com/huangzihaoal/article/details/78630659
通过了8组数据。 。。。还有两组啊!!
/*
7 4 10
2 6
5 -3
10 3
11 -3
13 1
17 6
20 2
输出 2、、、、10 59 112
41 27
89 -50
112 -4
144 -45
187 42
196 35
237 8
262 2
269 -8
273 -37
输出 87
*/
#include <bits/stdc++.h>
//#define debug
#define LL long long int
using namespace std;
int n,d,k;
const int MM=500001;
int dis[MM];
int score[MM];
int dp[MM];
deque<int> dq;
bool check(int g){
memset( dp ,0 ,sizeof(dp) );
dis[0]=0;
score[0]=0;
dq.clear();
//dq.push_back(0);
int jump1 = (g<d)? (d-g) : 1;
int jump2 = g+d;
// cout << "jump1 " << jump1 <<" ";
//cout << "jump2 " << jump2 ;
for(int i=1;i<=n;i++){
//这一行,由于删除掉的可能在下一次还可能有用。 调试了很久!!!还没有好办法
// for( int j=( dq.empty()? 0:dq.back()) ; j<=i-1; j++ ) dq.push_back( j );
dq.push_back( i-1 );
while( !dq.empty() && dis[i] - dis[ dq.front() ] > jump2 ) dq.pop_front(); //太远不行
//while ( !dq.empty() && dis[i] - dis[ dq.back() ] < jump1 ) dq.pop_back(); //太近也不行
deque<int>::iterator it;
#ifdef debug
cout << "debug ";
for( it=dq.begin();it!=dq.end();it++){
cout << *it << " ";
}
cout << "end debug" << endl;
#endif // debug
if( dq.empty() ) {
dp[i] = 0;
return false;
}
else {
dp[i] += score[ i ];
int temp=-100000;
for( it = dq.begin();it!=dq.end();it++)
if( dis[i] - dis[ *it ] >= jump1 ) //这一行,由于删除掉的可能在下一次还可能有用。所以在队伍后面没有删除。 调试了很久!!!只能用这个丑丑的代码了。
temp = max(temp, dp[ *it ] ); else break; dp[i] = dp[i] + temp ; #ifdef debug cout << "dp "; cout << i << " " << dp[i] << " "; cout << endl; #endif // debug if(dp[i] >= k) return true; } } return false;}int main(){cin>>n>>d>>k;for(int i=1;
i<=n; i++){ cin >> dis[i]; cin >> score[i]; // 数据保证 dis递增}int left = 1;int right = 1000000; int ans=-1;while( left+1 < right ) { int middle = (left+right) / 2; if( check( middle ) ) { ans=middle; right = middle; } else left = middle;} cout << ans;return 0;}