I am coded a solution for the problem: Kefka and Company which is on codeforces. problem link: https://codeforces.com/problemset/problem/580/B
I have made a vector of pairs. I use a for loop for traversing every pair of the vector. And I take in consideration the first element of the pair and run upper_bound function. Then I add all the 2nd elements in the pairs from the current index till the index provided by upper_bound. Then I compare the answer variable with current sum and update the answer variable.
code:
#include<bits/stdc++.h>
using namespace std;
int main(){
long n,d,m,s;
cin>>n>>d;
vector< pair<long ,long> > v;
for(long i=0;i<n;i++){
cin>>m>>s;
v.push_back( {m,s} );
}
sort(v.begin(),v.end());
long ans=0;
for(long i=0;i<n;i++){
long j = upper_bound(v.begin(),v.end(), make_pair(v[i].first+d,INT_MIN) ) - v.begin();
long x=0;
long f=i;
while(f<j){
x+=v[f].second;
f+=1;
}
ans=max(ans,x);
}
cout<<ans;
}
The code works fine and gives the correct result for smaller testcases but it cannot deal larger test cases. So i changed the 'int' datatype to 'long' but I get this error along with many other:
/usr/include/c++/6/bits/predefined_ops.h:72:22: error: no match for ‘operator<’ (operand types are ‘const std::pair’ and ‘std::pair’)
{ return __val < *__it; }
Comparing std::pair
s is only supported if the template arguments are the same.
In your code, make_pair(v[i].first+d,INT_MIN)
produce the type std::pair<long,int>
.
You likely meant make_pair(v[i].first+d,LONG_MIN)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.