1.活动安排问题
使剩余安排时间极大化
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
struct Node
{
int s;
int f;
};
bool cmp(Node a,Node b)
{
return a.f<b.f;
}
int greedySelector(vector<Node> &x,vector<bool> &a)
{
int n=x.size()-1;
int j=0;
int count=1;
a[0]=true;
for(int i=1;i<=n;i++)
{
if(x[i].s>=x[j].f)
{
a[i]=true;
j=i;
count++;
}
else
{
a[i]=false;
}
}
return count;
}
int main()
{
vector<Node> n;
vector<bool> a;
int num;
cin>>num;
for(int i=0;i<num;i++)
{
Node temp;
cin>>temp.s>>temp.f;
n.push_back(temp);
a.push_back(false);
}
sort(n.begin(),n.end(),cmp);
cout<<greedySelector(n,a)<<endl;
return 0;
}
2.最优装载
#include<iostream>
#include<vector>
#include<algorithm>
#include<cstdlib>
using namespace std;
struct Node
{
double w;
int i;
};
bool cmp(Node a,Node b)
{
return a.w<b.w;
}
double random(int start,int end)
{
return (start+rand()%(end-start)+0.0)/10;
}
int loading(vector<Node> &x,vector<int> &a,double c)
{
int i=0;
while(i<x.size())
{
if(x[i].w<=c)
{
c=c-x[i].w;
a.push_back(x[i].i);
i++;
}
else
{
break;
}
}
return i;
}
int main()
{
vector<Node> n;
vector<int> a;
int num;
double c;
cin>>num>>c;
for(int i=0;i<num;i++)
{
Node temp;
temp.w=random(1,2000);
temp.i=i;
n.push_back(temp);
cout<<"n["<<i<<"]: "<<temp.w<&