心急的C小加 贪心算法
2024-09-25 05:53:02
心急的C小加
时间限制:1000 ms | 内存限制:65535 KB
难度:4
- 描述
-
C小加有一些木棒,它们的长度和质量都已经知道,需要一个机器处理这些木棒,机器开启的时候需要耗费一个单位的时间,如果第i+1个木棒的重量和长度都大于等于第i个处理的木棒,那么将不会耗费时间,否则需要消耗一个单位的时间。因为急着去约会,C小加想在最短的时间内把木棒处理完,你能告诉他应该怎样做吗?
- 输入
- 第一行是一个整数T(1<T<1500),表示输入数据一共有T组。
每组测试数据的第一行是一个整数N(1<=N<=5000),表示有N个木棒。接下来的一行分别输入N个木棒的L,W(0 < L ,W <= 10000),用一个空格隔开,分别表示木棒的长度和质量。 - 输出
- 处理这些木棒的最短时间。
- 样例输入
-
3
5
4 9 5 2 2 1 3 5 1 4
3
2 2 1 1 2 2
3
1 3 2 2 3 1 - 样例输出
-
2
1
3 首先排序,然后在考虑长度的基础上遍历处理掉当前可以处理的并标记,然后继续遍历,遇见没被标记的就cnt++,再次在该点遍历一遍,注意更改当前处理的最大重量#include<iostream>
#include<cstdio>
#include<cstring>
#include<sstream>
#include<algorithm>
#include<queue>
#include<vector>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<fstream>
#include<memory>
#include<list>
#include<string>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define MAXN 5003
#define INF 1000000009
/*
长度重量都大于当前的 那么无花费
否则花费为1
*/
struct node
{
int w, l;
};
bool cmp(node a, node b)
{
if (a.l == b.l) return a.w < b.w;
return a.l < b.l;
}
node a[MAXN];
bool been[MAXN];
int T, n;
int main()
{
scanf("%d", &T);
while (T--)
{
memset(been, false, sizeof(been));
scanf("%d", &n);
for (int i = ; i < n; i++)
scanf("%d%d", &a[i].l, &a[i].w);
sort(a, a + n, cmp);
int cnt = ;
for (int i = ; i < n; i++)
{
if (!been[i])
{
cnt++;
int tmp = a[i].w;
for (int j = i + ; j < n; j++)
{
if (!been[j] && a[j].w >= tmp)
{
been[j] = true;
tmp = a[j].w;
}
}
}
}
printf("%d\n", cnt);
}
return ;
}
最新文章
- PHP清理跨站XSS xss_clean 函数 整理自codeigniter Security
- Linux netstat详解
- BestCoder10 1002 Revenge of GCD(hdu 5019) 解题报告
- 课堂所讲整理:HTML--5JavaScript简介
- CODEVS 1036 商务旅行
- javascipt学习笔记1
- iOS: 获取文件路径
- CSS的力量
- 优步北京B组(8月10日-8月16日奖励规则)
- Spark结构式流编程指南
- [最短路][模版]P1346 电车
- python scipy计算机数值库
- Vmware下centos与windows能ping通并能上网
- Invalid property &#39;driver_class&#39; of bean class
- [UOJ#207. 共价大爷游长沙]——LCT&;随机化
- Selenium自动化 Xpath-元素定位
- bzoj 3238
- BZOJ4381[POI2015]Odwiedziny——分块+长链剖分
- python中的函数对象的内存地址是多少
- MT【45】抛物线外一点作抛物线的切线(尺规作图题)