题解:SP26348 NINJA5 - K NUMBERS
whisper_youth · · 题解
SP26348
题目大意
这道题真的有必要读题吗
从 1~N 中选尽量多的数,要求:
- 任意两个被选的数不相邻
- 给定的 k 个数必须被选
求最多能选多少个数。
思路
注意到 题目给的 k 个数会把
对于每一个长度为 因为不想特判第一和最后一个区间 因为第一个区间从
代码
最后附上 优美的 代码
#include<bits/stdc++.h>
using namespace std;
int n,k,ans;
int a[2000005];
void solve(){
cin>>n>>k;
for(int i=1;i<=k;i++){
cin>>a[i];
}
sort(a+1,a+k+1);//排序
a[k+1]=n+2;
a[0]=-1; //把第一和最后一个区间加进去
ans=k;
for(int i=0;i<=k;i++){
int len=a[i+1]-a[i]-3;//两端都不能选,所以减三
ans+=(len+1)/2;
}
cout<<ans<<endl;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int t;cin>>t;
while(t--) solve();//养成多组输入输出单独写函数好习惯(嗯对)
return 0;
}