AtCoder Beginner Contest 286——A - Range Swap

AtCoder Beginner Contest 286 题目讲解

A题 B题 C题 D题 E题


蒟蒻来讲题,还望大家喜。若哪有问题,大家尽可提!

Hello, 大家好哇!本初中生蒟蒻今天讲解一下AtCoder Beginner Contest 286的A题——Range Swap

===========================================================================================

原题

Problem Statement

You are given a sequence A = ( A 1 ​ , A 2 ​ , … , A N ​ ) A = (A_{1}​, A_{2}​, … , A_{N}​) A=(A1,A2,,AN) of length N N N and positive integers P , Q , R P,Q,R P,Q,R, and S S S.
Here, P P P, Q Q Q, R R R, and S S S satisfy 1 ≤ P ≤ Q < R ≤ S ≤ N 1\leq P\leq Q < R\leq S\leq N 1PQ<RSN and Q − P = S − R . Q−P=S−R. QP=SR.

Let B = ( B 1 ​ , B 2 ​ , … , B N ​ ) B=(B_{1}​,B_{2}​, …,B_{N}​) B=(B1,B2,,BN) be the sequence obtained by swapping the P P P-th through Q Q Q-th terms and the R R R-th through S S S-th terms of A A A.
Print the sequence B B B.

Constraints

  • 1 ≤ N ≤ 100 1\leq N\leq 100 1N100
  • 1 ≤ A i ​ ≤ 100 1\leq A_{i}​\leq 100 1Ai100
  • 1 ≤ P ≤ Q < R ≤ S ≤ N 1\leq P\leq Q1PQ<RSN
  • Q − P = S − R Q−P=S−R QP=SR
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format:

N  P Q R S
A1​ A2​ … AN​

Output

Print B 1 ​ , B 2 ​ , … , B N B_{1}​,B_{2}​,…,B_{N} B1,B2,,BN​, with spaces in between.

Sample Input 1

8 1 3 5 7
1 2 3 4 5 6 7 8

Sample Output 1

5 6 7 4 1 2 3 8

Swapping the 1 1 1-st through 3 3 3-rd terms ( 1 , 2 , 3 ) (1,2,3) (1,2,3) and the 5 5 5-th through 7 7 7-th terms ( 5 , 6 , 7 ) (5,6,7) (5,6,7) of the sequence A = ( 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ) A=(1,2,3,4,5,6,7,8) A=(1,2,3,4,5,6,7,8) results in B = ( 5 , 6 , 7 , 4 , 1 , 2 , 3 , 8 ) B=(5,6,7,4,1,2,3,8) B=(5,6,7,4,1,2,3,8), which should be printed with spaces in between.

Sample Input 2

5 2 3 4 5
2 2 1 1 1

Sample Output 2

2 1 1 2 1

The same integer may occur multiple times in the sequence.

Sample Input 3

2 1 1 2 2
50 100

Sample Output 3

100 50

Sample Input 4

10 2 4 7 9
22 75 26 45 72 81 47 29 97 2

Sample Output 4

22 47 29 97 72 81 75 26 45 2

思路

因为就只要更改2个区间,所以整个序列分成5个段,依次输出即可。(详情请看代码)


代码

#include 

using namespace std;

const int N = 1e2 + 10;

int n, p, q, r, s;
int a[N];


int main()
{
	cin >> n >> p >> q >> r >> s;
	
	for (int i = 1; i <= n; i ++)	
		cin >> a[i];
		
	for (int i = 1; i < p; i ++)//区间1之前的位置 
		cout << a[i] << " ";
		
	for (int i = r; i <= s; i ++) //区间1的位置输出区间2的元素的值
		cout <<a[i] << " ";
		
	for (int i = q + 1; i < r; i ++)//区间1与2之间的值
		cout << a[i] << " ";
		
	for (int i = p; i <= q; i ++)//区间2的位置输出区间1的元素的值
		cout << a[i] << " ";
		
	for (int i = s + 1; i <= n; i ++) //区间2之后的位置的值
		cout << a[i] << " ";

	return 0;
}

今天就到这里了!

大家有什么问题尽管提,我都会尽力回答的!最后,除夕夜祝大家新年快乐!
在这里插入图片描述

吾欲您伸手,点的小赞赞。吾欲您喜欢,点得小关注!

你可能感兴趣的:(算法-暴力,算法,c++,动态规划)