Circular Local MiniMax

Circular Local MiniMax

题面翻译

题目描述

给你 n 个整数 a1, a2, an 。 问有没有可能将它们排列在一个圆上,使每个数字严格地大于其相邻的两个数字或严格地小于其相邻的两个数

  • bi-1 < bi > bi+1
  • bi-1 > bi < bi+1

输入格式:

输入的第一行包含一个整数 $ t $ ( $ 1 \le t \le 3\cdot 10^4 $ ) ,表示数据组数。

接下来 T T T 行,第一行包含一个的整数 $ n $ ( $ 3 \le n \le 10^5 $ ) ,

第二行包含 $ n $ 整数 $ a_1, a_2, \ldots, a_n $ ( $ 0 \le a_i \le 10^9 $ ) 。( ∑ n ≤ 2 ⋅ 1 0 5 \sum n \le 2 \cdot 10^5 n2105

输出格式

对于每组数据,如果不可能将数字排列在满足语句条件的圆圈上,则输出 $ \texttt{NO} $ 。

否则,输出 $ \texttt{YES} $ 。在第二行,输出 $ b_1, b_2, \ldots, b_n $ ,这是 $ a_1, a_2, \ldots, a_n $ 的重新排列,并且满足声明中的条件。如果有多种有效的数字排列方式,你可以输出其中任何一种。

样例解释

可以证明,对于第一个和第三个测试案例,没有有效的安排。

在第二个测试案例中,安排 $ [1, 8, 4, 9] $ 是有效的。在这个排列中, 1 1 1 4 4 4 都比它们相邻的数小,而 8 、 9 8、9 89 则较大。

在第四个测试案例中,排列方式 [ 1 , 11 , 1 , 111 , 1 , 1111 ] [1,11,1,111,1,1111] [111111111111] 有效。在这个排列中,等于 1 1 1的三个元素比它们相邻的数小,而所有其他元素都比它们相邻的数大。

题目描述

You are given $ n $ integers $ a_1, a_2, \ldots, a_n $ . Is it possible to arrange them on a circle so that each number is strictly greater than both its neighbors or strictly smaller than both its neighbors?

In other words, check if there exists a rearrangement $ b_1, b_2, \ldots, b_n $ of the integers $ a_1, a_2, \ldots, a_n $ such that for each $ i $ from $ 1 $ to $ n $ at least one of the following conditions holds:

  • $ b_{i-1} < b_i > b_{i+1} $
  • $ b_{i-1} > b_i < b_{i+1} $

To make sense of the previous formulas for $ i=1 $ and $ i=n $ , one shall define $ b_0=b_n $ and $ b_{n+1}=b_1 $ .

输入格式

The first line of the input contains a single integer $ t $ ( $ 1 \le t \le 3\cdot 10^4 $ ) — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer $ n $ ( $ 3 \le n \le 10^5 $ ) — the number of integers.

The second line of each test case contains $ n $ integers $ a_1, a_2, \ldots, a_n $ ( $ 0 \le a_i \le 10^9 $ ).

The sum of $ n $ over all test cases doesn’t exceed $ 2\cdot 10^5 $ .

输出格式

For each test case, if it is not possible to arrange the numbers on the circle satisfying the conditions from the statement, output $ \texttt{NO} $ . You can output each letter in any case.

Otherwise, output $ \texttt{YES} $ . In the second line, output $ n $ integers $ b_1, b_2, \ldots, b_n $ , which are a rearrangement of $ a_1, a_2, \ldots, a_n $ and satisfy the conditions from the statement. If there are multiple valid ways to arrange the numbers, you can output any of them.

样例 #1

样例输入 #1

4
3
1 1 2
4
1 9 8 4
4
2 0 2 2
6
1 1 1 11 111 1111

样例输出 #1

NO
YES
1 8 4 9 
NO
YES
1 11 1 111 1 1111

提示

It can be shown that there are no valid arrangements for the first and the third test cases.

In the second test case, the arrangement $ [1, 8, 4, 9] $ works. In this arrangement, $ 1 $ and $ 4 $ are both smaller than their neighbors, and $ 8, 9 $ are larger.

In the fourth test case, the arrangement $ [1, 11, 1, 111, 1, 1111] $ works. In this arrangement, the three elements equal to $ 1 $ are smaller than their neighbors, while all other elements are larger than their neighbors

思路分析:

当n是奇数的时候,无论怎么组合无法成立,所以我们只需要考虑偶数即可,每次0 2 4 6 等位置放小的数,1 3 5 7放大的数,当出现前后有相等的情况直接标记判断

#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
int t,n;
int w[100005];
int cnt[100005];
int main()
{
    cin>>t;
    while(t--)
    {
        memset(cnt,0,sizeof cnt);
        bool st=0;
        int k=0;
        cin>>n;
        for(int i=0;i<n;i++) cin>>w[i];
        if(n%2==1){//提前结合
            cout<<"NO"<<endl;
            continue;
        }
        sort(w,w+n);
        int j=0;
	    for (int i = 0; i < n; i += 2) {//重构数组
		    cnt[i] = w[j++];
	    }
	    for (int i = 1; i < n; i += 2) {
		    cnt[i] = w[j++];
	    }
        cnt[j]=cnt[0];
        for(int i=1;i<=j;i++){ 
            if(cnt[i-1]==cnt[i])
            //出现相等的情况,不满足条件
                st=1;
        }
        if(st) cout<<"NO"<<endl;
        else{
        cout<<"YES"<<endl;
        for(int i=0;i<j;i++) cout<<cnt[i]<<" ";
        cout<<endl;
        }
    }
    return 0;
}
  • 37
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Minimax is a popular algorithm used in game theory and artificial intelligence to determine the optimal move for a player in a game with perfect information. It is often used in games such as chess, tic-tac-toe, and Connect Four. In C#, you can implement the Minimax algorithm by representing the game state and creating a recursive function to search through all possible moves and evaluate their outcomes. Here's a simplified example of Minimax in C#: ```csharp public int MiniMax(int[] board, int depth, bool isMaximizingPlayer) { // Base case: check if the game is over or the maximum depth is reached if (IsGameOver(board) || depth == 0) { return Evaluate(board); } if (isMaximizingPlayer) { int bestScore = int.MinValue; foreach (int move in GetPossibleMoves(board)) { int[] newBoard = MakeMove(board, move); int score = MiniMax(newBoard, depth - 1, false); bestScore = Math.Max(bestScore, score); } return bestScore; } else { int bestScore = int.MaxValue; foreach (int move in GetPossibleMoves(board)) { int[] newBoard = MakeMove(board, move); int score = MiniMax(newBoard, depth - 1, true); bestScore = Math.Min(bestScore, score); } return bestScore; } } // Example usage: int[] board = { 0, 0, 0, 0, 0, 0, 0, 0, 0 }; int bestMove = -1; int bestScore = int.MinValue; foreach (int move in GetPossibleMoves(board)) { int[] newBoard = MakeMove(board, move); int score = MiniMax(newBoard, depth, false); if (score > bestScore) { bestScore = score; bestMove = move; } } Console.WriteLine("Best move: " + bestMove); ``` This is a simplified example, and you would need to implement the `IsGameOver()`, `Evaluate()`, `GetPossibleMoves()`, and `MakeMove()` functions according to the rules of your specific game. The `depth` parameter controls the depth of the search tree, determining how far ahead the algorithm looks. Adjusting this parameter can affect the algorithm's performance and the quality of the decisions it makes.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值