公共鑰匙盒20170902

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
struct br
{
    int num,begin,end;
} b[1010];
int a[1010];
bool cmpb(const br &a,const br &b)
{
    return a.begin<b.begin;
}
bool cmpe(const br &a,const br &b)
{
    return a.end==b.end?a.num<b.num:a.end<b.end;
}//定義比較函數
int main()
{
    
    int n,k;
    cin>>n>>k;
    for(int i=0; i<k; i++)
    {
        int x;
        cin>>b[i].num>>b[i].begin>>x;
        b[i].end=x+b[i].begin;
    }
    for(int i=0; i<n; i++)
        a[i]=i+1;
    sort(b,b+k,cmpb);
    int time1=b[0].begin;
    sort(b,b+k,cmpe);
    int time2=b[k-1].end;
    for(int t=time1; t<=time2; t++)
    {

        for(int i=0; i<k; i++)
        {
            if(b[i].end==t)
            {
                for(int j=0; j<n; j++)
                    if(a[j]==0)
                    {
                        a[j]=b[i].num;
                        break;
                    }
            } //還鑰匙
            if(b[i].begin==t)
            {
                for(int j=0; j<n; j++)
                if(a[j]==b[i].num)  a[j]=0;
            }//取鑰匙
        }
    }

    for(int i=0; i<n; i++)
        cout<<a[i]<<" ";
    cout<<endl;
    return 0;
}

模擬題:思路是先找出最大最小時間,對時間遍歷,對取鑰匙還鑰匙進行操作,足夠

細心就不難

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章