Codeforces Round 945 (Div. 2)A-C

A题

题目大意,有三个人进行比赛,比赛为两人,赢的一方加2分,输的一方不得分,若平局则两人都加1分,给出三人的最终得分且保证p1 <= p2 <= p3,求可能的最大平局数, 若没有可能则输出-1。

分析:

由于不论输赢对于总得分的收益都为2,所以先判断p1 + p2 + p3是否为偶数,若不是,则输出-1即可。

由于p1 <= p2 <= p3, 那么容易得知1和2先与3平局一定可以使收益最大,考虑分为两种情况,如果p1 + p2 < p3, 那么很显然答案即为p1 + p2, 如果p1 + p2 >= p3, 那么可以想象到应先是p1,p2与p3平局,然后p1和p2平局, 由于必然有结果,所以很容易得知答案为(p1 + p2 + p3) / 2。

代码:

#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <set>
#include <map>
#include <queue>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

const int N = 2e5 + 10;

void solve()
{
    int a, b, c;
    cin >> a >> b >> c;
    if (a + b + c & 1)
    {
        cout << -1 << endl;
    }
    else
    {
        if (a + b < c) cout << a + b << endl;
        else cout << (a + b + c) / 2 << endl;
    }
}

int main()
{
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int t;
    cin >> t;
    while (t -- )
        solve();
    return 0;
}

B题:

题目大意:

给你n个数,然后找一个最小的k使所有的二元组i,j满足ai到a[i+k-1]之间的所有数的(或)等于aj到a[j+k-1]之间的所有数的(或)

分析:

很显然如果答案为k,那么大于等于k一定满足题意,小于k一定不满足题意,符合二分思想。然后我们需要判断能否符合题意, 容易想到遍历数组, 考虑到时间复杂度, 会想到前缀和,但是由于题目要求的为位运算, 所以可以考虑将a数组中的每个数看成二进制, 将每个位置都判断一遍即可。

时间复杂度:O(nlognlogn)

代码:

​
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <set>
#include <map>
#include <queue>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

const int N = 2e5 + 10;

int n;
int a[N][20];
int s[N][20];

bool check(int x)
{
    for (int j = 0; j < 20; j ++ )
    {
        for (int i = 1; i <= n - x; i ++ )
            if (!(s[i + x][j] - s[i][j] && s[x][j] - s[0][j] || !(s[i + x][j] - s[i][j]) && !(s[x][j] - s[0][j])))
            {
                return false;
            }
    }
    return true;
}

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

int main()
{
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int t;
    cin >> t;
    while (t -- )
        solve();
    return 0;
}

​

C题:

题目大意:

给你一个1到n的全排列p,然后你要构造一个全排列q, 数组ai=pi+qi,使得a【i- 1】<ai < a【i+ 1】的i(1<i<n)最多

分析:考虑到n一定为偶数, 思考可知最大价值一定为n / 2 - 1, 要求中间严格大于两边的数最多, 我们考虑将所有可能成为中间的位置加成n + 2, 其他位置尽可能平均处理, 可以推算总值为n * (n + 1), 成为中间的位置的总值为(n + 2) * (n / 2 - 1), 那么尽可能平均后的其他位置最大不会超过n + 1。 由于1是无法加成n + 2的, 所以我们考虑从1开始向数组两边取位置, 相邻中间位置的距离为2。 最后从值为1的位置开始, 加上未被使用的最大数, 以此做到尽可能去平均处理。

代码:

#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <set>
#include <map>
#include <queue>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

const int N = 2e5 + 10;

int n;
int a[N];//位置为i的值
int p[N];//存值为i的位置
int res[N];//存答案
bool f[N];//true为还未用过

void solve()
{
    cin >> n;
    for (int i = 1; i <= n; i ++ )
    {
        cin >> a[i];
        p[a[i]] = i;
        res[i] = 0;
        f[i] = 1;
    }

    int l = p[1] - 1, r = p[1] + 1;
    while (l > 1)
    {
        res[l] = n + 2 - a[l];// 直接定下中间位置的值
        f[res[l]] = 0;//标记
        l -= 2;
    }
    while (r < n)
    {
        res[r] = n + 2 - a[r];
        f[res[r]] = 0;
        r += 2;
    }

    for (int i = 1, j = n; i <= n; i ++ )// 双指针寻找还未有答案的位置的数,从最小开始, 找目前最大
    {
        if (res[p[i]]) continue;
        while (j && !f[j]) j -- ;
        res[p[i]] = j;
        f[j] = 0;
    }

    for (int i = 1; i <= n; i ++ ) cout << res[i] << ' ';
    cout << endl;
}

int main()
{
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int t;
    cin >> t;
    while (t -- )
        solve();
    return 0;
}

翻译还是太花时间了, a题还读错了一次(差点以为样例是错的了), 思路挺难完整记录下来的,将就着看吧

  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
抱歉,根据提供的引用内容,我无法理解你具体想要问什么问题。请提供更清晰明确的问题,我将竭诚为你解答。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.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^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [【CodeforcesCodeforces Round 865 (Div. 2) (补赛)](https://blog.csdn.net/t_mod/article/details/130104033)[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^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *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^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值