CCF 2017 09-02 公共钥匙盒

 CCF 2017 09-02 公共钥匙盒

1.用快速排序函数结合排序规则函数来给取放排序。

2.vector数组的强大功能。

 1 #include<iostream>
 2 #include<vector> 
 3 #include<algorithm> 
 4 using namespace std;
 5 struct node{
 6     int num;//钥匙的编号 
 7     int start;//动作开始时间 
 8     int action;//动作的类型 0:放 1:取 
 9     node(){
10     }
11     node(int num,int start,int action):num(num),start(start),action(action){
12     }
13 };
14 
15 bool cmp(node a,node b)
16 { 
17     if(a.start != b.start) return a.start<b.start;///1.按照动作开始时间升序排列 
18     else{
19         ///2.如果时间相同,则先放后取
20         if(a.action != b.action) return a.action < b.action;
21         else{
22         ///3.如果时间相同且操作相同,则按照钥匙编号升序排队 
23         return a.num<b.num;
24         }
25     }
26 }
27 
28 int main()
29 {
30     int n,k;
31     while(cin>>n>>k)
32     {
33         vector<node> V;
34         vector<int> state(n+1); 
35         for(int i=1;i<=n;i++) state[i] = i;
36         for(int i=0;i<k;i++)
37         {
38             int num,start,len;
39             cin>>num>>start>>len;
40             V.push_back(node(num,start,1));//取走钥匙
41             V.push_back(node(num,start+len,0));
42         }
43         
44         ///按照起始时间升序排列 
45         sort(V.begin(),V.end(),cmp);
46         
47         for(int i=0;i<V.size();i++)
48         {
49             node u = V[i];
50             if(u.action == 1)//取走钥匙操作 
51             {
52                 for(int i=1;i<=n;i++)
53                     if(state[i] == u.num)
54                     {
55                         state[i] = -1;break;
56                     }    
57             }else{
58                 for(int i=1;i<=n;i++)
59                     if(state[i] == -1)
60                     {
61                         state[i] = u.num;break;
62                     }
63             }
64         }
65         
66         for(int i=1;i<=n;i++)
67         {
68             cout<<state[i];
69             if(i==n) cout<<endl;
70             else cout<<" ";
71         }
72     }
73     return 0;
74 }

 

 

 Python:

 1 N, K = list(map(int, input().split()))
 2 class node(object):
 3     def __init__(self, num, time, action):
 4         self.num, self.time, self.action = num, time, action
 5         # action: 1代表取,0代表放
 6 
 7 mlist = []
 8 for i in range(K):
 9     n1, n2, n3 = list(map(int, input().split()))
10     mlist.append(node(n1, n2, 1)) #
11     mlist.append(node(n1, n2 + n3, 0)) #
12 
13 keyarr = [(i+1) for i in range(N)]
14 
15 # 将list进行排序,规则为
16 # 1. 按照时间升序
17 # 2. 时间相同,按照先放后取,即action升序
18 # 3. time和action相同,按照钥匙编号num升序
19 mlist.sort(key=lambda x:(x.time, x.action, x.num))
20 
21 # 按照排序后的list进行操作
22 for i in range(2*K):
23     # print(mlist[i].time, mlist[i].action, mlist[i].num,)
24     if mlist[i].action == 1: #
25         keyarr[keyarr.index(mlist[i].num)] = -1
26     else:
27         for j in range(N):
28             if keyarr[j] == -1:
29                 keyarr[j] = mlist[i].num
30                 break
31 
32 for i in range(N):
33     print(keyarr[i], end="\t")

 

posted @ 2018-02-26 22:23  卉卉卉大爷  阅读(600)  评论(0编辑  收藏  举报