zoukankan      html  css  js  c++  java
  • csuoj-1010-Water Drinking

    题目:

    Description

    The Happy Desert is full of sands. There is only a kind of animal called camel living on the Happy Desert. Cause they live here, they need water here. Fortunately, they find a pond which is full of water in the east corner of the desert. Though small, but enough. However, they still need to stand in lines to drink the water in the pond.

    Now we mark the pond with number 0, and each of the camels with a specific number, starting from 1. And we use a pair of number to show the two adjacent camels, in which the former number is closer to the pond. There may be more than one lines starting from the pond and ending in a certain camel, but each line is always straight and has no forks.

    Input

    There’re multiple test cases. In each case, there is a number N (1≤N≤100000) followed by N lines of number pairs presenting the proximate two camels. There are 99999 camels at most.

    Output

    For each test case, output the camel’s number who is standing in the last position of its line but is closest to the pond. If there are more than one answer, output the one with the minimal number.

    Sample Input

    1
    0 1
    5
    0 2
    0 1
    1 4
    2 3
    3 5
    5
    1 3
    0 2
    0 1
    0 4
    4 5
    

    Sample Output

    1
    4
    2
    分析:
    1,每个首尾节点保存所在链表的第一个节点和最后一个节点的下标,链表的长度,骆驼的标号;
    2,合并a和b所在的链表。
    代码:
    #include<iostream>
    #include<algorithm>
    using namespace std;
    struct Camel{
        int cnt;
        int flag;
        int last;
        int first;
    }camel[100005];
    int cmp(const struct Camel &a,const struct Camel &b){
        if(a.flag > b.flag) return 1;
        else if(a.flag == b.flag){
            if(a.cnt < b.cnt) return 1;
            else if(a.cnt == b.cnt){
                if(a.last < b.last)
                    return 1;
                else return 0;
            }
            else return 0;
        }
        else return 0;
    }
    
    int main(){
        int n;
        cin.sync_with_stdio(false);
        while(cin >> n){
            for(int i = 1;i <= n;i++){
                camel[i].cnt = 1;
                camel[i].flag = 0;
                camel[i].last = camel[i].first = i;
            }
            int a,b;
            for(int i = 0;i < n;i++){
                cin >> a >> b;
                if(a == 0){
                    camel[b].flag = 1;
                }
                else{
                    camel[camel[a].first].cnt += camel[b].cnt;
                    camel[camel[b].last].cnt = camel[camel[a].first].cnt;
                    camel[camel[a].first].last = camel[b].last;
                    camel[camel[b].last].first = camel[a].first;
                }
            }
            sort(camel + 1,camel + n + 1,cmp);
    //        for(int i = 1;i <= n;i++){
    //            cout << i << " " << camel[i].first << " " << camel[i].last << " " << camel[i].cnt << " " << camel[i].flag << endl;
    //        }
            cout << camel[1].last << endl;
        }
        return 0;
    }

  • 相关阅读:
    吴裕雄--天生自然 诗经:天净沙·秋思
    阿里云Kubernetes服务
    一探究竟:善用 MaxCompute Studio 分析 SQL 作业
    MaxCompute Studio 使用入门
    AI 一体机,满足新时代的新需求
    OSS重磅推出OSS Select——使用SQL选取文件的内容
    如何将DynamoDB的数据增量迁移到表格存储
    多隆:淘宝第一行代码撰写者的程序世界
    专访阿里巴巴量子实验室:最强量子电路模拟器“太章”到底强在哪?
    饿了么CTO张雪峰:允许90后的技术人员“浮躁“一点
  • 原文地址:https://www.cnblogs.com/tracy520/p/6690977.html
Copyright © 2011-2022 走看看