C++编程 hdu 1065(贪心题目)

2024-02-29 04:08
文章标签 c++ 题目 编程 贪心 hdu 1065

本文主要是介绍C++编程 hdu 1065(贪心题目),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Wooden Sticks

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 7   Accepted Submission(s) : 3
Font: Times New Roman | Verdana | Georgia
Font Size:  

Problem Description

There is a pile of n wooden sticks. The length and weight of each stick are known in advance. The sticks are to be processed by a woodworking machine in one by one fashion. It needs some time, called setup time, for the machine to prepare processing a stick. The setup times are associated with cleaning operations and changing tools and shapes in the machine. The setup times of the woodworking machine are given as follows: 

(a) The setup time for the first wooden stick is 1 minute. 
(b) Right after processing a stick of length l and weight w , the machine will need no setup time for a stick of length l' and weight w' if l<=l' and w<=w'. Otherwise, it will need 1 minute for setup. 

You are to find the minimum setup time to process a given pile of n wooden sticks. For example, if you have five sticks whose pairs of length and weight are (4,9), (5,2), (2,1), (3,5), and (1,4), then the minimum setup time should be 2 minutes since there is a sequence of pairs (1,4), (3,5), (4,9), (2,1), (5,2).

Input

The input consists of T test cases. The number of test cases (T) is given in the first line of the input file. Each test case consists of two lines: The first line has an integer n , 1<=n<=5000, that represents the number of wooden sticks in the test case, and the second line contains n 2 positive integers l1, w1, l2, w2, ..., ln, wn, each of magnitude at most 10000 , where li and wi are the length and weight of the i th wooden stick, respectively. The 2n integers are delimited by one or more spaces.

Output

The output should contain the minimum setup time in minutes, one per line.

Sample Input

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

Sample Output

2
1
3

Source


