HDOJ.1051 Wooden Sticks (贪心)
Wooden Sticks
题意分析
给出T组数据,每组数据有n对数,分别代表每个木棍的长度l和重量w。第一个木棍加工需要1min的准备准备时间,对于刚刚经加工过的木棍,如果接下来的木棍l和w均小于等于上一根木棍的l和w那么就不许要准备时间,否则的话还需要1min的准备时间。求解对于每组数据,所需要的最小的准备时间是多少。
贪心策略。
对木棍进行降序排序,首要关键字是l,次要关键字是w(可以颠倒)。然后选取最顶端的木棍,向下遍历,对于当前木棍,若l和w均小于等于顶层木棍,那么标记它为已处理,并且更新此时木棍的数据,继续遍历,直到没有木棍未知。此时判断所有的木棍是否处理完,是的话输出结果,否则的话继续选取顶层没有处理的木棍,重复遍历的操作。
代码总览
/*
Title:HDOJ.1051
Author:pengwill
Date:2016-11-25
*/
#include <iostream>
#include <algorithm>
#include <stdio.h>
#define max 5005
using namespace std;
struct stick{
int len;
int weight;
bool fish;
}item[max];
bool cmp(stick a,stick b)
{
if(a.len == b.len){
return a.weight>b.weight;
}else{
return a.len>b.len;
}
}
int main()
{
int t;
//freopen("in.txt","r",stdin);
scanf("%d",&t);
while(t--){
int n,i;
scanf("%d",&n);
for(i = 0;i<n;i++){
scanf("%d %d",&item[i].len,&item[i].weight);
}
sort(item,item+n,cmp);
int num = 0,nlen,nweight,cnt = 0,temp= 0;
while(num!= n){
for(i = temp;i<n;i++){
if(item[i].fish == false){
nlen = item[i].len;
nweight = item[i].weight;
item[i].fish = true;
num++;cnt++;i++;
temp = i;
break;
}
}
for(;i<n;i++){
if(item[i].fish == false && item[i].len<=nlen && item[i].weight<=nweight){
item[i].fish = true;
num++;
nlen = item[i].len;
nweight = item[i].weight;
}
}
}
printf("%d\n",cnt);
for(i = 0;i<n;i++){
item[i].fish = false;
}
}
return 0;
//fclose(stdin);
}
HDOJ.1051 Wooden Sticks (贪心)的更多相关文章
- HDOJ 1051. Wooden Sticks 贪心 结构体排序
Wooden Sticks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- HDU 1051 Wooden Sticks (贪心)
Wooden Sticks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- HDU 1051 Wooden Sticks 贪心||DP
Wooden Sticks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- HDU - 1051 Wooden Sticks 贪心 动态规划
Wooden Sticks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDOJ 1051. Wooden Sticks
题目 There is a pile of n wooden sticks. The length and weight of each stick are known in advance. The ...
- HDU 1051 Wooden Sticks 贪心题解
本题一看就知道是最长不减序列了,一想就以为是使用dp攻克了. 只是那是个错误的思路. 我就动了半天没动出来.然后看了看别人是能够使用dp的,只是那个比較难证明其正确性,而其速度也不快.故此并非非常好的 ...
- hdu 1051 wooden sticks (贪心+巧妙转化)
#include <iostream>#include<stdio.h>#include<cmath>#include<algorithm>using ...
- 1270: Wooden Sticks [贪心]
点击打开链接 1270: Wooden Sticks [贪心] 时间限制: 1 Sec 内存限制: 128 MB 提交: 31 解决: 11 统计 题目描述 Lialosiu要制作木棍,给n根作为原料 ...
- hdu 1051:Wooden Sticks(水题,贪心)
Wooden Sticks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
随机推荐
- hdu1050Moving Tables(贪心)
Moving Tables Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- P/Invoke 光标的操作
获取与设置光标在屏幕上的位置 GetCursorPos 获取光标在屏幕上的位置,光标位置始终是在屏幕坐标纵指定的,并且不受包含光标的窗口映射模式的影响 函数原型: BOOL GetCursorPos( ...
- Python 更换国内pip源
pip国内的一些镜像: 阿里云 http://mirrors.aliyun.com/pypi/simple/ 中国科技大学 https://pypi.mirrors.ustc.edu.cn/sim ...
- 即刻开始使用Kotlin开发Android的12个原因(KAD 30)
作者:Antonio Leiva 时间:Jul, 11, 2017 原文链接:https://antonioleiva.com/reasons-kotlin-android/ 这组文章已到最后了,它们 ...
- 接口测试工具postman(七)下载文件接口
按照一般请求接口,配置好接口地址以及参数,点击Send and Download 按钮,执行请求的同时会下载文件
- lesson 24 A skeleton in the cupboard
lesson 24 A skeleton in the cupboard conceal sth from sb 对某人隐藏某事 He conceals his girlfriend from his ...
- hibernate.hbm2ddl.auto=update不能自动生成表结构
在写上篇文章<spring整合springmvc和hibernate>的时候,曾遇到一个问题 INFO: Server startup in 8102 ms Hibernate: inse ...
- (Python爬虫04)了解通用爬虫和聚焦爬虫,还是理论知识.快速入门可以略过的
如果现在的你返回N年前去重新学习一门技能,你会咋做? 我会这么干: ...哦,原来这个本事学完可以成为恋爱大神啊, 我要掌握精髓需要这么几个要点一二三四..... 具体的学习步骤是这样的一二三.... ...
- 41. Maximum Subarray
Description Given an array of integers, find a contiguous subarray which has the largest sum. The su ...
- how to install pygraphviz on windows 10 with python 3.6
Here's what worked for me: Win 7 AMD64 Install MSFT C++ compiler. Install Anaconda for Win AMD64, Py ...