第 71 场双周赛

 

 

 1 #define pb push_back
 2 class Solution {
 3 public:
 4     int minimumSum(int num) {
 5         vector<int> p;
 6         while(num){
 7             p.pb(num%10),num/=10;
 8         }
 9         sort(p.begin(),p.end());
10         int res=p[0]*10+p[2]+p[1]*10+p[3];
11         return res;
12     }
13 };//2239

 

 

 

 1 #define pb push_back
 2 class Solution {
 3 public:
 4     vector<int> pivotArray(vector<int>& nums, int pivot) {
 5         vector<int> ans;
 6         for(auto i:nums){
 7             if(i<pivot) ans.pb(i);
 8         }
 9         for(auto i:nums){
10             if(i==pivot) ans.pb(i);
11         }
12         for(auto i:nums){
13             if(i>pivot) ans.pb(i);
14         }
15         return ans;
16     }
17 };

 

 

按常规套路枚举整除60取模60得到分钟和秒~

 

 1 const int inf = 0x3f3f3f3f;
 2 class Solution {
 3 public:
 4     int minCostSetTime(int startAt, int moveCost, int pushCost, int targetSeconds) {
 5         int ans=inf;
 6         vector<int> p;
 7         for(int i=0;i<=99;i++){  // 枚举秒 
 8             if((targetSeconds-i)%60==0&&i<=targetSeconds){
 9                 int res=(targetSeconds-i)/60;
10                 if(res>99) continue;
11                 int a=res,b=i;
12 //                cout<<"("<<a<<" "<<b<<"):";
13                 p.clear();
14                 
15                 while(b) p.push_back(b%10),b/=10;
16                 while(p.size()<2) p.push_back(0);
17                 while(a) p.push_back(a%10),a/=10;
18                 reverse(p.begin(),p.end());
19                 
20 //                for(auto k:p) cout<<k<<" ";cout<<"over";
21                 int st = startAt;
22                 int num=0,f=1;
23                 for(int j=0;j<p.size();j++){
24                     if(f){
25                         if(p[j]){
26                             f=0;
27                         }else{
28                             continue;
29                         }
30                     }
31                     if(p[j]==st){
32                         num+=pushCost;
33                     }else{
34                         num+=moveCost+pushCost;
35                         st=p[j];
36                     }
37                 }
38                 ans=min(ans,num);
39 //                cout<<num<<endl;        
40             }
41         }
42         return ans;
43     }
44 };

 

 

 

枚举分割点 + 优先队列

 

 1 const int N = 2e6+100 ;
 2 typedef long long ll;
 3 #define pb push_back
 4 priority_queue<int,vector<int>,less<int> > q;
 5 priority_queue<int,vector<int>,greater<int> > p;
 6 ll a[N],b[N];
 7 class Solution {
 8 public:
 9     long long minimumDifference(vector<int>& nums){
10         while(q.size()) q.pop();
11         while(p.size()) p.pop();
12         int m=nums.size();int n=m/3;
13         ll s=0;
14         for(int i=0;i<n;i++) q.push(nums[i]),s+=nums[i];
15         a[n-1]=s;
16         for(int i=n;i<m;i++){
17             if(nums[i]<q.top()){
18                 s-=q.top();
19                 s+=nums[i];
20                 q.pop();
21                 q.push(nums[i]);
22             }
23             a[i]=s;
24         }
25         s=0;
26         for(int i=m-1;i>=m-n;i--) p.push(nums[i]),s+=nums[i];
27         b[m-n]=s;
28         for(int i=m-n-1;i>=0;i--){
29             if(nums[i]>p.top()){
30                 s-=p.top();
31                 s+=nums[i];
32                 p.pop();
33                 p.push(nums[i]);
34             }
35             b[i]=s;
36         }
37         ll ans=a[n-1]-b[n];
38         for(int i=n;i<2*n;i++){
39             ans=min(ans,a[i]-b[i+1]);
40         }
41         return ans;
42     }
43 };

 

posted @ 2022-02-06 17:30  pengge666  阅读(24)  评论(0编辑  收藏  举报