[英]Why do I need different sorting formats in C++ for sorting arrays and priority queues on this USACO code?
I'm pretty new to C++, and was trying a USACO problem from this past year.我对 C++ 很陌生,并且在去年尝试了一个 USACO 问题。 This is my code below, which works, but took hours to fiddle around with the formatting.
这是我下面的代码,它有效,但花了几个小时来摆弄格式。 It turns out that I needed
原来我需要
bool sorting(total a, total b) {return a.t < b.t;}
to be able to sort an array of objects (which failed for my priority queue), whereas I needed能够对对象数组进行排序(对于我的优先级队列失败),而我需要
struct uppersort {
bool operator()(bounded a, bounded b) {
return a.u > b.u;
}
};
to be able to sort a priority queue (which failed for my array).能够对优先级队列进行排序(我的阵列失败了)。 I'm just wondering why this was true, and if there's a simpler way to do either part.
我只是想知道为什么这是真的,以及是否有更简单的方法来完成任一部分。 Actually, I'm also just looking for ways to simplify my code in general.
实际上,我也只是在寻找总体上简化代码的方法。 Thanks!
谢谢!
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
struct bounded {
int cow;
long l;
long u;
};
struct total {
long t;
long whichcow;
bool begorend;
};
struct uppersort {
bool operator()(bounded a, bounded b) {
return a.u > b.u;
}
};
bool sorting(total a, total b) {return a.t < b.t;}
int main() {
ofstream fout ("lifeguards.out");
ifstream fin ("lifeguards.in");
long n;
fin >> n;
vector<bounded> cows(n);
for(int i=0; i<n; i++) {
fin >> cows[i].l >> cows[i].u;
cows[i].cow = i;
}
vector<total> endpoints(2*n);
for(int i=0; i<n; i++) {
endpoints[i].t = cows[i].l;
endpoints[i].whichcow = i;
endpoints[i].begorend = 0;
endpoints[n+i].t=cows[i].u;
endpoints[n+i].whichcow = i;
endpoints[n+i].begorend = 1;
}
sort(endpoints.begin(), endpoints.end(), sorting);
int containnumber = 0;
long totaltime = 0;
vector<long> eachtime(n);
long prevtime = 0;
long curtime = 0;
priority_queue<bounded, vector<bounded>, uppersort> contains;
for(int i=0; i<2*n; i++) {
prevtime = curtime;
curtime = endpoints[i].t;
if(containnumber==1) {
eachtime[contains.top().cow] += curtime - prevtime;
}
if(containnumber >0) {
totaltime += curtime - prevtime;
}
if(endpoints[i].begorend==0) {
contains.push(cows[endpoints[i].whichcow]);
containnumber++;
} else {
contains.pop();
containnumber--;
}
}
long min = -1;
for(int i=0; i<n; i++) {
if(min==-1 || eachtime[i]<min) {
min = eachtime[i];
}
}
fout << totaltime-min << endl;
}
Both sort
and priority_queue
are expecting the provided type to be sort-able (haveoperator<
). sort
和priority_queue
都期望提供的类型是可排序的(有operator<
)。
Since neither your bounded
nor your total
do, you have to provide your own function in both cases.由于您的
bounded
和total
都没有,因此您必须在两种情况下都提供自己的函数。 The difference in format you asked about is just an artifact of the interface.您询问的格式差异只是界面的一个工件。
In both cases the simpler or more elegant solution is to define the comparison operators on your types.在这两种情况下,更简单或更优雅的解决方案是在您的类型上定义 比较运算符。
struct bounded {
int cow;
long l;
long u;
bool operator<(bound rhs)const{
return u<rhs.u;
}
};
struct total {
long t;
long whichcow;
bool begorend;
bool operator<(total rhs)const{
return t<rhs.t;
}
};
You should fill in the rest of the comparison operators for good form (or just compare_three_way
, operator<=>
, if you are using c++20 or newer).您应该填写其余的比较运算符以获得良好的形式(或者只是
compare_three_way
, operator<=>
,如果您使用的是 c++20 或更新版本)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.