首页 > 其他分享 >Codeforces Round #842 (Div. 2) A-C, 补D

Codeforces Round #842 (Div. 2) A-C, 补D

时间:2023-01-06 15:55:05浏览次数:54  
标签:p2 p1 842 int mst Codeforces mp Div define

A. Greatest Convex

题意:

给定一个 k ,求一个小于 k 的数 x ,使得 x! + (x - 1)! 是 k 的倍数

分析:

题目已经给出提示: y! = y ⋅ (y − 1)!,输出 n - 1

cout << n - 1 << endl;

B. Quick Sort

题意:

给定 n 个数,要求每次选择其中的 k 个数进行排序,并按排序后的顺序放在数列最后,问最少的操作次数使 n 个数单调递增

分析:

由于最后是有序的,每步不管怎么排,应该按照选较大的 k 个数放到最后,对与顺序正确但位置不正确的几个数,如 ...1...2...3...,只需要选择中间逆序的数进行排排序,即只需要从 1 开始的递增序列的长度不需要选出来排序;
所以先计算不需要重排的数最多有多少,然后剩下的数 ÷ k 上取整即可

#include <bits/stdc++.h>
#include <bits/extc++.h>
using namespace std;
using namespace __gnu_pbds;
#define mst(x, y) memset(x, y, sizeof x)
#define endl '\n'
#define INF LONG_LONG_MAX
#define x first
#define y second
#define int long long
#define Lson u << 1, l, mid
#define Rson u << 1 | 1, mid + 1, r
#define FAST ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
const int N = 200010, MOD = 1e9 + 7;
const double EPS = 1e-6;
typedef pair<int, int> PII;
typedef unordered_map<int, int> Ump;
int T;
int n, k;
int a[N], pos[N];
int idx;
void solve()
{
    idx = 0;
    cin >> n >> k;
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
        pos[a[i]] = i;
    }

    int res = 1;
    for (int i = 2; i <= n; i++)
    {
        if (pos[i] > pos[i - 1])
            res++;
        else
            break;
    }
    cout << ceil(1.0 * (n - res) / k) << endl;
}
signed main()
{
    FAST;
    cin >> T;
    while (T--)
        solve();
    return 0;
}

C. Elemental Decompress

题意:

\[构造,构造排列 p1 ,p2, 使得 a[i] = max(p1[i], p2[i]) \]

分析:

首先看出:每个数出现次数不能超过 2 (1 出现次数不超过 1),

  • 对于出现 2 次的数,p1 放一次, p2 放一次
  • 对于出现 1 次的数,都放在 p1

这样安排后,对 p1 剩下的数进行放置,每次 p1[i] 安排一个没安排的且小于 a[i] 的最大的数,对 p2 也按照这样的方式计算,在过程中,若出现找不到这个数,那么说明出现矛盾,”NO“
举例:

\[\begin{array}{|c|c|c|} \hline a&6&5&5&4&3&1\\ \hline p1&6&5&*&4&3&1\\ \hline p2&6&*&5&4&3&1\\ \hline \end{array} \]

下面只需要安排空格上的数字即可
知乎上更简洁的代码

