hihocoder :補提交卡
阿新 • • 發佈:2019-02-10
1 列舉,貪心
#include<iostream>
#include<string>using namespace std;
int main(){
int testNum,N,M,max=0;
int Day[100],delet[100],output[100],gap;
cin>>testNum;
for(int i=0;i<testNum;i++){
cin>>N>>M;
for(int j=0;j<N;j++){
cin>>Day[j];
}
if(N>M)
for(int j=0;j<N-M+1;j++){
for(int m=0;m<j;m++){
delet[m]=Day[m];
}
for(int m=j+M;m<N;m++){
delet[m-M]=Day[m];
}
for(int k=0;k<N-M+1;k++){
if(k==0)
gap=delet[k]-1;
else if(k==N-M)
gap=100-delet[k-1];
else
gap=delet[k]-delet[k-1]-1;
max=gap>max?gap:max;
}
}
else
max=100;
output[i]=max;
max=0;
}
for(int i=0;i<testNum;i++){
cout<<output[i]<<endl;
}
system("pause");
return 0;
}
2 貪心改進
for(j=1;j<=N-M+1;j++){
if((Day[j+M]-Day[j-1]-1)>max){max=(Day[j+M]-Day[j-1]-1);
}
}
對於一個具體問題,要確定它是否具有貪心選擇性質,必須證明每一步所作的貪心選擇最終導致問題的整體最優解.
分析:(1)最優子結構:M張補提交卡,如果M-1張是最優的,最後一張補提交卡一定是用在M-1的連續位置。
(2)最優的一定是這M張卡用的位置。