HDU 3974 Assign the task 简单搜索

时间:2023-03-08 16:28:09

根据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[]; int main(){
std::ios::sync_with_stdio(false);
int i, j, k, t, n, m, u, v, num, tt, caseNum, ttime;
char cmd;
caseNum = ;
cin >> t;
while(t--){
int tim = ;
for(i = ; i <= ; ++i){
a[i].work = -;
a[i].time = ;
a[i].leader = -;
}
cin >> n;
for(i = ; 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 != -){
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 ;
}