#include <bits/stdc++.h>
#include <bits/extc++.h>
using namespace std;
using namespace __gnu_pbds;
#define mst(x, y) memset(x, y, sizeof x)
#define endl '\n'
#define INF LONG_LONG_MAX
#define x first
#define y second
#define int long long
#define Lson u << 1, l, mid
#define Rson u << 1 | 1, mid + 1, r
#define FAST ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
const int N = 200010, MOD = 1e9 + 7;
const double EPS = 1e-6;
typedef pair<int, int> PII;
typedef unordered_map<int, int> Ump;
int T;
int n;
int a[N];
int p1[N], p2[N];
bool f[N], st1[N], st2[N];
map<int, int> mp;
void init()
{
    mst(p1, 0), mst(p2, 0);
    mp.clear();
    mst(f, false);
    mst(st1, false), mst(st2, false);
}
void solve()
{
    init();
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
        mp[a[i]]++;
        if (mp[a[i]] == 2)
        {
            f[a[i]] = true;
        }
    }
    for (int i = 1; i <= n; i++)
    {
        if (mp[a[i]] > 2 || mp[1] > 1)
        {
            cout << "NO" << endl;
            return;
        }
    }

    for (int i = 1; i <= n; i++)
    {
        if (mp[a[i]] == 1 && !f[a[i]])
        {
            p1[i] = p2[i] = a[i];
            st1[a[i]] = true, st2[a[i]] = true;
        }
        else if (mp[a[i]] == 1 && f[a[i]])
        {
            p2[i] = a[i], st2[a[i]] = true;
        }
        else
        {
            p1[i] = a[i];
            st1[a[i]] = true;
            mp[a[i]]--;
        }
    }

    vector<int> v1, v2;
    for (int i = 1; i <= n; i++)
    {
        if (!st1[i])
            v1.push_back(i);
        if (!st2[i])
            v2.push_back(i);
    }

    for (int i = 1; i <= n; i++)
    {
        if (!p1[i])
        {
            auto pos = lower_bound(v1.begin(), v1.end(), a[i]);
            if (pos == v1.begin())
            {
                cout << "NO" << endl;
                return;
            }
            pos--;
            p1[i] = *pos;
            v1.erase(pos);
        }
    }

    for (int i = 1; i <= n; i++)
    {
        if (!p2[i])
        {
            auto pos = lower_bound(v2.begin(), v2.end(), a[i]);
            if (pos == v2.begin())
            {
                cout << "NO" << endl;
                return;
            }
            pos--;
            p2[i] = *pos;
            v2.erase(pos);
        }
    }
    
    cout << "YES" << endl;
    for (int i = 1; i <= n; i++)
        cout << p1[i] << " ";
    cout << endl;
    for (int i = 1; i <= n; i++)
        cout << p2[i] << " ";
    cout << endl;
}
signed main()
{
    FAST;
    cin >> T;
    while (T--)
        solve();
    return 0;
}

标签:p2,p1,842,int,mst,Codeforces,mp,Div,define
From: https://www.cnblogs.com/Aidan347/p/17030675.html

相关文章

  • Codeforces Round #842 (Div. 2) A-D题解
    比赛链接A、GreatestConvex非常的简单。根据样例直接猜是输出\(n-1\).上一个\(Python\)代码。T=int(input())whileT>0:T-=1n=int(input())......
  • B. Quick Sort【Codeforces Round #842 (Div. 2)】
    B.QuickSortYouaregivenapermutation【排列】†\(p\)oflength\(n\)andapositiveinteger\(k≤n\).Inoneoperation,you:Choose\(k\)distinctelement......
  • Codeforces Contest 1616
    A.IntegerDiversity直接用个map贪心,如果有相同的就反向即可。B.MirrorintheString这道题洛谷的翻译锅了,所以建议去看原题。考虑这样一个字符串baacc,那么答案显......
  • 「Codeforces」寒假训练 2023 #2
    A.YetAnotherPalindromeProblem原题链接#include<bits/stdc++.h>usingnamespacestd;typedeflonglongll;constintN=1e5+10;intt;intn;inta[N];i......
  • Codeforces Round #839 (Div. 3)题解
    C.DifferentDifferences(贪心)题意​ 给定\(k\),\(n\)\((2\lek\len\le40)\)。从\([1-n]\)中不重复地任选\(k\)个数组成一个数组,使这个数组的差分数组中不同的......
  • Educational Codeforces Round 11
    EducationalCodeforcesRound11https://codeforces.com/contest/660A.Co-primeArray\(1\)与任何数的\(gcd\)都为\(1\),直接在不符合条件的两点间塞就可以了#inc......
  • 1.4 vp Codeforces Round #838 (Div. 2)
    A-DivideandConquer题意:给出序列a,设b为a中元素总和。你可以选择a中任意元素,将它除以二(向下取整)。问最少需要多少次可以使b为偶数思路:将a划分为奇偶两个集合。a中偶数......
  • LOJ #2842. 「JOISC 2018 Day 4」野猪
    题面传送门考试的时候只想到处理\(O(1)\)的边没想到维护\(O(1)\)的路径。首先如果没有可以退一步的限制显然就是相邻两点的最短路之和。退一步的限制想到点边互换。与处......
  • Codeforces Hello 2023 CF 1779 A~F 题解
    点我看题A.HallofFame先把不用操作就合法的情况判掉。然后发现交换LL,RR,RL都是没用的,只有交换LR是有用的,这样可以照亮相邻的两个位置。所以我们就是要找到一个位置i,......
  • Codeforces Hello 2023 CF 1779 A~F 题解
    点我看题A.HallofFame先把不用操作就合法的情况判掉。然后发现交换LL,RR,RL都是没用的,只有交换LR是有用的,这样可以照亮相邻的两个位置。所以我们就是要找到一个位置i,......