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 n n skewers parallel to each other, and enumerated them with consecutive integers from 1 1 to n n in order from left to right. For better cooking, he puts them quite close to each other, so when he turns skewer number i i, it leads to turning k k closest skewers from each side of the skewer i i, that is, skewers number i −k i−k, i − k +1 i−k+1, ..., i −1 i−1, i +1 i+1, ..., i + k −1 i+k−1, i +k i+k (if they exist).
For example, let n =6 n=6 and k =1 k=1. When Miroslav turns skewer number 3 3, then skewers with numbers 2 2, 3 3, and 4 4 will come up turned over. If after that he turns skewer number 1 1, then skewers number 1 1, 3 3, and 4 4 will be turned over, while skewer number 2 2will 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 n n skewers with the minimal possible number of actions. For example, for the above example n =6 n=6 and k =1 k=1, two turnings are sufficient: he can turn over skewers number 2 2 and 5 5.
Help Miroslav turn over all n n skewers.
Input
The first line contains two integers n n and k k ( 1 ≤ n ≤1000 1≤n≤1000, 0 ≤ k ≤1000 0≤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 l l — the minimum number of actions needed by Miroslav to turn over all n n skewers. After than print l l integers from 1 1 to n ndenoting the number of the skewer that is to be turned over at the corresponding step.
Examples
Input
Output
Input
Output
Note
In the first example the first operation turns over skewers 1 1, 2 2 and 3 3, the second operation turns over skewers 4 4, 5 5, 6 6 and 7 7.
In the second example it is also correct to turn over skewers 2 2 and 5 5, but turning skewers 2 2 and 4 4, or 1 1 and 5 5 are incorrect solutions because the skewer 3 3 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 longLL; 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 { 21int i,p,j,n,k; 22int cnt=0; 23scanf("%d%d",&n,&k); 24p=n%(2*k+1); 25if(p==0) 26{ 27for(i=k+1;i<=n;i+=(2*k+1)) 28ans[cnt++]=i; 29} 30else if(p<=(k+1)&&p!=0) 31{ 32ans[cnt++]=1; 33for(i=k+2;i<=n;i+=(2*k+1)) 34{ 35if(i+k<=n) 36ans[cnt++]=i+k; 37else 38ans[cnt++]=n; 39} 40 41} 42else 43{ 44ans[cnt++]=p-(k+1)+1; 45for(i=p+1;i<=n;i+=(2*k+1)) 46{ 47if(i+k<n) 48ans[cnt++]=i+k; 49} 50} 51printf("%d\n",cnt); 52for(i=0;i<cnt;i++) 53printf("%d ",ans[i]); 54return 0; 55 } View Code