京东4.18 笔试
1.最小亏损的题目,最后提交的,第一次排序超时,过了50%,换成大顶堆
#京东##笔试题目##include <iostream> #include <vector> #include <algorithm> #include <queue> using namespace std; void getMinloss(priority_queue<int>losData,int m,int n, int q){ int res=0; int day=q/m,lis=q%m; if(lis>0) day=day+1; if(q==n){ for (int i = 1; i <=day; ++i) { for (int j = 0; j < m; ++j) { res+=i*losData.top(); losData.pop(); if(losData.empty()) break; } } } else{ for (int k = 0; k <n-q; ++k){ losData.pop(); } for (int i = 1; i <=day; ++i) { for (int j = 0; j < m; ++j) { res+=i*losData.top(); losData.pop(); if(losData.empty()) break; } } } cout<<res<<endl; } int main() { int n,m,q; cin>>n>>m; priority_queue<int> losData; int temp; for (int i = 0; i <n ; ++i) { cin>>temp; losData.push(temp); } cin>>q; vector<int> qData; for (int j = 0; j < q; ++j) { cin>>temp; qData.push_back(temp); } for (int k = 0; k <q; ++k) { int s=qData[k]; getMinloss(losData,m,n,s); } return 0; }