Bicycle training algorithm

# Algorithm training bicycle parking
resource constraints
Time limit: 1.0s memory limit: 256.0MB
description of the problem
  has come to stop in order n bike shed, in addition to the first bicycle, each bike will have just parked in a parking shed left or right bicycles. (eg parking shed has three bicycles, numbered from left to right: 3,5,1 now numbered as four bikes parked in the 2 to 5 bikes left, so now numbered bicycle parking shed They are: 3,2,5,1). N the case of a given parking bicycles, the final output sequentially numbered parking bicycle shed.
The input format
  of the first line of an integer n.
  A second line integer x. It represents the first bike number.
  The following n-1 lines of three integers x, y, z.
  When z = 0, x represents a number of bicycles parked just to the left of the bicycle number y
  z = 1, x represents a number of bicycles parked in just the right number y bicycle
output format
  from left to right output Parking No. bicycle shed
sample input
. 4
. 3
. 1. 3. 1
2 0. 1
. 5. 1 2
sample output
3251
data size and conventions
  n <= 100000
  bicycle positive integer numbers not more than 100,000.

Timeout Code 80
#### The main reason is the very large n, the variable length through the entire array #### during a very time-consuming
duplicated code

 1 #include<iostream>
 2 #include<vector>
 3 //anthor:ZQ
 4 using namespace std;
 5 int main(){
 6     vector<int>obj;
 7     vector<int>::iterator it;
 8     int n,ns,nq,flag,n1;
 9     cin>>n;
10     cin>>n1;
11     obj.push_back(n1);
12     for(int i=1;i<n;i++){
13         cin>>ns>>nq>>flag;
14         for(it=obj.begin();it!=obj.end();it++){
15             if(*it==nq){
16                 if(flag==0){
17                     obj.insert(it,ns);
18                 }else{
19                     obj.insert(it+1,ns);
20                 }
21                 break; 
22             }
23         }
24     }
25     for(it=obj.begin();it!=obj.end();it++){
26         cout<<*it<<" ";
27     }
28     return 0;
29 } 

Copy the code
to improve the code

### do not have to go looking for a bike a position, call the function ### find library () directly locked position

Code:

复制代码
 1 #include<iostream>
 2 #include<vector>
 3 #include<algorithm>
 4 //anthor:ZQ
 5 using namespace std;
 6 int main(){
 7     vector<int>obj;
 8     vector<int>::iterator it;
 9     vector<int>::iterator position;
10     int n,ns,nq,flag,n1;
11     cin>>n;
12     cin>>n1;
13     obj.push_back(n1);
14     for(int i=1;i<n;i++){
15         cin>>ns>>nq>>flag;
16         position=find(obj.begin(),obj.end(),nq);
17         if(flag==0){
18             obj.insert(position,ns);
19         }else{
20             obj.insert(position+1,ns);
21         }
22     }
23 //        for(it=obj.begin();it!=obj.end();it++){
24 //            if(*it==nq){
25 //                if(flag==0){
26 //                    obj.insert(it,ns);
27 //                }else{
28 //                    obj.insert(it+1,ns);
29 //                }
30 //                break; 
31 //            }
32 //        }
33     for(it=obj.begin();it!=obj.end();it++){
34         cout<<*it<<" ";
35     }
36     return 0;
37 } 
复制代码

Reproduced, please indicate the source
label: BlueBridgeOJ

Released four original articles · won praise 0 · Views 1590

Guess you like

Origin blog.csdn.net/qq_44139941/article/details/104536502
Recommended