codeforces 1040B(思维)

题意描述

有n个煎饼,每次可以反转会反转左右k个,求最小反转次数

思路

如果k为0的话,每个煎饼一定需要反转。如果 n n n能够整除 2 ∗ k + 1 2*k+1 2k+1的话,那么每次都加 2 ∗ k + 1 2*k+1 2k+1一定不会重叠,否则的话,我们可以先对余数的情况进行输出,然后就转换成了整除的状态。

AC代码

#include
#define x first
#define y second
#define PB push_back
#define mst(x,a) memset(x,a,sizeof(x))
#define all(a) begin(a),end(a)
#define rep(x,l,u) for(ll x=l;x
#define rrep(x,l,u) for(ll x=l;x>=u;x--)
#define IOS ios::sync_with_stdio(false);cin.tie(0);
using namespace std;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<long,long> PLL;
typedef pair<char,char> PCC;
typedef long long ll;
const int N=1005;
const int M=1e6+10;
const int INF=0x3f3f3f3f;
const int MOD=1e9+7;
void solve(){
    int n,k;cin>>n>>k;
    if(!k){
        cout<<n<<endl;
        rep(i,1,n+1) cout<<i<<' ';
        cout<<endl;
        return;
    }
    if(n<=2*k+1){
        cout<<1<<endl;
        cout<<n/2+1<<endl;
        return;
    }
    if(n%(2*k+1)==0){
        cout<<n/(2*k+1)<<endl;
        for(int i=k+1;i<=n;i+=(2*k+1)) cout<<i<<' ';
        cout<<endl;
        return;
    }
    cout<<n/(2*k+1)+1<<endl;
    for(int i=1+(n%(2*k+1))/2;i<=n;i+=(2*k+1)) cout<<i<<' ';
    cout<<endl;
}
int main(){
    IOS;
    solve();
    return 0;
}

你可能感兴趣的:(CodeForces,算法,思维)