544. Output Contest Matches

本文介绍了一种在NBA季后赛中使比赛更有趣的策略:始终让实力较强的队伍与较弱的队伍进行比赛。通过示例展示了如何将这种策略应用于不同规模的比赛场次,并给出了一段Java代码实现该策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

During the NBA playoffs, we always arrange the rather strong team to play with the rather weak team, like make the rank 1 team play with the rank nth team, which is a good strategy to make the contest more interesting. Now, you're given n teams, you need to output their final contest matches in the form of a string.

The n teams are given in the form of positive integers from 1 to n, which represents their initial rank. (Rank 1 is the strongest team and Rank n is the weakest team.) We'll use parentheses('(', ')') and commas(',') to represent the contest team pairing - parentheses('(' , ')') for pairing and commas(',') for partition. During the pairing process in each round, you always need to follow the strategy of making the rather strong one pair with the rather weak one.

Example 1:

Input: 2
Output: (1,2)
Explanation: 
Initially, we have the team 1 and the team 2, placed like: 1,2.
Then we pair the team (1,2) together with '(', ')' and ',', which is the final answer.

Example 2:

Input: 4
Output: ((1,4),(2,3))
Explanation: 
In the first round, we pair the team 1 and 4, the team 2 and 3 together, as we need to make the strong team and weak team together.
And we got (1,4),(2,3).
In the second round, the winners of (1,4) and (2,3) need to play again to generate the final winner, so you need to add the paratheses outside them.
And we got the final answer ((1,4),(2,3)).

Example 3:

Input: 8
Output: (((1,8),(4,5)),((2,7),(3,6)))
Explanation: 
First round: (1,8),(2,7),(3,6),(4,5)
Second round: ((1,8),(4,5)),((2,7),(3,6))
Third round: (((1,8),(4,5)),((2,7),(3,6)))
Since the third round will generate the final winner, you need to output the answer (((1,8),(4,5)),((2,7),(3,6))).

Note:

  1. The n is in range [2, 212].
  2. We ensure that the input n can be converted into the form 2k, where k is a positive integer.
根据题意,每次取队list列第一位和最后一位配对。建立templist,每次从两端向中间遍历list,之后用templist覆盖掉list。list的大小等于一时就得到答案。代码如下:

public class Solution {
    public String findContestMatch(int n) {
        List<String> list = new ArrayList<String>();
        for (int i = 1; i <= n; i ++) {
            list.add(i + "");
        }
        while (list.size() > 1) {
            List<String> temp = new ArrayList<String>();
            for (int i = 0; i < list.size()/2; i ++) {
                temp.add("(" + list.get(i) + "," + list.get(list.size() - 1 - i) + ")");
            }
            list = temp;
        }
        return list.get(0);
    }
}

题目描述 现有一个空整数序列 A = ( ) A=()。你需要按顺序处理 Q Q个查询操作,查询分为两种类型: 类型 1 1:格式为1 c x。将 c c个 x x添加到 A A的末尾。 类型 2 2:格式为2 k。从 A A中移除前 k k个元素,并输出被移除的 k k个整数之和。保证此时序列长度不小于 k k。 约束条件 1 ≤ Q ≤ 2 × 10 5 1≤Q≤2×10 5 在类型 1 1查询中, 1 ≤ c ≤ 10 9 1≤c≤10 9 在类型 1 1查询中, 1 ≤ x ≤ 10 9 1≤x≤10 9 在类型 2 2查询中,设此时 A A的长度为 n n,则 1 ≤ k ≤ min ⁡ ( 10 9 , n ) 1≤k≤min(10 9 ,n) 所有输入值均为整数 输入格式 输入通过标准输入给出,格式如下: Q Q query 1 query 1 ​ query 2 query 2 ​ ⋮ ⋮ query Q query Q ​ 其中 query i query i ​ 表示第 i i个查询,其格式为以下两种之一: 1 1 c c x x 2 2 k k 输出格式 设共有 q q个类型 2 2查询。输出 q q行, 第 i i行应包含对第 i i个类型 2 2查询的应答结果。 样例1 Inputcopy Outputcopy 5 1 2 3 1 4 5 2 3 1 6 2 2 5 11 19 第 1 1次查询:将 2 2个 3 3添加到 A A末尾。此时序列变为 A = ( 3 , 3 ) A=(3,3) 第 2 2次查询:将 4 4个 5 5添加到 A A末尾。此时序列变为 A = ( 3 , 3 , 5 , 5 , 5 , 5 ) A=(3,3,5,5,5,5) 第 3 3次查询:移除 A A中前 3 3个元素。被移除的 3 3个整数之和为 3 + 3 + 5 = 11 3+3+5=11,故输出 11 11。移除后序列为 A = ( 5 , 5 , 5 ) A=(5,5,5) 第 4 4次查询:将 6 6个 2 2添加到 A A末尾。此时序列变为 A = ( 5 , 5 , 5 , 2 , 2 , 2 , 2 , 2 , 2 ) A=(5,5,5,2,2,2,2,2,2) 第 5 5次查询:移除 A A中前 5 5个元素。被移除的 5 5个整数之和为 5 + 5 + 5 + 2 + 2 = 19 5+5+5+2+2=19,故输出 19 19。移除后序列为 A = ( 2 , 2 , 2 , 2 ) A=(2,2,2,2) 样例2 Inputcopy Outputcopy 10 1 75 22 1 81 72 1 2 97 1 84 82 1 2 32 1 39 57 2 45 1 40 16 2 32 2 42 990 804 3024 样例3 Inputcopy Outputcopy 10 1 160449218 954291757 2 17217760 1 353195922 501899080 1 350034067 910748511 1 824284691 470338674 2 180999835 1 131381221 677959980 1 346948152 208032501 1 893229302 506147731 2 298309896 16430766442004320 155640513381884866 149721462357295680 cpp: #include <bits/stdc++.h> #define int long long using namespace std; int q; signed main() { cin >> q; queue<int> a; while (q--) { int n; cin >> n; if (n == 1) { int c, x; cin >> c >> x; for (int i = 1; i <= c; i++) a.push(x); } else if (n == 2) { int k, sum = 0; cin >> k; for (int i = 1; i <= k; i++) { sum += a.front(); a.pop(); } cout << sum << endl; } } return 0; } 优化cpp
最新发布
07-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值