谁来帮帮我看看代码啊,扑街了
查看原帖
谁来帮帮我看看代码啊,扑街了
794146
SCma楼主2023/1/9 20:15
#include<bits/stdc++.h>
#define long long int
using namespace std;
struct node{
    int id,price,date,digits;
    bool operator <(node b)const{//重载运算符 
        return price<b.price;
    }
    bool operator >(node b)const{//重载运算符 
    	return price>b.price;
	}
}a[100010];
bool cmp(const node ac,const node b){
	return ac.price < b.price;
}
int days,kinds,sum,hasn[100010];
priority_queue<node,vector<node>,greater<node> >q;//小根堆 
int main(){
	cin.tie();//解绑 
	cout.tie();//解绑 
	cin >> days >> kinds;
	for(int i=1;i<=kinds;i++){
		a[i].id=i;
		cin >> a[i].price >> a[i].date >> a[i].digits;
		q.push(a[i]);
	}//快读 
	sort(a+1,a+1+kinds,cmp);
	int pos=1;
	for(int i=days;i>0;i--){
		while(days-i==q.top().date/*如果超过保质期*/){
			q.pop();//弹出 
			if(pos<=kinds) pos+=1;
			q.push(a[pos]);//更新堆 
		}
		if(q.empty()){//如果堆为空:没有合适巧克力商品 
			cout << "-1" << endl;
			return 0;
		}
		node tmp = q.top();
		sum += tmp.price;
		hasn[tmp.id]++;//计数 
		if(tmp.digits==hasn[tmp.id]){
			q.pop();
		}
	} 
	cout << sum << endl;
	return 0;
} 
2023/1/9 20:15
加载中...