Slava and tanks 877C
Slava plays his favorite game "Peace Lightning". Now he is flying a bomber on a very specific map.
Formally, map is a checkered field of size 1?×?n, the cells of which are numbered from 1 to n
If a tank takes damage for the first time, it instantly moves to one of the neighboring cells (a tank in the cell n
Help Slava to destroy all tanks using as few bombs as possible.
The first line contains a single integer n (2?≤?n?≤?100?000) — the size of the map.
OutputIn the first line print m — the minimum number of bombs Slava needs to destroy all tanks.
In the second line print m integers k1,?k2,?...,?km. The number ki means that the i-th bomb should be dropped at the cell ki.
If there are multiple answers, you can print any of them.
Examples input Copy2output Copy
3input Copy
2 1 2
3output Copy
4
2 1 3 2
題意:炸坦克,如果炸第一個坦克,它只能跑到第二個坦克,如果炸第n個坦克,它只能跑到第n-1個坦克,如果炸中間坦克,它可以跑到它相鄰的坦克上
分析:先炸偶數位上的坦克(因為偶數總是比奇數少),然後偶數位上的坦克都跑到奇數位上了,再炸奇數位上的坦克,原來偶數位上的坦克就沒了,奇數位上的所有坦克跑到偶數位上去了,然後再炸一次偶數位上的坦克,這下所有的坦克都炸沒了
1 #include<cstdio> 2 int main() 3 { 4 int n; 5 while(~scanf("%d",&n)) 6 { 7 printf("%d\n",3*n/2);//總次數 8 for(int i=2;i<=n;i+=2) 9 { 10 printf("%d ",i); 11 } 12 for(int i=1;i<=n;i+=2) 13 { 14 printf("%d ",i); 15 } 16 for(int i=2;i<=n;i+=2) 17 { 18 printf("%d ",i); 19 } 20 printf("\n"); 21 } 22 return 0; 23 }
Slava and tanks 877C