CodeForces - 1040B Shashlik Cooking
Long story short, shashlik is Miroslav's favorite food. Shashlik is prepared on several skewers simultaneously. There are two states for each skewer: initial and turned over.
This time Miroslav laid out nn skewers parallel to each other, and enumerated them with consecutive integers from 11 to nn in order from left to right. For better cooking, he puts them quite close to each other, so when he turns skewer number ii, it leads to turning kk closest skewers from each side of the skewer ii, that is, skewers number i−ki−k, i−k+1i−k+1, ..., i−1i−1, i+1i+1, ..., i+k−1i+k−1, i+ki+k (if they exist).
For example, let n=6n=6 and k=1k=1. When Miroslav turns skewer number 33, then skewers with numbers 22, 33, and 44 will come up turned over. If after that he turns skewer number 11, then skewers number 11, 33, and 44 will be turned over, while skewer number 22will be in the initial position (because it is turned again).
As we said before, the art of cooking requires perfect timing, so Miroslav wants to turn over all nn skewers with the minimal possible number of actions. For example, for the above example n=6n=6 and k=1k=1, two turnings are sufficient: he can turn over skewers number 22 and 55.
Help Miroslav turn over all nn skewers.
Input
The first line contains two integers nn and kk (1≤n≤10001≤n≤1000, 0≤k≤10000≤k≤1000) — the number of skewers and the number of skewers from each side that are turned in one step.
Output
The first line should contain integer ll — the minimum number of actions needed by Miroslav to turn over all nn skewers. After than print ll integers from 11 to nndenoting the number of the skewer that is to be turned over at the corresponding step.
Examples
7 2
2
1 6
5 1
2
1 4
Note
In the first example the first operation turns over skewers 11, 22 and 33, the second operation turns over skewers 44, 55, 66 and 77.
In the second example it is also correct to turn over skewers 22 and 55, but turning skewers 22 and 44, or 11 and 55 are incorrect solutions because the skewer 33 is in the initial state after these operations.
若翻动其中的一个烤串,也会影响到两侧的烤串,问怎样才能翻动最少的次数,使得全部的烤串都由初始的正面变成反面。
一刻开始思考的是,两侧的烤串是否要翻动,直接考虑了边界,然后想用边界去就决定主体,但是这个切入点是错的。
正确的切入点是:因为每串烤串影响的范围是一定的,那么就有了个周期循环的过程,且每个烤串被翻过一次,也就是说不需要一个数被除两次,因此将问题缩小为有几个串是多余的。再分类讨论:
设余数为p,将所有多余的串放到开头。这些烤串肯定是只翻动一次,问题是翻哪一串。
又,不管翻哪一串最小的影响都是k+1,所以若p>(K+1)好说,若p<(k+1)&&p!=0 , 则必须翻第一串且因为它造成了过多的影响,所以之后每一个翻动的位置都要向后错。
1 #include<cstdio> 2 #include<cstdlib> 3 #include<cstring> 4 #include<string> 5 #include<cmath> 6 #include<algorithm> 7 #include<queue> 8 #include<stack> 9 #include<deque> 10 #include<map> 11 #include<iostream> 12 using namespace std; 13 typedef long long LL; 14 const double pi=acos(-1.0); 15 const double e=exp(1); 16 const int N = 100009; 17 18 int ans[10009]; 19 int main() 20 { 21 int i,p,j,n,k; 22 int cnt=0; 23 scanf("%d%d",&n,&k); 24 p=n%(2*k+1); 25 if(p==0) 26 { 27 for(i=k+1;i<=n;i+=(2*k+1)) 28 ans[cnt++]=i; 29 } 30 else if(p<=(k+1)&&p!=0) 31 { 32 ans[cnt++]=1; 33 for(i=k+2;i<=n;i+=(2*k+1)) 34 { 35 if(i+k<=n) 36 ans[cnt++]=i+k; 37 else 38 ans[cnt++]=n; 39 } 40 41 } 42 else 43 { 44 ans[cnt++]=p-(k+1)+1; 45 for(i=p+1;i<=n;i+=(2*k+1)) 46 { 47 if(i+k<n) 48 ans[cnt++]=i+k; 49 } 50 } 51 printf("%d\n",cnt); 52 for(i=0;i<cnt;i++) 53 printf("%d ",ans[i]); 54 return 0; 55 }