• HDU 3974 Assign the task 简单搜索


    根据Rex 的思路才知道可以这么写。

    题目意思还是很好理解的,就是找到当前雇员最近的任务。

    做法是,可以开辟一个 tim 变量,每次有雇员得到昕任务时候 ++tim

    然后取寻找最近的任务的时候写一个搜索就可以

    核心代码:

                    while(num != -1){
                        num = a[num].leader;
                        if(ttime < a[num].time){
                            ans = a[num].work;
                            ttime = a[num].time;
                        }
                    }
    

    Source code:

    //#pragma comment(linker, "/STACK:16777216") //for c++ Compiler
    #include <stdio.h>
    #include <iostream>
    #include <cstring>
    #include <cmath>
    #include <stack>
    #include <queue>
    #include <vector>
    #include <algorithm>
    #define ll long long
    #define Max(a,b) (((a) > (b)) ? (a) : (b))
    #define Min(a,b) (((a) < (b)) ? (a) : (b))
    #define Abs(x) (((x) > 0) ? (x) : (-(x)))
    
    using namespace std;
    
    const int INF = 0x3f3f3f3f;
    struct sc{
        int work, leader, time;
    }a[50001];
    
    int main(){
        std::ios::sync_with_stdio(false);
        int i, j, k, t, n, m, u, v, num, tt, caseNum, ttime;
        char cmd;
        caseNum = 0;
        cin >> t;
        while(t--){
            int tim = 0;
            for(i = 1; i <= 50000; ++i){
                a[i].work = -1;
                a[i].time = 0;
                a[i].leader = -1;
            }
            cin >> n;
            for(i = 1; i < n; ++i){
                cin >> u >> v;
                a[u].leader = v;
            }
            cout << "Case #" << ++caseNum << ":" << endl;
            cin >> m;
            while(m--){
                cin >> cmd;
                if(cmd == 'C'){
                    cin >> num;
                    ttime = a[num].time;
                    int ans = a[num].work;
                    while(num != -1){
                        num = a[num].leader;
                        if(ttime < a[num].time){
                            ans = a[num].work;
                            ttime = a[num].time;
                        }
                    }
                    cout << ans << endl;
                } else{
                    cin >> num >> tt;
                    a[num].work = tt;
                    a[num].time = ++tim;
                }
            }
        }
        return 0;
    }
  • 相关阅读:
    吴恩达 机器学习EX1学习笔记 MATLAB实现
    二分法解具有单调性的方程
    利用new定位运算符进行高效的数组动态增扩
    单循环链表基本操作及部分可能出现的细节问题
    数组中某元素的删除
    C# 实现可克隆(ICloneable)的类型
    Python学习十三
    Python学习十二
    Python学习十一
    Python学习十
  • 原文地址:https://www.cnblogs.com/wushuaiyi/p/4163048.html
Copyright © 2020-2023  润新知