优先队列

田浩廷 代码

include

include

include

using namespace std;

struct shoes
{
int power;
int money;
};

struct cmp {
bool operator ()(shoes &a, shoes &b) {
if (a.power != b.power) {
return a.power < b.power;
}
else {
return a.money > b.money;
}
}
};

shoes a[100001];
int main() {
priority_queue, cmp> shooes;
priority_queue blood;
int N, M, temp, ans, min = 0, k;
bool flag = true;
cin >> N >> M;
shoes tempshoe;
for (int i = 0; i < N; i++) {
cin >> temp;
blood.push(temp);
}
for (int i = 0; i < M; i++) {
cin >> a[i].power;
}
for (int i = 0; i < M; i++) {
cin >> a[i].money;
}
for (int i = 0; i < M; i++) {
shooes.push(a[i]);
}
int len1 = blood.size();
int len2 = shooes.size();
//cout << len1 << " " << len2 << endl;
for (int i = 0; i < len1; i++) {
k = 0;
ans = 9999999;
temp = blood.top();
blood.pop();
for (int j = 0; j < len2; j++) {
if (shooes.empty() || (shooes.top().power < temp)) { //最大能量踩不死猫
if (j == 0) {
flag = false;
}
break;
}
else {
if (ans > shooes.top().money) {
ans = shooes.top().money;
tempshoe = shooes.top();
}
a[k++] = shooes.top();
//t.push(shooes.top());
shooes.pop();
}
}
if (!flag) {
break;
}
min += ans;
//cout << ans << endl;
/while (!t.empty()) {
if (t.top().money == tempshoe.money && t.top().power == tempshoe.power) {
t.pop();
continue;
}
shooes.push(t.top());
t.pop();
}
/
for (int ii = 0; ii < k; ii++) {
if (a[ii].money == tempshoe.money && a[ii].power == tempshoe.power) {
continue;
}
shooes.push(a[ii]);
}
}
if (flag) {
cout << min << endl;
}
else {
cout << "no" << endl;
}
return 0;
}

张楠的代码

include

include

include

using namespace std;
struct shoes
{
int power;
int money;
};
struct cmp {
bool operator ()(shoes &a, shoes &b) {//钱少 力量少
if (a.money == b.money) {
return a.power > b.power;
}
else {
return a.money > b.money;
}
}
};
struct cmp1 {
bool operator ()(shoes &a, shoes &b) {// 力量大
if (a.power == b.power) {
return a.money > b.money;
}
else {
return a.power < b.power;
}
}
};
shoes a[100001];
int main() {
priority_queue, cmp1> shooes;
priority_queue, cmp> sh;
priority_queue blood;
int N, M, temp, ans, min = 0, k;
bool flag = true;
cin >> N >> M;
shoes tempshoe;
for (int i = 0; i < N; i++) {
cin >> temp;
blood.push(temp);
}
for (int i = 0; i < M; i++) {
cin >> a[i].power;
}
for (int i = 0; i < M; i++) {
cin >> a[i].money;
}
for (int i = 0; i < M; i++) {
shooes.push(a[i]);//鞋大的排在前面
}
int t;
int len1 = blood.size();//蚂蚁的个数
int len2 = shooes.size();//鞋子的个数
int temp1, temp2;
int ff=0;//有没有鞋
while(len1!=0) {//当蚂蚁的个数为零
t = blood.top();//血量最大的
len1--;//少一只蚂蚁
blood.pop();
while (!shooes.empty())//鞋不空
{
temp1 = shooes.top().power;//力量最大的鞋
if (temp1 >= t)
{
sh.push(shooes.top());
shooes.pop();
}
else
{
break;
}
}
if (sh.empty())//没有鞋
{
cout << "No" << endl;
return 0;
}
min += sh.top().money;//钱最少的弹出
sh.pop();
len2--;
}
cout << min << endl;
return 0;
}

你可能感兴趣的:(优先队列)