For this question, if N ∗ MN * MN∗If M is an even number, then the answer must beN ∗ M / 2 N * M / 2N∗M / 2 , if it is an odd number, it isN ∗ M / 2 − 1 N * M / 2-1N∗M/2−1 . Of course, considering that C++ division is an even division, so directly outputN ∗ M / 2 N * M / 2N∗M / 2 is fine, if it is python, outputN ∗ M / / 2 N * M // 2N∗M//2.
#include<iostream>usingnamespace std;int M, N;intmain(){
cin >> M >> N;
cout << M * N /2<< endl;return0;}
If N is an even number, it will be decomposed into 2; if N is an odd number, it will be decomposed into a lot of 2 and one 3.
#include<cstdio>intmain(){
int n;scanf("%d",&n);if(n %2){
n /=2;printf("%d\n", n);for(int i =0; i +1< n; i++){
printf("%d ",2);}printf("%d\n",3);}else{
n /=2;printf("%d\n", n);for(int i =0; i < n; i++){
printf("%d%c",2, i +1== n ?'\n':' ');}}return0;}
This question is to sort the data first, then put the smallest number first from small to large, and then put a number on each side from small to large, for example: 1, 2, 3. So, first put 1; then put 1, 2; then put 3, 1, 2
#include<cstdio>#include<algorithm>usingnamespace std;int a[100005], ans[100005];intmain(){
int N;scanf("%d",&N);for(int i =0; i < N; i++){
scanf("%d",&a[i]);}sort(a, a + N);int p1 =0, p2 = N -1;int i =0;while(i < N){
ans[p1++]= a[i++];if(i < N) ans[p2--]= a[i++];}bool flag =true;for(int i =0; i < N; i++){
if(ans[i]>= ans[(i + N -1)% N]+ ans[(i +1)% N]){
flag =false;break;}}if(flag){
printf("YES\n");for(int i =0; i < N; i++){
printf("%d%c", ans[i], i +1== N ?'\n':' ');}}else{
printf("NO\n");}return0;}
This is a very interesting construction problem. Assuming that the number of open spaces is cnt and the number of walls that need to be placed is K, then start from an open space with cnt-K spaces, and then put the remaining K on the wall. In this way, it can be ensured that the remaining cnt-K spaces are connected.
#include<cstdio>#include<cstring>#include<algorithm>usingnamespace std;int N, M, K;constint maxn =510;char maze[maxn][maxn];int dx[]={
1,-1,0,0}, dy[]={
0,0,1,-1};int cnt, blank;voiddfs(int x,int y){
maze[x][y]='*';for(int i =0; i <4; i++){
int nx = x + dx[i], ny = y + dy[i];if(nx <0|| nx >= N || ny <0|| ny >= M || maze[nx][ny]!='.')continue;if(++blank <= cnt - K)dfs(nx, ny);}}intmain(){
scanf("%d%d%d",&N,&M,&K);for(int i =0; i < N; i++){
scanf("%s", maze[i]);}for(int i =0; i < N; i++){
for(int j =0; j < M; j++){
if(maze[i][j]=='.') cnt++;}}bool flag =false;for(int i =0; i < N; i++){
for(int j =0; j < M; j++){
if(maze[i][j]=='.'&&++blank <= cnt - K){
dfs(i, j);
flag =true;break;}}if(flag)break;}for(int i =0; i < N; i++){
for(int j =0; j < M; j++){
if(maze[i][j]=='*') maze[i][j]='.';elseif(maze[i][j]=='.') maze[i][j]='X';}}for(int i =0; i < N; i++)printf("%s\n", maze[i]);return0;}