#include <iostream>
using namespace std;
int n;
struct wooden
{int l;int w;int no;
};wooden wood[5001];
void init()
{int i;for(i = 0; i < 5001; i ++){wood[i].l = 0;wood[i].w = 0;wood[i].no = -1;}}bool compare(int i, int j)
{if(wood[i].l >= wood[j].l && wood[i].w >= wood[j].w)return true;else if(wood[i].l <= wood[j].l && wood[i].w <= wood[j].w)return true;elsereturn false;
}void check()
{int i;for(i = 0; i < n; i++){cout << "(" << wood[i].l <<","<< wood[i].w <<"," << wood[i].no <<") ";}
}//int doit()
//{
//    int i;
//    int m = 1;
//    for(i = 0; i < n - 1; i++)
//    {
//        if(compare(i, i + 1))
//        {
//            m ++;
//        }
//    }
//    return m;
//}int main()
{int t, cur, w;int i, j;cin >> t;while(t--){cin >> n;init();for(i = 0; i < n; i++)cin >> wood[i].l >> wood[i].w;for(i = 0; i < n; i ++){for(j = 0; j < n - i - 1; j++){if(wood[j].l > wood[j+1].l){swap(wood[j], wood[j+1]);}}}//        for(i = 0; i < n; i ++)
//        {
//            for(j = 0; j < n - i - 1; j++)
//            {
//                if(wood[j].w > wood[j+1].w)
//                    swap(wood[j], wood[j+1]);
//            }
//
//        }
//        }cur = 0;for(i = 0; i < n; i++){if(wood[i].no == -1){cur ++;w = wood[i].w;wood[i].no = cur;for(j = i; j < n; j++){if(wood[j].no == -1 && w <= wood[j].w){wood[j].no = cur;w = wood[j].w;}}}
//            check();
//                cout << endl;}cout << cur << endl;}return 0;
}

大水题。。但是做的时候出了问题 = =。一开始的想法是根据l排一次序,再根据w排一次序。。这是不行的,原因如下。。
可能出现这种情况(7,8),(8, 7),(9,10),(10,9),(11,11)
类似于如上情况就行不通了。。doit()注释掉的就是之前错的算法= =。。看别人说的分集合恍然大悟。。遂写成。。唉,这下惨了。。水题做成这个样子。。



这篇关于C++编程 hdu 1065(贪心题目)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/757533

相关文章

C++一个数组赋值给另一个数组方式

《C++一个数组赋值给另一个数组方式》文章介绍了三种在C++中将一个数组赋值给另一个数组的方法:使用循环逐个元素赋值、使用标准库函数std::copy或std::memcpy以及使用标准库容器,每种方... 目录C++一个数组赋值给另一个数组循环遍历赋值使用标准库中的函数 std::copy 或 std::

C++使用栈实现括号匹配的代码详解

《C++使用栈实现括号匹配的代码详解》在编程中,括号匹配是一个常见问题,尤其是在处理数学表达式、编译器解析等任务时,栈是一种非常适合处理此类问题的数据结构,能够精确地管理括号的匹配问题,本文将通过C+... 目录引言问题描述代码讲解代码解析栈的状态表示测试总结引言在编程中,括号匹配是一个常见问题,尤其是在

使用C++实现链表元素的反转

《使用C++实现链表元素的反转》反转链表是链表操作中一个经典的问题,也是面试中常见的考题,本文将从思路到实现一步步地讲解如何实现链表的反转,帮助初学者理解这一操作,我们将使用C++代码演示具体实现,同... 目录问题定义思路分析代码实现带头节点的链表代码讲解其他实现方式时间和空间复杂度分析总结问题定义给定

C++初始化数组的几种常见方法(简单易懂)

《C++初始化数组的几种常见方法(简单易懂)》本文介绍了C++中数组的初始化方法,包括一维数组和二维数组的初始化,以及用new动态初始化数组,在C++11及以上版本中,还提供了使用std::array... 目录1、初始化一维数组1.1、使用列表初始化(推荐方式)1.2、初始化部分列表1.3、使用std::

C++ Primer 多维数组的使用

《C++Primer多维数组的使用》本文主要介绍了多维数组在C++语言中的定义、初始化、下标引用以及使用范围for语句处理多维数组的方法,具有一定的参考价值,感兴趣的可以了解一下... 目录多维数组多维数组的初始化多维数组的下标引用使用范围for语句处理多维数组指针和多维数组多维数组严格来说,C++语言没

c++中std::placeholders的使用方法

《c++中std::placeholders的使用方法》std::placeholders是C++标准库中的一个工具,用于在函数对象绑定时创建占位符,本文就来详细的介绍一下,具有一定的参考价值,感兴... 目录1. 基本概念2. 使用场景3. 示例示例 1:部分参数绑定示例 2:参数重排序4. 注意事项5.

使用C++将处理后的信号保存为PNG和TIFF格式

《使用C++将处理后的信号保存为PNG和TIFF格式》在信号处理领域,我们常常需要将处理结果以图像的形式保存下来,方便后续分析和展示,C++提供了多种库来处理图像数据,本文将介绍如何使用stb_ima... 目录1. PNG格式保存使用stb_imagephp_write库1.1 安装和包含库1.2 代码解

C++实现封装的顺序表的操作与实践

《C++实现封装的顺序表的操作与实践》在程序设计中,顺序表是一种常见的线性数据结构,通常用于存储具有固定顺序的元素,与链表不同,顺序表中的元素是连续存储的,因此访问速度较快,但插入和删除操作的效率可能... 目录一、顺序表的基本概念二、顺序表类的设计1. 顺序表类的成员变量2. 构造函数和析构函数三、顺序表

使用C++实现单链表的操作与实践

《使用C++实现单链表的操作与实践》在程序设计中,链表是一种常见的数据结构,特别是在动态数据管理、频繁插入和删除元素的场景中,链表相比于数组,具有更高的灵活性和高效性,尤其是在需要频繁修改数据结构的应... 目录一、单链表的基本概念二、单链表类的设计1. 节点的定义2. 链表的类定义三、单链表的操作实现四、

C#多线程编程中导致死锁的常见陷阱和避免方法

《C#多线程编程中导致死锁的常见陷阱和避免方法》在C#多线程编程中,死锁(Deadlock)是一种常见的、令人头疼的错误,死锁通常发生在多个线程试图获取多个资源的锁时,导致相互等待对方释放资源,最终形... 目录引言1. 什么是死锁?死锁的典型条件:2. 导致死锁的常见原因2.1 锁的顺序问题错误示例:不同