拼多多客户端笔试2020.08.02
只做了两个题目(太菜了),把自己的代码记录一下吧
第一个题目:0.94
第三个题目:0.95
第一个题目: #include <bits/stdc++.h> #include "iostream" #include "vector" #include "algorithm" using namespace std; int N,M,T; bool cmp(pair<int,int>a,pair<int,int>b){ return a.first>b.first; } int main() { cin>>N>>M>>T; int x,y; int maxYlunch=0,maxYdinner=0; vector<pair<int,int>>lunch; vector<pair<int,int>>dinner; while(N--){ cin>>x>>y; lunch.push_back(make_pair(y,x)); maxYlunch = max(maxYlunch,y); } while(M--){ cin>>x>>y; dinner.push_back(make_pair(y,x)); maxYdinner = max(maxYdinner,y); //cout<<" sdfsdfsd"<<endl; } //cout<<maxYdinner<<" "<<maxYlunch<<" "<<T; if((maxYlunch+maxYdinner)<T){ cout<<-1<<" "; return 0; } sort(lunch.begin(),lunch.end()); sort(dinner.begin(),dinner.end(),cmp);//从小到大排序否则超时了 //cout<<" sdfsdfsd1"<<endl; int fat=100000; for(int i=0;i<lunch.size();i++){ //cout<<lunch.size()<<" "; //cout<<" sdfsdfsd2"<<endl; if(lunch[i].first>=T){ fat = min(fat,lunch[i].second); continue; } for(int j=0;j<dinner.size();j++){ if((lunch[i].first + dinner[j].first)>=T){ fat = min(fat,lunch[i].second+dinner[j].second); } else{ break;//排序为了节省时间的,后边的都小,不满足条件,直接跳出就可以 } } } for(int i=0;i<dinner.size();i++) { if (dinner[i].first >= T) { fat = min(fat, dinner[i].second); } } cout<<fat<<" "; return 0; }
第二个题目 #include <bits/stdc++.h> #include "iostream" #include "vector" #include "algorithm" using namespace std; int k; int N; int main(){ cin>>k>>N; vector<int>num; int x; int n=N; while(N--){ cin>>x; num.push_back(x); } bool flag = false; int ans =0; int ret =0; for(int i=0;i<n;i++){ if(k-num[i]==0){ flag =true; cout<<"paradox"; break; } else if(k - num[i] < 0){ ret++; ans = num[i] - k; k = ans; } else if(k - num[i] > 0){ ans = k - num[i]; k -= num[i]; } } if(!flag){ cout<<ans<<" "<<ret; } return 0; }