题目描述:

AC源码:

此题考查贪心算法,解题思路:首先使用快速排序,以w或l按升序排序(注意相等时,应按另一值升序排序),这样就将二维变量比较,变为了一维的,排好序的一边就不需要去管了,只需要对未排序的一边直接进行贪心遍历。时间复杂度O(n^2)

#include"iostream"
#include"algorithm"
using namespace std; struct Stick
{
int l;
int w;
bool processed;
}; bool cmp(Stick a, Stick b)
{ if(a.l < b.l)
{
return true;
}
else if(a.l == b.l)
{
return a.w < b.w;
}
else
{
return false;
}
} int main()
{
int t, n, total, val;
Stick s[5000];
scanf("%d", &t);
for(int i = 0; i < t; i++)
{
scanf("%d", &n);
total = 0;
for(int j = 0; j < n; j++)
{
scanf("%d %d", &s[j].l, &s[j].w);
s[j].processed = false;
}
sort(s, s+n, cmp);
for(int j = 0; j < n; j++)
{
if(!s[j].processed)
{
total++;
val = s[j].w;
s[j].processed = true;
for(int k = j + 1; k < n; k++)
{
if(!s[k].processed && s[k].w >= val)
{
val = s[k].w;
s[k].processed = true;
}
}
}
}
printf("%d\n", total);
}
return 0;
}

  

HD-ACM算法专攻系列(21)——Wooden Sticks的更多相关文章

  1. HD-ACM算法专攻系列(23)——Crixalis's Equipment

    题目描述: AC源码:此次考察贪心算法,解题思路:贪心的原则是使留下的空间最大,优先选择Bi与Ai差值最大的,至于为什么?这里用只有2个设备为例,(A1,B1)与(A2,B2),假设先搬运A1,搬运的 ...

  2. HD-ACM算法专攻系列(22)——Max Sum

    问题描述: AC源码: 此题考察动态规划,解题思路:遍历(但有技巧),在于当前i各之和为负数时,直接选择以第i+1个为开头,在于当前i各之和为正数时,第i个可以不用作为开头(因为前i+1个之和一定大于 ...

  3. HD-ACM算法专攻系列(20)——七夕节

    问题描述: AC源码: /**/ #include"iostream" #include"cmath" using namespace std; int mai ...

  4. HD-ACM算法专攻系列(19)——Leftmost Digit

    问题描述: AC源码: 解题关键是,数据很大,不能强算,需要使用技巧,这里使用科学计算法,令N^N=a*10^n ,取对数后变为 N*log10(N)=log10(a)+n,令x = log10(a) ...

  5. HD-ACM算法专攻系列(18)——Largest prime factor

    题目描述: 源码: 需要注意,若使用cin,cout输入输出,会超时. #include"iostream" #include"memory.h" #defin ...

  6. HD-ACM算法专攻系列(17)——find your present (2)

    题目描述: 源码: #include"iostream" #include"string" using namespace std; bool IsFirstH ...

  7. HD-ACM算法专攻系列(16)——考试排名

    问题描述: 源码: 主要要注意输出格式. #include"iostream" #include"iomanip" #include"algorith ...

  8. HD-ACM算法专攻系列(15)——Quoit Design

    问题描述: 源码: 经典问题——最近邻问题,标准解法 #include"iostream" #include"algorithm" #include" ...

  9. HD-ACM算法专攻系列(14)——find your present (2)

    问题描述: 源码: #include"iostream" #include"algorithm" using namespace std; bool cmp(i ...

随机推荐

  1. java题(转载)

    1.下面中哪两个可以在A的子类中使用:( ) class A { protected int method1 (int a, int b) { return 0; } } A. public int ...

  2. C#监测方法执行效率

    System.Diagnostics.Stopwatch watch = new Stopwatch(); watch.Start(); // 开始监视代码运行时间 //需要监测的代码 dothing ...

  3. Walking on the path of Redis --- Introduction and Installation

    废话开篇 以前从来没听说过有Redis这么个玩意,无意间看到一位仁兄的博客,才对其有所了解,所以决定对其深入了解下.有不对的地方还请各位指正. Redis介绍 下面是官方的介绍,不喜欢english的 ...

  4. java keytool证书工具使用小结(转载)

    原文地址:http://www.micmiu.com/lang/java/keytool-start-guide/ Keytool 是一个Java数据证书的管理工具 ,Keytool将密钥(key)和 ...

  5. Ubuntu win8 小设备版本

    Ubuntu小设备支持列表:https://wiki.ubuntu.com/Touch/Devices win8 小设备 刷ubutntu:tieba.baidu.com/p/2772275438

  6. MVC 先后顺序

    @foreach (var item in Model) { if (ViewBag.GetModel.ParentID == item.DictID) { <option value=&quo ...

  7. 死磕itchat源码--目录结构

    阅读itchat源码时,先弄清itchat的目录结构 itchat │ config.py │ content.py │ core.py │ log.py │ returnvalues.py │ ut ...

  8. Codeforces Round #468 (Div. 2, based on Technocup 2018 Final Round)D. Peculiar apple-tree

    In Arcady's garden there grows a peculiar apple-tree that fruits one time per year. Its peculiarity ...

  9. iOS tcpdump抓包方法(需越狱)

    前提条件:机器要破解,cydia能打开 需要工具1.openssh2.tcpdump 安装工具方法:1.连接网络,打开cydia2.确认Cydia设置为开发者模式(管理->设置->开发者) ...

  10. ZOJ 2316 Matrix Multiplication

    Matrix Multiplication Time Limit: 2000ms Memory Limit: 32768KB This problem will be judged on ZJU. O ...