首页 > 代码库 > HDU 3974 Assign the task 简单搜索

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;}

 

HDU 3974 Assign the task 简单搜索