题目描述
小水喜欢吃正方形的巧克力。但是小水现在手里只有 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 Clipboard9
#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;
}
本文介绍了如何解决一个关于巧克力切割的问题,其中小水试图从长方形巧克力中切出正方形来控制饮食。通过使用优先队列和贪心策略,算法可以确定小水能获得的最大正方形巧克力总面积。示例输入和输出展示了算法的运行情况。
821

被折叠的 条评论
为什么被折叠?



