题目链接
题意
给你01串,求其中是否能恰好分割成类似于(0,010,01010)的字串。如果不行输出-1,如果可以,输出分割的字串数量和每个字串中的每个字母在01串中的位置。举个例子。001100,可以分成010和010。
题解
对题意稍作分析,知道这题应该用贪心的思路做。因为0是可以单独成串的,所以对1的分析优先性要高于0。接着分析所要分割的字串的特性。010是一个1带2个0,01010是2个1带3个0。为了使所有的1都能分到0,考虑的分成的字串要尽可能的长。分完之后单独的0单独成串。
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<cmath>
#include<cstdlib>
#include<string>
#include<cstring>
#include<bitset>
#define LL long long
#define mod 1e9+7
#define INF 0x3f3f3f3f
using namespace std;
namespace FastIO {
template<typename tp> inline void read(tp &x) {
x=0; register char c=getchar(); register bool f=0;
for(;c<'0'||c>'9';f|=(c=='-'),c = getchar());
for(;c>='0'&&c<='9';x=(x<<3)+(x<<1)+c-'0',c = getchar());
if(f) x=-x;
}
template<typename tp> inline void write(tp x) {
if (x==0) return (void) (putchar('0'));
if (x<0) putchar('-'),x=-x;
int pr[20]; register int cnt=0;
for (;x;x/=10) pr[++cnt]=x%10;
while (cnt) putchar(pr[cnt--]+'0');
}
template<typename tp> inline void writeln(tp x) {
write(x);
putchar('\n');
}
}
using namespace FastIO;
LL n,c;
int q;
main(){
read(n),read(q);
while(q--){
read(c);
if(c%2) writeln((c+1)/2); else{
LL d=n;
while(!(c%2)){
c=d-c/2;
d=c;
}
writeln(n-c+(c+1)/2);
}
}
}