经典的贪心模型,常规思路:将M和B排序即可
看到没有人用优先队列,于是我的showtime到了
说下思路:
读入时将数加入啊a,b堆中,不用处理(二叉堆本来就有有序的性质)
读完后逐个判断,照题目模拟即可
总时间复杂度:O(nlogn) 其实就是堆排序的时间复杂度
楼下的那位用了sort还说是O(2n)的,大家不要犯这种低级错误
贴代码:
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<ctime>
#include<iostream>
#include<string>
#include<vector>
#include<list>
#include<deque>
#include<stack>
#include<queue>
#include<deque>
#include<map>
#include<set>
#include<algorithm>
#pragma GCC optimize(2)//STL必带
using namespace std;
#define ll long long
const int maxn=0x7f;
const int INF=0x7fffffff;
inline void read(int&x){//quick input
int data=0,w=1;
char ch=getchar();
while(ch!='-'&&!isdigit(ch))
ch=getchar();
if(ch=='-')
w=-1,ch=getchar();
while(isdigit(ch))
data=10*data+ch-'0',ch=getchar();
x=data*w;
}
void write(int x){//quick output
if(x<0)
putchar('-'),x=-x;
if(x>9)
write(x/10);
putchar('0'+x%10);
}
priority_queue<int> a,b;
int main()
{
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
int n,x,y,t;
read(n);read(x);read(y);
for(int i=1;i<=n;++i){//use variable "t" to improve memory and time
read(t);
a.push(t);
read(t);
b.push(t);
}
int ans=0,u,v;
while(!a.empty()){//slower than "sort", easier to understand though
u=a.top();
a.pop();
v=b.top();
b.pop();
if(u<v)//do as what the problem said
ans+=(v-u)*x;
else if(u>v)
ans+=(u-v)*y;
}
write(ans);
// fclose(stdin);
// fclose(stdout);
return 0;
}