以下代码可对结构体数组中的元素进行排序,也差不多算是一个小小的模板了吧
#include
#include
using namespace std;
struct node
{
int x;
int y;
bool operator
{
return x < a.x;//对结构体数组x进行从大到小排序
//return y > a.y;//对结构体数组y进行从大到小排序
}
}s[100];
//bool cmp(int x,int y)//这个重构函数不能用在结构体数组中
//{
//return x > y;
//}
int main()
{
int n;
cin >> n;
for(int i = 0;i < n;i++)
cin >> s[i].x >> s[i].y;
sort(s,s+n);
for(int i = 0;i < n;i++)
cout << s[i].x << " " << s[i].y << endl;
cout << endl;
return 0;
}
运行结果:
也可以这样
#include#include
using namespacestd;structnode
{intx;inty;bool operatora.x;
}
}s[100];boolcmp(node m,node n)
{
m.x
}intmain()
{intn;
cin>>n;for(int i = 0;i < n;i++)
cin>> s[i].x >>s[i].y;
sort(s,s+n);for(int i = 0;i < n;i++)
cout<< s[i].x << " " << s[i].y <
cout<
}
对优先队列的应用,POJ2431是一个很好的题目,此题用了优先队列+贪心
Expedition
Time Limit: 1000MS
Memory Limit: 65536K
Total Submissions: 29720
Accepted: 8212
Description
A group of cows grabbed a truck and ventured on an expedition deep into the jungle. Being rather poor drivers, the cows unfortunately managed to run over a rock and puncture the truck's fuel tank. The truck now leaks one unit of fuel every unit of distance it travels.
To repair the truck, the cows need to drive to the nearest town (no more than 1,000,000 units distant) down a long, winding road. On this road, between the town and the current location of the truck, there are N (1 <= N <= 10,000) fuel stops where the cows can stop to acquire additional fuel (1..100 units at each stop).
The jungle is a dangerous place for humans and is especially dangerous for cows. Therefore, the cows want to make the minimum possible number of stops for fuel on the way to the town. Fortunately, the capacity of the fuel tank on their truck is so large that there is effectively no limit to the amount of fuel it can hold. The truck is currently L units away from the town and has P units of fuel (1 <= P <= 1,000,000).
Determine the minimum number of stops needed to reach the town, or if the cows cannot reach the town at all.
Input
* Line 1: A single integer, N
* Lines 2..N+1: Each line contains two space-separated integers describing a fuel stop: The first integer is the distance from the town to the stop; the second is the amount of fuel available at that stop.
* Line N+2: Two space-separated integers, L and P
Output
* Line 1: A single integer giving the minimum number of fuel stops necessary to reach the town. If it is not possible to reach the town, output -1.
Sample Input
4
4 4
5 2
11 5
15 10
25 10
Sample Output
2
Hint
INPUT DETAILS:
The truck is 25 units away from the town; the truck has 10 units of fuel. Along the road, there are 4 fuel stops at distances 4, 5, 11, and 15 from the town (so these are initially at distances 21, 20, 14, and 10 from the truck). These fuel stops can supply up to 4, 2, 5, and 10 units of fuel, respectively.
OUTPUT DETAILS:
Drive 10 units, stop to acquire 10 more units of fuel, drive 4 more units, stop to acquire 5 more units of fuel, then drive to the town.
Source
思路:贪心思路为,在假设汽车一路向前,在途中有经过加油站的话,把加油站进行排序,符合条件的加油站中的汽油从大到小排序,存到优先队列中,在汽车用完时,即随时可以在队列的队头取汽油,使得汽车尽可能加最多的汽油,停最少次
#include#include#include
using namespacestd;structnode
{intdis;intfuel;bool operator
{return dis > a.dis;//返回dis,说明是对dis这个数组进行排序操作
}
}stop[10005];
priority_queueque;intmain()
{intn,l,p;
cin>>n;for(int i = 0;i < n;i++)
cin>> stop[i].dis >>stop[i].fuel;
cin>> l >>p;int ans = 0;
sort(stop,stop+ n);//对加油站距离dis数组进行从大到小排序
que.push(p);//队列自动从大到小排序,即排序汽油p
int temp = 0;while(l > 0 && !que.empty())//卡车未到达终点并且卡车在当前汽油用完前有路过加油站
{
ans++;//卡车停下加一次油计数器
l -= que.top();//加油,更新一次汽油用尽后距离终点的距离
que.pop();//删除已用的加油站的汽油
while(l <= stop[temp].dis && temp < n)//卡车距离终点的距离小于等于最近加油站的距离并且这个加油站的位置在终点加油站前面,这里假设终点也为一个加油站。//l <= stop[i].dis意思是卡车能经过离它最近的一个加油站,如果大于的话,说明卡车停下时没有加油站可加油
que.push(stop[temp++].fuel);//将经过的加油站压入优先队列,要使用的时候就取队头元素(队头中存的汽油最大) //如果经过加油站,则一定要将该加油站的可加油量添加到优先队列当中
}//temp++说明离卡车最近的加油站,卡车继续往前开,加油站点也依次往后,所以变量temp需要自增if(l > 0)//如果卡车距离终点的距离还大于0的话,即通过不了终点
cout << "-1" <
return 0;
}
其中代码有详细的注释,希望注释能加深理解
代码参考于:博客园-小小菜鸟