HDU 1051 wooden sticks greedy

Source: Internet
Author: User

Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1051

Question:

Given the length and weight of some wood sticks, the installation time of the first wood stick is 1 minute. If the length and weight of the last wood stick do not exceed the length and weight of the next wood stick, therefore, the installation time is not required. Otherwise, it will take 1 minute.

Find the minimum installation time.

For example: (), and)

It takes only 2 minutes. (The first root takes 1 minute, and then () It takes 1 minute to go)



Ideas:

I tried DP. I knew it was greedy at first glance. I don't know who is using DP after half a day. Qaq

If you are not talking about it, find it back when selecting a selection until you cannot find the uninstalled wood stick.

Complexity O (N ^ 2)



#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int MAXN = 5000;struct wooden{int L, W;bool operator <(const wooden& x)const{if (L == x.L)return W < x.W;return L < x.L;}}a[MAXN];int main(){int T;scanf("%d", &T);while (T--){int n;scanf("%d", &n);for (int i = 0; i < n; i++)scanf("%d%d", &a[i].L, &a[i].W);sort(a, a + n);bool used[MAXN] = { 0 };int ans = 1;for (int i = 0; i < n; i++){if (used[i])continue;used[i] = true;int L=a[i].L, W=a[i].W;for (int j = i + 1; j < n; j++){if (used[j])continue;if (a[j].W >= W && a[j].L >= L){used[j] = true;L = a[j].L;W = a[j].W;}}bool find = false;for (int j = i + 1; j < n; j++){if (!used[j]){find = true;break;}}if (!find)break;ans++;}printf("%d\n", ans);}return 0;}


HDU 1051 wooden sticks greedy

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.