首页 > 其他分享 >Codeforces Round 922 (Div 2)

Codeforces Round 922 (Div 2)

时间:2024-02-02 13:11:30浏览次数:20  
标签:return int Codeforces long 1LL && 922 Div define

Codeforces Round 922 (Div. 2)

A - Brick Wall

贪心的去想水平的越多越好,k随意改,那么可以构造出没有垂直的,那么计算水平的有几块就行

#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define Acode ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define int long long

void solve()
{
    int n, m;
    cin >> n >> m;
    cout << n * (m / 2) << endl;
}

signed main()
{
    Acode;
    int T = 1;
    cin >> T;
    while (T--)
    {
        solve();
    }
    return 0;
}

B - Minimize Inversions

任意次数的交换, 交换出现两种情况,i,j 位置在a数组是逆序对,b不是,那么交换完之后,在a数组不是逆序对了,但是b数组会是,因为是排列,不存在元素相等,不影响答案。因为是排列,不存在元素相等。第二种情况即为a中为逆序对,b中也为逆序对,交换完,逆序对减少

所以直接结构体排序即可,就把a升序排,b跟着排就行

#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define Acode ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define int long long
const int N = 2e6 + 10;

struct node
{
    int a, b;
} s1[N];

bool cmp(node a, node b)
{
    return a.a < b.a;
}

void solve()
{
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++)
        cin >> s1[i].a;
    for (int i = 1; i <= n; i++)
        cin >> s1[i].b;
    sort(s1 + 1, s1 + 1 + n, cmp);
    for (int i = 1; i <= n; i++)
        cout << s1[i].a << " ";
    cout << endl;
    for (int i = 1; i <= n; i++)
        cout << s1[i].b << " ";
    cout << endl;
}

signed main()
{
    Acode;
    int T = 1;
    cin >> T;
    while (T--)
    {
        solve();
    }
    return 0;
}

C - XOR-distance

异或操作,位运算优先考虑拆位!

考虑贪心的做法,就是a和b在同一位置上如果相同,那么x在这一位置上取0/1都不影响的。

那么考虑a和b不同的情况,我们假定a大于b , 那么想从高位到低位,a和b不同的第一次出现的位置。

分成两种情况1. r<2^i,就是我们改不了这一位,那么非常简单,我们后面能改的地方就都改成和这一位相反即可。

  1. r>=2^i 就是我们能改,我们不改,但是把后面能改的改成相反的。由于高位能改,自然后面低位都能改。
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define Acode ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define int long long
const int N = 2e6 + 10;

int qpow(int a, int b)
{
    int res = 1;
    while (b)
    {
        if (b & 1)
            res = res * a;
        b >>= 1;
        a = a * a;
    }
    return res;
}

void solve()
{
    int a, b, r;
    cin >> a >> b >> r;
    if (a < b)
        swap(a, b);
    int a1 = a, b1 = b;
    int flag = 0;
    for (int i = 62; i >= 0; i--)
    {
        int x = qpow(2LL, i);
        // if (i == 2)
        // {
        //     cerr << (((a >> i) & 1) == 1) << " " << (((b >> i) & 1) == 0) << " " << cnt << " " << abs(cnt - 2 * x) << " " << r << " " << cnt << endl;
        //     cerr << x << endl;
        // }
        if (((a >> i) & 1LL) == 1 && ((b >> i) & 1LL) == 0 && !flag)
        {
            flag = 1;
            continue;
        }
        if (((a >> i) & 1LL) == 0 && ((b >> i) & 1LL) == 1 && !flag)
        {
            flag = -1;
            continue;
        }

        if (((a >> i) & 1LL) == 0 && ((b >> i) & 1LL) == 1 && flag == -1 && r >= x)
        {
            a1 += x;
            b1 -= x;
            r -= x;
            // if (i == 2)
            //     cerr << 2 << endl;
        }
        if (((a >> i) & 1LL) == 1 && ((b >> i) & 1LL) == 0 && flag == 1 && r >= x)
        {
            a1 -= x;
            b1 += x;
            r -= x;
            // if (i == 2)
            //     cerr << 2 << endl;
        }
        // cerr << a1 << " " << b1 << " " << r << " " << flag << " " << i << endl;
    }
    cout << abs(a1 - b1) << endl;
}

signed main()
{
    Acode;
    int T = 1;
    cin >> T;
    while (T--)
    {
        solve();
    }
    return 0;
}

D - Blocking Elements

最大值最小,考虑二分答案,check比较难想

贪心不正确,用dp。

dp[i]的含义为,前i位的情况均符合答案,并且i点作为分割点,那么我们创建一个n+1这个点,且这个点a[n+1]=0,用于check dp[n+1]和mid的关系,因为n这个点我们不确定是否作为分割点,而a[n+1]=0,对每一段的值无影响,且可以从前面合法的情况中转移

所以,

dp[i]=min(dp[k]~dp[i-1])+a[i];k为可转移的最左的下标

#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define Acode ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define int long long
const int N = 2e6 + 10;
int n;
int a[N];
int dp[N];
int s[N];

