Codeforces Global Round 23 C. Permutation Operations 解题报告

原题链接:

Problem - D - Codeforces

题目描述:

Alperen has two strings, ss and tt which are both initially equal to "a".

He will perform qq operations of two types on the given strings:

  • 1kx1kx — Append the string xx exactly kk times at the end of string ss. In other words, s:=s+x+⋯+xk timess:=s+x+⋯+x⏟k times.
  • 2kx2kx — Append the string xx exactly kk times at the end of string tt. In other words, t:=t+x+⋯+xk timest:=t+x+⋯+x⏟k times.

After each operation, determine if it is possible to rearrange the characters of ss and tt such that ss is lexicographically smaller†† than tt.

Note that the strings change after performing each operation and don't go back to their initial states.

†† Simply speaking, the lexicographical order is the order in which words are listed in a dictionary. A formal definition is as follows: string pp is lexicographically smaller than string qq if there exists a position ii such that pi<qipi<qi, and for all j<ij<i, pj=qjpj=qj. If no such ii exists, then pp is lexicographically smaller than qq if the length of pp is less than the length of qq. For example, abdc<abeabdc<abe and abc<abcdabc<abcd, where we write p<qp<q if pp is lexicographically smaller than qq.

Input

The first line of the input contains an integer tt (1≤t≤1041≤t≤104) — the number of test cases.

The first line of each test case contains an integer qq (1≤q≤105)(1≤q≤105) — the number of operations Alperen will perform.

Then qq lines follow, each containing two positive integers dd and kk (1≤d≤21≤d≤2; 1≤k≤1051≤k≤105) and a non-empty string xx consisting of lowercase English letters — the type of the operation, the number of times we will append string xx and the string we need to append respectively.

It is guaranteed that the sum of qq over all test cases doesn't exceed 105105 and that the sum of lengths of all strings xx in the input doesn't exceed 5⋅1055⋅105.

Output

For each operation, output "YES", if it is possible to arrange the elements in both strings in such a way that ss is lexicographically smaller than tt and "NO" otherwise.

题目大意:

给定一个长度为n的正整数数组和正整数x,判断(a1!+a2!+a3!+……+an!)是否被x!整除。

解题思路:

因为(n + 1)! = (n + 1) * n!,也就是n + 1个n!可以合并为(n + 1)!,

所以我们可以记录数组中每个小于x的ai出现的数量,每出现(ai + 1)个i,我们就可以将其合并为一个ai + 1。最后只需要所有的合并操作之后,所有的ai都大于等于x,那么其阶乘和是能够整除x!的。具体实现的时候使用map对ai进行计数即可。细节详见代码。

代码(CPP):

#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 5e5 + 10;
const int INF = 0x3fffffff;
int n, x;
map<int, int> mp;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cout << fixed;
    cout.precision(18);

    cin >> n >> x;
    for (int i = 1; i <= n; i++)
    {
        int v;
        cin >> v;
        if(v < x)
            mp[v]++;
    }
    bool flag = true;
    for (int i = 1; i < x; i++)
    {
        if(mp[i] % (i + 1) != 0)  // 合并不完,还有多余的,那么最后一定有小于x的ai
        {
            flag = false;
            break;
        }
        mp[i + 1] += mp[i] / (i + 1);
    }
    if(flag)
        cout << "Yes\n";
    else
        cout << "No\n";

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,Codeforces Round 511 (Div. 1)是一个比赛的名称。然而,引用内容中没有提供与这个比赛相关的具体信息或问题。因此,我无法回答关于Codeforces Round 511 (Div. 1)的问题。如果您有关于这个比赛的具体问题,请提供更多的信息,我将尽力回答。 #### 引用[.reference_title] - *1* [Codeforces Round 860 (Div. 2)题解](https://blog.csdn.net/qq_60653991/article/details/129802687)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Codeforces Round 867 (Div. 3)(A题到E题)](https://blog.csdn.net/wdgkd/article/details/130370975)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Codeforces Round 872 (Div. 2)(前三道](https://blog.csdn.net/qq_68286180/article/details/130570952)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值