zoj1025 Wooden Sticks

DAG转移,从切题的数量来看是一道水题,给你n个棒,大的可以延续小的,问最少上升子序列的个数。

其实这道题是用贪心来写的,因为这是个有向无环图,到达分叉口,每一条路都要便历,所以每条路应该一样对待,有两维限制,可以先对其中一维进行排序,然后根据第二维的情况进行处理。

for循环:

#include<cstdio>
#include<algorithm>
#include<cmath>
#include<iostream>
#include<cstring>
using namespace std;
int n;
struct node
{
    int u,v;
}big[5005];
bool vis[5005];
bool cmp(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;
}
int main()
{
    //freopen("input.txt","r",stdin);
    int cas;
    cin>>cas;
    while(cas--)
    {
       cin>>n;
       for(int i=0;i<n;i++)
       {
            cin>>big[i].u>>big[i].v;
       }
       sort(big,big+n,cmp);
       int ans=0;
       int t=0;
       memset(vis,0,sizeof(vis));
       for(int i=0;i<n;i++)
       {
            if(vis[i])
            continue;
            vis[i]=1;
            t=big[i].v;
            ans++;
            for(int j=i+1;j<n;j++)
            {
                if(big[j].v>=t&&vis[j]==0)
                {
                    t=big[j].v;
                    vis[j]=1;
                }
            }
       }
       printf("%d\n",ans);
    }
}

记忆化dfs贪心:

#include<cstdio>
#include<algorithm>
#include<cmath>
#include<iostream>
#include<cstring>
using namespace std;
int n;
struct node
{
    int u,v;
}big[5005];
bool vis[5005];
bool cmp(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;
}
void dfs(int x)
{
    if(vis[x])
        return;
    vis[x]=1;
    for(int j=x+1;j<n;j++)
    {
        if(vis[j]==0&&big[j].v>=big[x].v)
        {
            dfs(j);
            break;//记忆化贪心的时候要加上break;
        }
    }
}

int main()
{
    //freopen("input.txt","r",stdin);
    int cas;
    cin>>cas;
    while(cas--)
    {
       cin>>n;
       for(int i=0;i<n;i++)
       {
            cin>>big[i].u>>big[i].v;
       }
       sort(big,big+n,cmp);
       int ans=0;
       memset(vis,0,sizeof(vis));
       for(int i=0;i<n;i++)
       {
            if(!vis[i])
         {
           dfs(i);
           ans++;
         }
       }
       printf("%d\n",ans);
    }
} 
时间: 2024-10-16 10:28:16

zoj1025 Wooden Sticks的相关文章

HDU 1051:Wooden Sticks

Wooden Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 11605    Accepted Submission(s): 4792 Problem Description There is a pile of n wooden sticks. The length and weight of each stick a

Wooden Sticks POJ - 1065

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

HDU 1051 Wooden Sticks 贪心||DP

Wooden Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 22000    Accepted Submission(s): 8851 Problem Description There is a pile of n wooden sticks. The length and weight of each stick ar

HDU 1051 Wooden Sticks (贪心)

Wooden Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 11249    Accepted Submission(s): 4629 Problem Description There is a pile of n wooden sticks. The length and weight of each stick a

ZOJ 1025. Wooden Sticks 贪心 结构体排序

Wooden Sticks Time Limit: 2 Seconds      Memory Limit: 65536 KB 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

HDOJ 1051. Wooden Sticks 贪心 结构体排序

Wooden Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 15564    Accepted Submission(s): 6405 Problem Description There is a pile of n wooden sticks. The length and weight of each stick a

(hdu step 1.3.6)Wooden Sticks(求长度和重量都严格递增的数列的个数)

题目: Wooden Sticks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2374 Accepted Submission(s): 921   Problem Description There is a pile of n wooden sticks. The length and weight of each stick are

POJ1065 Wooden Sticks

Wooden Sticks Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 23403   Accepted: 10053 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 woodworki

1051 Wooden Sticks(贪心-3)

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