codeforces #321 div 2 B. Kefa and Company(尺取法)
2015-09-25 22:46
357 查看
B. Kefa and Company
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Kefa wants to celebrate his first big salary by going to restaurant. However, he needs company.
Kefa has n friends, each friend will agree to go to the restaurant if Kefa asks. Each friend is characterized by the amount of money he has and the friendship factor in respect to Kefa. The parrot doesn't want any friend to feel poor compared to somebody else in the company (Kefa doesn't count). A friend feels poor if in the company there is someone who has at least d units of money more than he does. Also, Kefa wants the total friendship factor of the members of the company to be maximum. Help him invite an optimal company!
Input
The first line of the input contains two space-separated integers, n and d (1 ≤ n ≤ 105,
View Code
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Kefa wants to celebrate his first big salary by going to restaurant. However, he needs company.
Kefa has n friends, each friend will agree to go to the restaurant if Kefa asks. Each friend is characterized by the amount of money he has and the friendship factor in respect to Kefa. The parrot doesn't want any friend to feel poor compared to somebody else in the company (Kefa doesn't count). A friend feels poor if in the company there is someone who has at least d units of money more than he does. Also, Kefa wants the total friendship factor of the members of the company to be maximum. Help him invite an optimal company!
Input
The first line of the input contains two space-separated integers, n and d (1 ≤ n ≤ 105,
/************************************************************************* > File Name: code/cf/#321/B.cpp > Author: 111qqz > Email: rkz2013@126.com > Created Time: 2015年09月25日 星期五 17时21分55秒 ************************************************************************/ #include<iostream> #include<iomanip> #include<cstdio> #include<algorithm> #include<cmath> #include<cstring> #include<string> #include<map> #include<set> #include<queue> #include<vector> #include<stack> #include<cctype> #define y1 hust111qqz #define yn hez111qqz #define j1 cute111qqz #define ms(a,x) memset(a,x,sizeof(a)) #define lr dying111qqz using namespace std; #define For(i, n) for (int i=0;i<int(n);++i) typedef long long LL; typedef double DB; const int inf = 0x3f3f3f3f; const int N=1E5+7; LL n,d; struct Q { LL m,f; }q ; //没开long long ,再wa一发,sssssad bool cmp(Q a,Q b) { if (a.m<b.m) return true; return false; } void solve() { LL head = 0 ; LL tail = 0 ; LL sum = 0 ; LL ans = -1 ; while (head<n) { while (q[tail].m-q[head].m<d&&tail<n) //条件看错...不能等于d,怒wa一发 { sum += q[tail].f; // cout<<"sum:"<<sum<<endl; tail++; } // cout<<"sum:"<<sum<<endl; ans = max (ans,sum); sum -= q[head].f; head++; } cout<<ans<<endl; } int main() { #ifndef ONLINE_JUDGE freopen("in.txt","r",stdin); #endif while(scanf("%I64d %I64d",&n,&d)!=EOF) { for ( int i = 0; i < n ; i++) cin>>q[i].m>>q[i].f; sort(q,q+n,cmp); // for ( int i = 0 ; i < n ; i++) cout<<q[i].m<<" "<<q[i].f<<endl; solve(); } #ifndef ONLINE_JUDGE fclose(stdin); #endif return 0; }
View Code
相关文章推荐