【CCF历年题题解】201709-2 公共钥匙盒【模拟】

定义一个结构<时间,类型,钥匙号>。
先将输入转换为这种结构的元素,再排序后依次处理即可

#include <iostream>
#include <algorithm>

using namespace std;

const int N = 1010;

typedef pair<int,pair<int,int>> PIII; // <时间,<类型,几号钥匙>> 还:0,取1

int n,k;
vector<PIII> list;
int st[N]; // 定义为int

int main()
{
    
    
    cin >> n >> k;
    for(int i=1;i<=n;i++) st[i] = i;
    
    while(k -- )
    {
    
    
        int w,s,c;
        cin >> w >> s >> c; // 钥匙,开始时间,持续时间
        list.push_back({
    
    s,{
    
    1,w}});
        list.push_back({
    
    s + c,{
    
    0,w}});
    }
    
    sort(list.begin(),list.end());
    
    for(auto c : list)
    {
    
    
        int time = c.first, type = c.second.first, num = c.second.second;
        
        if(type == 0){
    
     // 还
            for(int i = 1;i<=n;i++)
                if(!st[i]) {
    
    
                    st[i] = num;
                    break;
                }
        }
        else{
    
     // 取,取的方式不对
            for(int i =1;i<=n;i++) 
                if(st[i] == num){
    
    
                    st[i] = 0;
                    break;
                }
        }
        
        //printf("%d %d %d\n",time,type,num);
       
    //   for(int i=1;i<=n;i++) cout << st[i] << " ";
    //   cout << endl;
        
    }
    
    for(int i=1;i<=n;i++) cout << st[i] << " ";
    
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43154149/article/details/108541540