阿里4.1 笔试第二题 请帮忙看看
iter=find(arrow.begin(),arrow.end(),small_value[0]); //这句话报错了 错在哪啊
#include<iostream>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
struct prs
{
int cost;
int att;
};
bool cmp(int a,int b)
{
return a>b;
}
bool cmp1(prs p1,prs p2)
{
return p1.att>p2.att;
}
bool cmp2(prs p1,prs p2)
{
return p1.cost<p2.cost;
}
int mincost(vector<int> &monst,vector<prs> &arrow)
{
int m_l=monst.size();
int a_l=arrow.size();
vector<prs>::iterator iter;
if(m_l<a_l)
return -1;
vector<prs> small_value;
prs temp;
int value=0;
sort(monst.begin(),monst.end(),cmp);
sort(arrow.begin(),arrow.end(),cmp1);
int m=0;
int a=0;
while(m<m_l&&a<a_l)
{
while(monst[m]<=arrow[a].att)
{
small_value.push_back(arrow[a]);
a++;
}
if(small_value.size()!=0)
{
sort(small_value.begin(),small_value.end(),cmp2);
value=value+small_value[0].cost;
iter=find(arrow.begin(),arrow.end(),small_value[0]);
arrow.erase(iter);
}
small_value.clear();
m++;
a=0;
a_l=arrow.size();
}
if(arrow.size()==0)
return value;
else return -1;
}
int main()
{
int n,m;
cin>>n>>m;
vector<int> monst;
vector<prs> arrow;
prs ptmp;
int temp;
int i;
for(i=0;i<n;i++)
{
cin>>temp;
monst.push_back(temp);
}
for(i=0;i<m;i++)
{
cin>>ptmp.cost;
arrow.push_back(ptmp);
}
for(i=0;i<m;i++)
{
cin>>arrow[i].att;
}
cout<<mincost(monst,arrow);
}
#阿里笔试##阿里巴巴##笔试题目##include<vector>
#include<queue>
#include<algorithm>
using namespace std;
struct prs
{
int cost;
int att;
};
bool cmp(int a,int b)
{
return a>b;
}
bool cmp1(prs p1,prs p2)
{
return p1.att>p2.att;
}
bool cmp2(prs p1,prs p2)
{
return p1.cost<p2.cost;
}
int mincost(vector<int> &monst,vector<prs> &arrow)
{
int m_l=monst.size();
int a_l=arrow.size();
vector<prs>::iterator iter;
if(m_l<a_l)
return -1;
vector<prs> small_value;
prs temp;
int value=0;
sort(monst.begin(),monst.end(),cmp);
sort(arrow.begin(),arrow.end(),cmp1);
int m=0;
int a=0;
while(m<m_l&&a<a_l)
{
while(monst[m]<=arrow[a].att)
{
small_value.push_back(arrow[a]);
a++;
}
if(small_value.size()!=0)
{
sort(small_value.begin(),small_value.end(),cmp2);
value=value+small_value[0].cost;
iter=find(arrow.begin(),arrow.end(),small_value[0]);
arrow.erase(iter);
}
small_value.clear();
m++;
a=0;
a_l=arrow.size();
}
if(arrow.size()==0)
return value;
else return -1;
}
int main()
{
int n,m;
cin>>n>>m;
vector<int> monst;
vector<prs> arrow;
prs ptmp;
int temp;
int i;
for(i=0;i<n;i++)
{
cin>>temp;
monst.push_back(temp);
}
for(i=0;i<m;i++)
{
cin>>ptmp.cost;
arrow.push_back(ptmp);
}
for(i=0;i<m;i++)
{
cin>>arrow[i].att;
}
cout<<mincost(monst,arrow);
}