codeforces - 877C - Slava and tanks 【找规律呀找规律】
2017-10-28 14:56
471 查看
C. Slava and tanks
time limit per test2 secondsmemory limit per test256 megabytes
inputstandard input
outputstandard output.
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, in each cell there can be one or several tanks. Slava doesn’t know the number of tanks and their positions, because he flies very high, but he can drop a bomb in any cell. All tanks in this cell will be damaged.
If a tank takes damage for the first time, it instantly moves to one of the neighboring cells (a tank in the cell n can only move to the cell n - 1, a tank in the cell 1 can only move to the cell 2). If a tank takes damage for the second time, it’s counted as destroyed and never moves again. The tanks move only when they are damaged for the first time, they do not move by themselves.
Help Slava to destroy all tanks using as few bombs as possible.
Input
The first line contains a single integer n (2 ≤ n ≤ 100 000) — the size of the map.Output
In 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
2output
32 1 2
input
3output
42 1 3 2
题意: 一共有n个坑,每个坑里有一个坦克,你可以在任意坑中放炸弹,当坦克第一次被炸弹炸中后它会立即往两边去(两边的只能向里去),当一个坦克被炸弹炸两次后就摧毁了,问你用最少的炸弹摧毁所有的坦克,并将落炸弹的顺序打印
分析: 这题一开始卡了好久,后来换了个思路,主要是顺序,当n是奇数的时候我们先炸偶数的,这时坦克都跑去奇数位了,然后偶数位都没坦克了,然后再炸奇数位的,其中由刚刚偶数位过来的坦克都被炸死了,所有的坦克都去偶数位了,最后我们在炸偶数位即可,当然当n位偶数的时候同理,模拟下即可。对了,特判下当n == 1时。
参考代码
#include <bits/stdc++.h> using namespace std; int main() { int n;cin>>n; if(n == 1) { cout<<2<<endl; cout<<1<<' '<<1<<endl; return 0; } if(n&1){ cout<<n/2*3+1<<endl; for(int i = 2;i <= n - 1;i+=2){ cout<<i<<' '; } for(int i = 1;i <= n;i+=2) { cout<<i<<' '; } for(int i = 2;i <= n-1; i += 2) { printf("%d%c",i,i == n-1?'\n':' '); } } else { cout<<n/2*3<<endl; for(int i = 1;i <= n - 1;i+=2){ cout<<i<<' '; } for(int i = 2;i <= n;i+=2) { cout<<i<<' '; } for(int i = 1;i <= n-1; i += 2) { printf("%d%c",i,i == n-1?'\n':' '); } } return 0; }
如有错误或遗漏,请私聊下UP,thx
相关文章推荐
- Codeforces 877(442 Div.2) C. Slava and tanks
- Codeforces 877 C Slava and tanks
- Codeforces Round #442 (Div. 2) C. Slava and tanks 【找规律】
- Codeforces 877 C. Slava and tanks
- 【CodeForces】669A - Little Artem and Presents(找规律)
- CodeForces 554A-Kyoya and Photobooks【规律】
- Codeforces 471C MUH and House of Cards【找规律+二分】
- 【Codeforces Round #442 (Div. 2) C】Slava and tanks
- codeforces 400C - Inna and Huge Candy Matrix(耐心找规律,细节还是要注意的)
- Codeforces 66 D Petya and His Friends(找规律)
- Codeforces Round #442 (Div. 2) C. Slava and tanks
- 【CodeForces 339B】Red and Blue Balls 找规律
- Codeforces Round #442 C. Slava and tanks
- codeforces 755 d PolandBall and Polygon(找规律)
- 【组合数+找规律】codeforces 815B - Karen and Test
- Codeforces--368C--Sereja and Algorithm(规律)
- Codeforces 599B: Spongebob and Joke(技巧,规律)
- CodeForces 261C Maxim and Matrix [规律题]
- CodeForces - 584D Dima and Lisa (素数拆分)数学规律
- Codeforces 476D Dreamoon and Sets 规律+构造