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