【发布时间】:2021-07-24 02:03:04
【问题描述】:
我为这个问题编写了一个解决方案:在 codeforces 上的 Kefka and Company。 问题链接:https://codeforces.com/problemset/problem/580/B
我已经制作了一个成对的向量。我使用 for 循环遍历每一对向量。我考虑了该对的第一个元素并运行了 upper_bound 函数。然后我添加从当前索引到由upper_bound 提供的索引的对中的所有第二个元素。然后我将答案变量与当前总和进行比较并更新答案变量。
代码:
#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;
}
该代码工作正常,并为较小的测试用例提供了正确的结果,但它不能处理较大的测试用例。所以我将“int”数据类型更改为“long”,但我得到了这个错误以及许多其他错误:
/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; }
【问题讨论】:
-
我尝试包含
#include<pair>,但它给出了错误:#include<pair> ^compilation terminated. -
我的错误
std::pair定义在<utility>中也缺少<vector>、<algorithm>和<iostream>。 -
这是代码:ideone.com/YTfvk9。我按照你说的做了修改。但仍然收到此错误。
/usr/include/c++/6/bits/predefined_ops.h:72:22: error: no match for ‘operator<’ (operand types are ‘const std::pair’ and ‘std::pair’) -
您是否故意编辑错误消息?您链接中的消息实际上报告了
(operand types are ‘const std::pair<long int, int>’ and ‘std::pair<long int, long int>’)
标签: c++ vector binary-search