zoj1025 Wooden Sticks

Source: Internet
Author: User
Tags cmath

Dag transfer, from the point of view is a water problem, give you n rods, large can continue small, ask the number of the minimum ascending subsequence.

In fact, the problem is to write with greed, because this is a non-circular map, to reach the fork, each road to the calendar, so each road should be the same treatment, there are two-dimensional restrictions, you can first on one of the dimensions of the order, and then according to the second dimension of the situation to deal with.

For loop:

#include <cstdio>#include<algorithm>#include<cmath>#include<iostream>#include<cstring>using namespacestd;intN;structnode{intU,v;} big[5005];BOOLvis[5005];BOOLCMP (node A,node b) {if(a.u<b.u)return 1; Else if(a.u==b.u&&a.v<B.V)return 1; return 0;}intMain () {//freopen ("Input.txt", "R", stdin);    intCAs; CIN>>CAs;  while(cas--) {cin>>O;  for(intI=0; i<n;i++) {cin>>big[i].u>>big[i].v; } sort (Big,big+n,cmp); intans=0; intt=0; memset (Vis,0,sizeof(VIS));  for(intI=0; i<n;i++)       {            if(Vis[i])Continue; Vis[i]=1; T=big[i].v; Ans++;  for(intj=i+1; j<n;j++)            {                if(big[j].v>=t&&vis[j]==0) {T=big[j].v; VIS[J]=1; }}} printf ("%d\n", ans); }}

Memory of Dfs greedy:

#include <cstdio>#include<algorithm>#include<cmath>#include<iostream>#include<cstring>using namespacestd;intN;structnode{intU,v;} big[5005];BOOLvis[5005];BOOLCMP (node A,node b) {if(a.u<b.u)return 1; Else if(a.u==b.u&&a.v<B.V)return 1; return 0;}voidDfsintx) {    if(Vis[x])return; VIS[X]=1;  for(intj=x+1; j<n;j++)    {        if(vis[j]==0&&big[j].v>=big[x].v)            {DFS (j);  Break;//when the memory of greed to add break;        }    }}intMain () {//freopen ("Input.txt", "R", stdin);    intCAs; CIN>>CAs;  while(cas--) {cin>>N;  for(intI=0; i<n;i++) {cin>>big[i].u>>big[i].v; } sort (Big,big+n,cmp); intans=0; memset (Vis,0,sizeof(VIS));  for(intI=0; i<n;i++)       {            if(!Vis[i])           {DFS (i); Ans++; }} printf ("%d\n", ans); }} 

zoj1025 Wooden Sticks

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.