bool check(int mid)
{
    int l = 0;
    set<pair<int, int>> st;  
    //用set来维护当前可以转移的最优解,其实是可以用单调队列转移的,但是set能过
    for (int i = 0; i <= n + 1; i++)
    {
        if (st.size() == 0)
            dp[i] = a[i], st.insert({dp[i], i});
        else
        {
            while (s[i - 1] - s[l] > mid && l <= i - 1)
            {
                st.erase({dp[l], l});
                l++;
            }
            int x = (*st.begin()).first;
            dp[i] = x + a[i];
            st.insert({dp[i], i});
        }
        // cerr << st.size() << " " << (*st.begin()).first << endl;
    }
    // cerr << endl;
    // for (int i = 1; i <= n + 1; i++)
    // {
    //     cerr << dp[i] << " ";
    // }
    if (dp[n + 1] > mid)
        return false;
    else
        return true;
}

void solve()
{
    cin >> n;
    a[n + 1] = 0;
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
        s[i] = s[i - 1] + a[i];
    }
    int l = 1, r = 1e15;
    // cerr << check(7) << endl;
    while (l <= r)
    {
        int mid = l + r >> 1;
        if (check(mid))
            r = mid - 1;
        else
            l = mid + 1;
    }
    cout << l << endl;
}

signed main()
{
    Acode;
    int T = 1;
    cin >> T;
    while (T--)
    {
        solve();
    }
    return 0;
}

标签:return,int,Codeforces,long,1LL,&&,922,Div,define
From: https://www.cnblogs.com/chenchen336/p/18003008

相关文章

  • Codeforces Round 922 (Div. 2)
    https://codeforces.com/contest/1918题目很有意思。A~Dvp中过了,但是太太太慢,亟须复健。E赛后过的,交互题真是难调!F看题解过的A.BrickWall*800用砖头砌墙有形状\(1\timesk\)的水平砖和形状\(k\times1\)的竖直砖,要不重不漏地铺满\(n\timesm\)的区域,问水平砖数量......
  • CF922 div2 D. Blocking Elements
    题面代码点击查看代码#include<bits/stdc++.h>usingnamespacestd;#defineIOSios::sync_with_stdio(0);cin.tie(0);cout.tie(0);#definerep(i,a,n)for(inti=a;i<=n;i++)#defineper(i,a,n)for(inti=n;i>=a;i--)#defineendl"\n"#definefif......
  • js+css 父div,里面有很多子div,当子div在父div中放不下时候,自动滚动子div,向左横向滚动,
    <!DOCTYPEhtml><htmllang="en"><head>  <metacharset="UTF-8">  <metaname="viewport"content="width=device-width,initial-scale=1.0">  <style>    #parentDiv{  ......
  • Codeforces Round 922 (Div. 2)
    CodeforcesRound922(Div.2)ps:25分钟AB都over,C给我打破防了、、、讨厌异或、、、我一直以为是数学结果、、、只能说一怒之下怒了一下A.BrickWall想法:要使得稳定性高,那么就多用1*2的砖块就行(A题可以直接找规律,通过样例)#include<bits/stdc++.h>usingnamespacestd;......
  • Codeforces Round 922 (Div. 2) 赛后总结
    自豪的是D题做出来了,悲哀的是B题没能做出来C题的绝对值最小D题,DP存不下状态就把状态放进所求值中比赛快结束的时候,我想,这个B题,它但凡需要我通过归并排序或者树状数组求逆序对,不比C题进制转化要难?于是我就猜了一个结论结论是对的,但不幸的是,我编程实现的时候出错了考虑怎样证......
  • Codeforces Round 770 (Div. 2)(数学异或奇偶性)
    B.FortuneTelling拿到题目看数据范围之后就知道暴力显然是来不及的。那么只能找性质。\(考虑x和x+3的不同\quad奇偶性不同\)\(然后考虑两种操作对于一个数的奇偶性的影响\)\(加法:同奇偶则运算结果是偶,不同则运算结果为奇\)\(异或:惊奇的发现异或也是这样的\)这样我们就......
  • Codeforces Round 922 (Div. 2)
    基本情况A题当时做完提交一直编译错误后面发现是语言选择错误,浪费了五六分钟,然后去做B没想到去看C看了样例感觉可以做,结果干了好久都没出来,倒回去看B还是没做出来,感觉全程很紧张不知道为什么,脚一直在抖。A.BrickWall没啥好说的,就是全部放竖直的,实在不能放了再放横的而且把横......
  • Codeforces Round 921 (Div. 1)
    Preface被折纸狠狠地腐乳了,但好在手速够快光速写完前两题成功把Ohara_Rinne这个号也打上橙了之后就不开其它小号打了,也差不多该尝试去向上挑战了,不然一直呆在舒适圈内也没啥提升的说A.DidWeGetEverythingCovered?直接把序列自动机建出来,不妨设状态\((x,y)\)表示构造了长......
  • Codeforces Round 922 (Div. 2)
    CodeforcesRound922(Div.2)比赛链接A.BrickWall思路简单的模拟,要想实现最高的稳定性,就横着放就可以了,因为长度必须大于等于2,所以最后即使不能被2整除,也可以算在里面Code#include<bits/stdc++.h>usingnamespacestd;#defineintlonglongvoidsolve(){ intn,......
  • Codeforces Round 922 (Div.2)
    题目链接点这里CF1918ABrickWallvoidsolve(){lln,m;cin>>n>>m;cout<<n*(m/2)<<endl;}CF1918BMinimizeInversions注意到,当其中一个排列有序时,总的逆序对数量最少()今天找个时间补上证明对于任意一对\(i,j\)位置,其可能的逆序对总......