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++ move 的作用详解及陷阱最佳实践

《C++move的作用详解及陷阱最佳实践》文章详细介绍了C++中的`std::move`函数的作用,包括为什么需要它、它的本质、典型使用场景、以及一些常见陷阱和最佳实践,感兴趣的朋友跟随小编一起看... 目录C++ move 的作用详解一、一句话总结二、为什么需要 move?C++98/03 的痛点⚡C++

详解C++ 存储二进制数据容器的几种方法

《详解C++存储二进制数据容器的几种方法》本文主要介绍了详解C++存储二进制数据容器,包括std::vector、std::array、std::string、std::bitset和std::ve... 目录1.std::vector<uint8_t>(最常用)特点:适用场景:示例:2.std::arra

C++构造函数中explicit详解

《C++构造函数中explicit详解》explicit关键字用于修饰单参数构造函数或可以看作单参数的构造函数,阻止编译器进行隐式类型转换或拷贝初始化,本文就来介绍explicit的使用,感兴趣的可以... 目录1. 什么是explicit2. 隐式转换的问题3.explicit的使用示例基本用法多参数构造

C++,C#,Rust,Go,Java,Python,JavaScript的性能对比全面讲解

《C++,C#,Rust,Go,Java,Python,JavaScript的性能对比全面讲解》:本文主要介绍C++,C#,Rust,Go,Java,Python,JavaScript性能对比全面... 目录编程语言性能对比、核心优势与最佳使用场景性能对比表格C++C#RustGoJavapythonjav

C++打印 vector的几种方法小结

《C++打印vector的几种方法小结》本文介绍了C++中遍历vector的几种方法,包括使用迭代器、auto关键字、typedef、计数器以及C++11引入的范围基础循环,具有一定的参考价值,感兴... 目录1. 使用迭代器2. 使用 auto (C++11) / typedef / type alias

C++ scoped_ptr 和 unique_ptr对比分析

《C++scoped_ptr和unique_ptr对比分析》本文介绍了C++中的`scoped_ptr`和`unique_ptr`,详细比较了它们的特性、使用场景以及现代C++推荐的使用`uni... 目录1. scoped_ptr基本特性主要特点2. unique_ptr基本用法3. 主要区别对比4. u

C++11中的包装器实战案例

《C++11中的包装器实战案例》本文给大家介绍C++11中的包装器实战案例,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录引言1.std::function1.1.什么是std::function1.2.核心用法1.2.1.包装普通函数1.2.

C++多线程开发环境配置方法

《C++多线程开发环境配置方法》文章详细介绍了如何在Windows上安装MinGW-w64和VSCode,并配置环境变量和编译任务,使用VSCode创建一个C++多线程测试项目,并通过配置tasks.... 目录下载安装 MinGW-w64下载安装VS code创建测试项目配置编译任务创建 tasks.js

C++ 多态性实战之何时使用 virtual 和 override的问题解析

《C++多态性实战之何时使用virtual和override的问题解析》在面向对象编程中,多态是一个核心概念,很多开发者在遇到override编译错误时,不清楚是否需要将基类函数声明为virt... 目录C++ 多态性实战:何时使用 virtual 和 override?引言问题场景判断是否需要多态的三个关

C++简单日志系统实现代码示例

《C++简单日志系统实现代码示例》日志系统是成熟软件中的一个重要组成部分,其记录软件的使用和运行行为,方便事后进行故障分析、数据统计等,:本文主要介绍C++简单日志系统实现的相关资料,文中通过代码... 目录前言Util.hppLevel.hppLogMsg.hppFormat.hppSink.hppBuf