HDU 1051 wooden sticks greedy question

Source: Internet
Author: User

At first glance, I knew it was the longest sequence without subtraction. I thought it was solved by using DP. But that is a wrong idea.

I just moved for a long time. Then I looked at other people who could use DP, but it was hard to prove its correctness, and its speed was not fast. Therefore, it is not a good solution.

So I directly calculate it, simulate and select the non-subtraction subsequence, and the answer is displayed after the selection.

Ideas:

1. sort by length

2. Select the weight based on the principle of no reduction. Select one and eliminate one.

After the final elimination, the processing is complete, and the answer will come out naturally.

It turned out to be a simulated question with a DP coat! A simple question disguised as a difficult problem.

The code can also be very concise:

# Include <stdio. h> # include <vector> # include <string. h> # include <algorithm> # include <iostream> # include <string> # include <limits. h> # include <stack> # include <queue> # include <set> # include <map> using namespace STD; const int max_n = 5001; pair <int, int> arr [max_n]; int calincrese () {int ans = 0; sort (ARR, arr + n); For (INT I = 0; I <N; I ++) {If (ARR [I]. second = int_min) continue; int Wei = arr [I]. second; ans ++; For (Int J = I + 1; j <n; j ++) {If (ARR [J]. second> = Wei) Wei = arr [J]. second, arr [J]. second = int_min;} return ans;} int main () {int t; scanf ("% d", & T); While (t --) {scanf ("% d", & N); For (INT I = 0; I <n; I ++) {scanf ("% d ", & amp; arr [I]. first, & arr [I]. second);} printf ("% d \ n", calincrese ();} return 0 ;}



HDU 1051 wooden sticks greedy question

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.