如下排序方式为什么会导致WA 90
查看原帖
如下排序方式为什么会导致WA 90
392157
Little_CartG2_leaf楼主2024/10/31 14:10

rt

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define PII pair<int,int>
#define fi first
#define se second
#define mkp make_pair
const int N=300024;
int t,n,m,ans,sum;
struct node{
	int d,l;
	bool operator<(node x)const{
		return d<x.d;
	}
	bool operator>(node x)const{
		return d>x.d;
	}
}a[N];
bool cmp(node x,node y){
	return x.l<y.l;
}
priority_queue<node,vector<node> > q;
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>a[i].d>>a[i].l;
	}
	sort(a+1,a+1+n,cmp);
	for(int i=1;i<=n;i++){
		if(sum+a[i].d<=a[i].l){
			q.push(a[i]);
			ans++;
			sum+=a[i].d;
		}
		else{
			if(a[i].d<q.top().d){
				sum-=q.top().d;
				sum+=a[i].d;
				q.pop();
				q.push(a[i]);
			}
		}
	}
	cout<<ans;
	return 0;
}

该代码中 cmp 按照 t2t_2 升序排列,是正确的,取得了 100 分。

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define PII pair<int,int>
#define fi first
#define se second
#define mkp make_pair
const int N=300024;
int t,n,m,ans,sum;
struct node{
	int d,l;
	bool operator<(node x)const{
		return d<x.d;
	}
	bool operator>(node x)const{
		return d>x.d;
	}
}a[N];
bool cmp(node x,node y){
	return (x.l-x.d)<(y.l-y.d);
}
priority_queue<node,vector<node> > q;
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>a[i].d>>a[i].l;
	}
	sort(a+1,a+1+n,cmp);
	for(int i=1;i<=n;i++){
		if(sum+a[i].d<=a[i].l){
			q.push(a[i]);
			ans++;
			sum+=a[i].d;
		}
		else{
			if(a[i].d<q.top().d){
				sum-=q.top().d;
				sum+=a[i].d;
				q.pop();
				q.push(a[i]);
			}
		}
	}
	cout<<ans;
	return 0;
}

该代码中 cmp 按照 t2t1t_2-t_1 升序排列,但是是错误的,WA on 4,取得了 90 分,不是很理解为什么按照 t2t1t_2-t_1 升序排列是错误的,求一个证明。

2024/10/31 14:10
加载中...