HDU 1051(处理木棍 贪心)
2024-10-14 15:18:51
题意是处理一批木棍,如果当前处理的木棍长度和重量均大于前一根木棍的长度和重量,则处理当前木棍花费为 0,否则花费为 1。
用结构体存储木棍信息,将木棍按照长度从小到大排序,若长度相等则按照重量从小到大排序,在序列中找到比当前所处理的木棍长度和重量均大于且未处理的木棍再进行处理,若没有满足要求的木棍了,则再次从头开始处理未处理过的木棍,直到所有的木棍处理完毕。
代码如下:
#include <bits/stdc++.h>
using namespace std;
struct stick
{
int l,w,num;
}p[];
bool cmp(stick a,stick b)
{
return a.l!=b.l?a.l<b.l:a.w<b.w;
}
int main()
{
int t,n,ans,sum,el,ew;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i = ; i < n; ++i)
{
scanf("%d%d",&p[i].l,&p[i].w);
p[i].num = ;
}
sort(p,p+n,cmp);
ans = sum = ;
while(sum<n)
{
el = ew = ;
for(int i = ; i < n; ++i)
if(p[i].num&&p[i].l>=el&&p[i].w>=ew)
{
p[i].num = ;
el = p[i].l;
ew = p[i].w;
++sum;
}
++ans;
}
printf("%d\n",ans);
}
return ;
}
最新文章
- 水的demo
- HTML5 Web app开发工具Kendo UI Web中如何绑定网格到远程数据
- Android 完整开源应用,完整开源项目
- Bootstrap插件系列——Bootstrap-table初始化、分页、客户端搜索、服务端搜索
- Maven Scope
- Java Hour 42 fastjson
- Linux platform设备简介
- 设计模式_Memento_备忘录模式
- wdlinux mysql innodb的安装
- Ubuntu知识记录
- POJ 3525 Most Distant Point from the Sea
- dump json 显示中文问题
- POJ - 2912 Rochambeau 种类并查集
- idea报错:Invalid bound statement (not found)
- /usr/bin/uwsgi --http :8888 --wsgi-file wsgi.py --master --processes 4 --threads 2
- kaggle-Digit Recognizer
- sql server: Graphs, Trees, Hierarchies and Recursive Queries
- hdu 1556 A - Color the ball 其他做法
- Delphi:窗体的扩展样式GWL_EXSTYLE用于SetWindowLong
- IDEA导入jar包
热门文章
- Java文件下载:如何编码文件名称以及如何设置HttpServletResponse
- Github如何提交修改的代码以及更新到最新版本
- SQLServer之UNIQUE约束
- jenkins使用开始踩坑(1)
- java.io.IOException: There appears to be a gap in the edit log. We expected txid ***, but got txid
- 搭建vue.js环境
- Asp.Net WebApi 使用OWIN架构后,出现 “没有 OWIN 身份验证管理器与此请求相关联(No OWIN authentication manager is associated with the request)” 异常的解决办法
- 基于HTML5 的互联网+地铁行业
- CI/CD持续集成/持续部署 敏捷开发
- jeecg字典表—普通表