zoukankan      html  css  js  c++  java
  • Milking Order II

    题目描述

    Farmer John's N cows (2≤N≤100), conveniently numbered 1…N as always, happen to have too much time on their hooves. As a result, they have worked out a complex social structure related to the order in which Farmer John milks them every morning. After weeks of study, Farmer John has discovered that this structure is based on two key properties.
    First, due to the cows' social hierarchy, some cows insist on being milked before other cows, based on the social status level of each cow. For example, if cow 3 has the highest status, cow 2 has average status, and cow 5 has low status, then cow 3 would need to be milked earliest, followed later by cow 2 and finally by cow 5.

    Second, some cows only allow themselves to be milked at a certain position within the ordering. For example, cow 4 might insist on being milked second among all the cows.

    Luckily, Farmer John will always be able to milk his cows in an order satisfying all of these conditions.

    Unfortunately, cow 1 has recently fallen ill, so Farmer John wants to milk this cow as early in the order as possible so that she can return to the barn and get some much-needed rest. Please help Farmer John determine the earliest position cow 1 can appear in the milking order.

    输入

    The first line contains N, M (1≤M<N), and K (1≤K<N), indicating that Farmer John has N cows, M of his cows have arranged themselves into a social hierarchy, and K of his cows demand that they be milked in a specific position in the order. The next line contains M distinct integers mimi (1≤mi≤N). The cows present on this line must be milked in the same order in which they appear in this line. The next K lines contain two integers ci (1≤ci≤N) and pi (1≤pi≤N), indicating that cow cici must be milked in position pi.
    It is guaranteed that under these constraints, Farmer John will be able to construct a valid milking order.

    输出

    Please output the earliest position cow 1 can take in the milking order。
     
    #include <bits/stdc++.h>
    #define ll long long
    using namespace std;
    int n,m,k;
    int a[205];
    struct node
    {
        int x,p;
    }b[205];
    map<int,int>mp,vis;
    bool check(int p)
    {
        for(int i=0;i<k;i++)
        {
            vis[b[i].x]=b[i].p;
            mp[b[i].p]=b[i].x;
        }
        if(mp[p])
            return false;
        mp[p]=1;
        vis[1]=p;
        int q=1,e=1;
        while(e<=n&&q<=m)
        {
            if(!vis[a[q]])
            {
                if(!mp[e])
                {
                    mp[e]=a[q];
                    vis[a[q]]=e;
                    q++;
                    e++;
                }
                else
                {
                    int tt=0;
                    for(int j=1;j<=m;j++)
                    {
                        if(a[j]==mp[e])
                        {
                            tt=j;
                            break;
                        }
                    }
                    if(tt>q&&tt)
                    {
                        return false;
                    }
                    e++;
                }
            }
            else
            {
                int tt=0;
                for(int i=1;i<=m;i++)
                {
                    if(a[i]==mp[e])
                    {
                        tt=i;
                        break;
                    }
                }
                if(tt&&tt>q)
                    return false;
                if(e<=vis[a[q]])
                {
                    e=vis[a[q]]+1;
                }
                else
                    return false;
                q++;
            }
        }
        //cout<<e<<" "<<m<<endl;
        if(e<=m)
            return 0;
        return 1;
    }
    int main()
    {
        scanf("%d %d %d",&n,&m,&k);
        for(int i=1;i<=m;i++)
        {
            scanf("%d",&a[i]);
        }
        for(int i=0;i<k;i++)
        {
            int x,p;
            scanf("%d %d",&x,&p);
            b[i].x=x;
            b[i].p=p;
        }
        if(vis[1])
        {
            printf("%d",vis[1]);
            return 0;
        }
        for(int i=1;i<=n;i++)
        {
            mp.clear();
            vis.clear();
            if(check(i))
            {
                printf("%d
    ",i);
                return 0;
            }
        }
        return 0;
    }
  • 相关阅读:
    剑指offer题解(python版)(更新到第16题)
    Java基础知识详解:值传递
    [LeetCode] 583. Delete Operation for Two Strings
    [LeetCode] 856. Score of Parentheses
    [LeetCode] 1129. Shortest Path with Alternating Colors
    [LeetCode] 1561. Maximum Number of Coins You Can Get
    [LeetCode] 1052. Grumpy Bookstore Owner
    [LeetCode] 991. Broken Calculator
    [LeetCode] 1054. Distant Barcodes
    [LeetCode] 1245. Tree Diameter
  • 原文地址:https://www.cnblogs.com/Diliiiii/p/10279551.html
Copyright © 2011-2022 走看看