P1799 小水同学的巧克力(加强版)

本文介绍了如何解决一个关于巧克力切割的问题,其中小水试图从长方形巧克力中切出正方形来控制饮食。通过使用优先队列和贪心策略,算法可以确定小水能获得的最大正方形巧克力总面积。示例输入和输出展示了算法的运行情况。

题目描述

小水喜欢吃正方形的巧克力。但是小水现在手里只有 nn 块长方形的巧克力,小水可以从这些巧克力上切下一些正方形的巧克力。为了控制饮食,小水最多只能吃 mm 块正方形巧克力,并且要求切完后原巧克力还是矩形。小水想知道,他最多能吃到多少面积的正方形巧克力。

输入描述

第一行两个正整数n,m(1≤n≤106,1≤m≤105), 意义如题面所示。 接下来 nn 行, 每行两个正整数 a,b(1≤a,b≤100000) 表示长方形巧克力的边长。

输出描述

输出一个正整数,表示小水最多能吃多少面积的正方形巧克力。

样例输入

Copy to Clipboard
2 3 2 3 2 3 

样例输出

Copy to Clipboard
9
#include<bits/stdc++.h>
#include<set>
using namespace std;

typedef struct cookie{
	long long int chang;
	long long int kuan; 
	friend bool operator < (const cookie& n1, const cookie& n2)
    {
        return n1.kuan < n2.kuan;
    }
}cookie;

int main(){
	long long int n, m, i, j, x, y;
	cookie temp;
	while(cin>>n>>m){
		multiset<cookie> c;
		long long int re = 0;
		for(i = 0; i < n; i++){
			cin>>x>>y;
			temp.chang = max(x, y);
			temp.kuan = min(x, y);
			c.insert(temp);
		}
		while(m && !c.empty()){
			multiset<cookie>::iterator iter = --c.end();
			re += (*iter).kuan * (*iter).kuan;
			temp.chang = max((*iter).kuan, (*iter).chang - (*iter).kuan);
			temp.kuan = min((*iter).kuan, (*iter).chang - (*iter).kuan);
			c.erase(--c.end());
			m--;
			if(temp.kuan)
				c.insert(temp);
		}
		cout<<re<<endl;
	}
	return 0;
}
/*
 * @Description: To iterate is human, to recurse divine.
 * @Autor: Recursion
 * @Date: 2022-06-06 14:20:40
 * @LastEditTime: 2022-06-06 19:15:18
 */
#include <bits/stdc++.h>
#define LL long long 
using namespace std;
const int N = 500;

LL n,m;
struct node{
    LL x;//长
    LL y;//宽
    bool operator < (const node& x1) const{
        return y < x1.y;
    }
}a[N];
priority_queue<node> q;

LL ans;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    cin >> n >> m;

    node temp;
    for(int i = 1;i <= n;i ++){
        int x,y;
        cin >> x >> y;
        temp.x = max(x,y);
        temp.y = min(x,y);
        q.push(temp);
    }

    // while(!q.empty()){
    //     node temp = q.top();
    //     q.pop();
    //     cout << temp.x << " " << temp.y << endl;
    // }    
    
    while(m--&&!q.empty()){
        node temp = q.top();
        q.pop();
        ans += temp.y*temp.y;
        int tx = max(temp.x - temp.y,temp.y);
        int ty = min(temp.x - temp.y,temp.y);
        temp.x = tx;
        temp.y = ty;
        if(temp.y)
            q.push(temp);
    }

    cout << ans << endl;
    return 0;
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值