B. Sequence Game

文章介绍了如何在给定有序数组a和大小为n的b数组中,根据特定规则插入元素并输出最终答案。程序使用C++实现,包括读取输入、处理逻辑和输出结果。

题目:

样例:

输入
6
3
4 6 3
3
1 2 3
5
1 7 9 5 7
1
144
2
1 1
5
1 2 2 1 1

输出
6
4 3 2 6 3 3
3
1 2 3
6
1 7 9 3 5 7
1
144
2
1 1
6
1 2 2 1 1 1

思路:

        找规律,思维题。我们查看 a 和 b 之间的关系可以知道,a[i - 1] <= a[i] 就放进 b 序列当中,遇到 a[i - 1] > a[i] 的时候我们要插入一个比 a[i] 还要小的值。

代码详解如下:

#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <unordered_map>
#define endl '\n'
#define YES puts("YES")
#define NO puts("NO")
#define umap unordered_map
#pragma GCC optimize(3,"Ofast","inline")
#define ___G std::ios::sync_with_stdio(false),cin.tie(0), cout.tie(0)
using namespace std;
const int N = 2e6 + 10;

inline void solve()
{
	int x;	// 读取值
	vector<int>ans;		// 答案 a 数组
	
	int n;		// 	b 数组大小 
	cin >> n;	
	
	cin >> x;	// 读入 a1
	
	ans.emplace_back(x);	// 首先存储 a1;
	
	int r = x;		// r 作为记录前一个数值
	
	for(int i = 1;i < n;++i)
	{
		cin >> x;
		
		// 如果 b 数组中当前元素大于上一个记录的元素,那么存储好 a 数组里面
		if(x >= r) ans.emplace_back(x);
		else
		{
			ans.emplace_back(1);
			ans.emplace_back(x);		
		}	
		
		r = x;	// 记录当前元素
	}	
	
	// 输出答案
	cout << ans.size() << endl;
	for(auto i : ans)
	{
		cout << i << ' ';
	}
	cout << endl;
}


int main()
{
//	freopen("a.txt", "r", stdin);
	___G;
	int _t = 1;
	cin >> _t;
	while (_t--)
	{
		solve();
	}

	return 0;
}

最后提交:

7-3 Manhattan(c++题解,代码无注释) 分数 35 作者 DAI, Longao 单位 杭州百腾教育科技有限公司 m.jpg Manhattan is a board game where players gain scores by building skyscrapers(摩天大楼)with blocks of heights 1, 2 or 3 on the table. After adding a block to an existing skyscraper, the total height of a player's blocks in this skyscraper must not be lower than the total height of any other players' blocks in this same skyscraper. For the sake of simplicity, let's assume that only two players are playing the game and they add blocks to the only skyscraper. Also there are infinite blocks of each type. Let sequence A=a 1 ​ ,a 2 ​ ,⋯,a n ​ (1≤a i ​ ≤3) be the heights of blocks added to the skyscraper in order and sequence B=b 1 ​ ,b 2 ​ ,⋯,b n ​ (b i ​ ∈{1,2}) be the indices of the players adding the corresponding blocks. After the game, however, sequence B is lost and only sequence A remains. Please calculate the number of different sequence B's so that the building procedure of the skyscraper does not break the game rules. Note that two sequences b 1 ​ ,b 2 ​ ,⋯,b n ​ and b 1 ′ ​ ,b 2 ′ ​ ,⋯,b n ′ ​ of length n are different if there exists an integer k such that 1≤k≤n and b k ​  =b k ′ ​ . Input The first line contains an integer n (1≤n≤10 5 ) indicating the number of blocks in the skyscraper. The second line contains n integers a 1 ​ ,a 2 ​ ,⋯,a n ​ (1≤a i ​ ≤3) indicating the heights of the blocks added to the skyscraper in order. Output Output in one line an integer indicating the number of different sequence B's. As the answer might be large, please print the answer modulo (10 9 +7). Sample Input 1 3 1 3 3 Sample Output 1 6 Sample Input 2 10 1 1 1 1 1 1 1 1 1 1 Sample Output 2 94 Hint For the first sample test case, the possible sequences are [1,1,1], [1,2,1], [1,2,2], [2,1,1], [2,1,2] and [2,2,2]. [1,1,2] is invalid because after the 2nd player builds the 3rd block, the total height of his blocks is 3, which is lower than the total height of blocks of the 1st player (which is 4). [2,2,1] is also invalid in the similar way. 代码长度限制 16 KB Java (javac) 时间限制 600 ms 内存限制 128 MB 其他编译器 时间限制 200 ms 内存限制 128 MB 栈限制 131072 KB
08-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值