hihocoder :补提交卡

来源:互联网 发布:java 泛型 t ? 编辑:程序博客网 时间:2024/05/23 18:49

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张卡用的位置。

原创粉丝点击