想不出怎么贪心,后来网上查了下,先用一整块板子覆盖全部,然后把间隔区域排序,从大逐一删除,知道被分割成cow数量段或者全部间隔区域删除完全。
注意开始和结束区域可能不占1次划分
/*
ID: hubiao cave
PROG: barn1
LANG: C++
*/
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
struct dist
{
int start;
int end;
int range;
};
char buf[202];
dist distarray[202];
int buf1[202];
void InsertSort(int number);
void InsertSort2(int number);
int main()
{
ifstream fin("barn1.in");
ofstream fout("barn1.out");
int barn,stall,cow;
fin>>barn>>stall>>cow;
int bestall;
for(int i=1;i<=cow;i++)
fin>>buf1[i];
InsertSort2(cow);
if(cow==1)
{
fout<<1<<endl;
return 0;
}
int j=0;
for(int i=1;i<=cow;i++)
{
int temp;
//fin>>temp;
temp=buf1[i];
if(i==1)
{
bestall=temp;
if(temp!=1)
{
j++;
distarray[j].end=temp;
distarray[j].range=temp-1;
continue;
}
}
if(i==cow)
{
if(temp!=stall)
{
j++;
distarray[j].start=temp;
distarray[j].range=stall-temp;
//break;
}
}
if(temp>bestall+1)
{
j++;
distarray[j].start=bestall;
distarray[j].end=temp;
distarray[j].range=temp-bestall-1;
bestall=temp;
}
else
{
bestall=temp;
}
}
InsertSort(j);
for(int i=j;i>=1;i--)
{
if(distarray[i].start==0||distarray[i].end==0)
{
stall-=distarray[i].range;
}
}
for(int i=j;i>=1;i--)
{
if(barn>1)
{
if(distarray[i].start!=0&&distarray[i].end!=0)
{
stall-=distarray[i].range;
barn--;
}
//stall-=distarray[i].range;
}
}
fout<<stall<<endl;
return 0;
}
void InsertSort(int number)
{
int i=2;
for(;i<=number;i++)
{
distarray[0]=distarray[i];
int j=i-1;
for(;j>0;j--)
{
if(distarray[j].range>distarray[0].range)
{
distarray[j+1]=distarray[j];
}
else
break;
}
distarray[j+1]=distarray[0];
}
}
void InsertSort2(int number)
{
int i=2;
for(;i<=number;i++)
{
buf1[0]=buf1[i];
int j=i-1;
for(;j>0;j--)
{
if(buf1[j]>buf1[0])
{
buf1[j+1]=buf1[j];
}
else
break;
}
buf1[j+1]=buf1[0];
}
}