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++ 中std::map内存管理

《深入解析C++中std::map内存管理》文章详解C++std::map内存管理,指出clear()仅删除元素可能不释放底层内存,建议用swap()与空map交换以彻底释放,针对指针类型需手动de... 目录1️、基本清空std::map2️、使用 swap 彻底释放内存3️、map 中存储指针类型的对象

Python异步编程之await与asyncio基本用法详解

《Python异步编程之await与asyncio基本用法详解》在Python中,await和asyncio是异步编程的核心工具,用于高效处理I/O密集型任务(如网络请求、文件读写、数据库操作等),接... 目录一、核心概念二、使用场景三、基本用法1. 定义协程2. 运行协程3. 并发执行多个任务四、关键

AOP编程的基本概念与idea编辑器的配合体验过程

《AOP编程的基本概念与idea编辑器的配合体验过程》文章简要介绍了AOP基础概念,包括Before/Around通知、PointCut切入点、Advice通知体、JoinPoint连接点等,说明它们... 目录BeforeAroundAdvise — 通知PointCut — 切入点Acpect — 切面

C++ STL-string类底层实现过程

《C++STL-string类底层实现过程》本文实现了一个简易的string类,涵盖动态数组存储、深拷贝机制、迭代器支持、容量调整、字符串修改、运算符重载等功能,模拟标准string核心特性,重点强... 目录实现框架一、默认成员函数1.默认构造函数2.构造函数3.拷贝构造函数(重点)4.赋值运算符重载函数

C++ vector越界问题的完整解决方案

《C++vector越界问题的完整解决方案》在C++开发中,std::vector作为最常用的动态数组容器,其便捷性与性能优势使其成为处理可变长度数据的首选,然而,数组越界访问始终是威胁程序稳定性的... 目录引言一、vector越界的底层原理与危害1.1 越界访问的本质原因1.2 越界访问的实际危害二、基

C#异步编程ConfigureAwait的使用小结

《C#异步编程ConfigureAwait的使用小结》本文介绍了异步编程在GUI和服务器端应用的优势,详细的介绍了async和await的关键作用,通过实例解析了在UI线程正确使用await.Conf... 异步编程是并发的一种形式,它有两大好处:对于面向终端用户的GUI程序,提高了响应能力对于服务器端应

c++日志库log4cplus快速入门小结

《c++日志库log4cplus快速入门小结》文章浏览阅读1.1w次,点赞9次,收藏44次。本文介绍Log4cplus,一种适用于C++的线程安全日志记录API,提供灵活的日志管理和配置控制。文章涵盖... 目录简介日志等级配置文件使用关于初始化使用示例总结参考资料简介log4j 用于Java,log4c

C++归并排序代码实现示例代码

《C++归并排序代码实现示例代码》归并排序将待排序数组分成两个子数组,分别对这两个子数组进行排序,然后将排序好的子数组合并,得到排序后的数组,:本文主要介绍C++归并排序代码实现的相关资料,需要的... 目录1 算法核心思想2 代码实现3 算法时间复杂度1 算法核心思想归并排序是一种高效的排序方式,需要用

C# async await 异步编程实现机制详解

《C#asyncawait异步编程实现机制详解》async/await是C#5.0引入的语法糖,它基于**状态机(StateMachine)**模式实现,将异步方法转换为编译器生成的状态机类,本... 目录一、async/await 异步编程实现机制1.1 核心概念1.2 编译器转换过程1.3 关键组件解析

C++11范围for初始化列表auto decltype详解

《C++11范围for初始化列表autodecltype详解》C++11引入auto类型推导、decltype类型推断、统一列表初始化、范围for循环及智能指针,提升代码简洁性、类型安全与资源管理效... 目录C++11新特性1. 自动类型推导auto1.1 基本语法2. decltype3. 列表初始化3