Codeforces Round #378 (Div. 2) 部分题解

本文深入讲解了几种典型的算法技巧,包括模拟法解决特定问题、贪心算法的应用场景及实现细节,以及通过实例演示如何高效地处理复杂的数据结构问题。

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

【A】模拟,顺序扫一遍,维护最大值即可!

【B】因为最多交换一个,直接先处理出不交换的答案,然后扫描一遍,对每一个交换之后,计算新的答案维护最大值!

【C】贪心,主要是合法性的判断,不合法的有3种,第一种两个序列的和不等,第二种最初时的怪物总体重与某时刻的怪物总体重不相等,第三种在最初时的怪物序列某段中,不存在任何一只怪物能够吃掉相邻怪物!其他情况都是有解的了,具体看代码吧!

【代码君】

//
//Created by just_sort 2016/11/2
//Copyright (c) 2016 just_sort.All Rights Reserved
//

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
using namespace __gnu_pbds;
typedef long long LL;
typedef pair<int, LL> pp;
#define MP(x,y) make_pair(x,y)
const int maxn = 200005;
const int N = 1005;
const int M = 2010;
const int inf = 0x3f3f3f3f;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>order_set;
//head

int a[N], b[N], n, m, l[N], r[N];
bool check()
{
    int pos = 1;
    for(int i = 1; i <= m; i++){
        int t = 0, j;
        for(j = pos; j <= n && t < b[i]; j++){
            t += a[j];
        }
        if(t != b[i]) return 0;
        int flag = 0;
        for(int k = pos + 1; k < j; k++){
            if(a[k] != a[k-1]) flag = 1;
        }
        l[i] = pos; r[i] = j;
        pos = j;
        if(flag == 0 && r[i] - l[i] != 1) return 0;
    }
    if(pos != n + 1) return 0;
    else return 1;
}

int main()
{
    scanf("%d",&n);
    for(int i = 1; i <= n; i++){
        scanf("%d",&a[i]);
    }
    scanf("%d",&m);
    for(int i = 1; i <= m; i++){
        scanf("%d",&b[i]);
    }
    if(!check())
    {
        printf("NO\n");
    }
    else{
        printf("YES\n");
        for(int i = 1; i <= m; i++){
            if(r[i] - l[i] == 1) continue;
            int mx = 0;
            for(int j = l[i]; j < r[i]; j++){
                mx = max(mx, a[j]);
            }
            int pos, flag;
            for(int j = l[i]; j < r[i]; j++)
            {
                if(a[j] == mx)
                {
                    if(j == l[i] && a[j+1] != mx)
                    {
                        flag = 1;
                        pos = j;
                        break;
                    }
                    else if(a[j] != a[j+1] && j != r[i] - 1)
                    {
                        flag = 1;
                        pos = j;
                        break;
                    }
                    else if(j == r[i] - 1 && a[j-1] != mx)
                    {
                        flag = 0;
                        pos = j;
                        break;
                    }
                    else if(a[j] != a[j-1] && j != l[i])
                    {
                        flag = 0;
                        pos = j;
                        break;
                    }
                }
            }
            if(flag)
            {
                for(int j = pos; j < r[i] - 1; j++){
                    printf("%d R\n",i + (pos - l[i]));
                }
                for(int j = i + pos - l[i]; j > i; j--){
                    printf("%d L\n",j);
                }
            }
            else
            {
                for(int j = i + pos -l[i]; j > i; j--)
                {
                    printf("%d L\n",j);
                }
                for(int j = pos + 1; j < r[i]; j++){
                    printf("%d R\n",i);
                }
            }
        }
    }
}

【D】贪心或者暴力都可以。贪心:我们先考虑用一块石头的最大内切球半径,一定是ans=max(min(a,b,c))。令a<=b<=c,那么如果我们想用两块石头来优化ans那么一定需要两块石头的b1c1和b2c2那个面,因为如果在这个合并的面内有a的话那么答案一定不会更优,所以我们用一个二维map存一下之前map[b][c]的最大的a即可,然后去判断合并的min(a+map[b][c],b)是否能更新ans即可

【代码1】

//
//Created by just_sort 2016/11/2
//Copyright (c) 2016 just_sort.All Rights Reserved
//

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
using namespace __gnu_pbds;
typedef long long LL;
typedef pair<int, LL> pp;
#define MP(x,y) make_pair(x,y)
const int maxn = 200005;
const int N = 1005;
const int M = 2010;
const int inf = 0x3f3f3f3f;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>order_set;
//head

int a[4];
map<pair<int,int>,int>ma;
map<pair<int,int>,int>id;

int main()
{
    int n, g1 = 0, g2 = 0, mx = 0;
    cin>>n;
    for(int i = 1; i <= n; i++){
        scanf("%d%d%d",&a[1],&a[2],&a[3]);
        sort(a+1,a+4);
        if(a[1] > mx)
        {
            g1 = i;
            g2 = 0;
            mx = a[1];
        }
        if(min(ma[MP(a[2],a[3])]+a[1],a[2]) > mx)
        {
            mx = min(ma[MP(a[2],a[3])]+a[1],a[2]);
            g1 = id[MP(a[2],a[3])];
            g2 = i;
        }
        if(a[1] > ma[MP(a[2],a[3])])
        {
            ma[MP(a[2],a[3])] = a[1];
            id[MP(a[2],a[3])] = i;
        }
    }
    if(g2 != 0){
        printf("2\n%d %d\n",g1,g2);
    }
    else{
        printf("1\n%d\n",g1);
    }
}


暴力:直接扫6个面维护最大值!这道题勉强卡过,数据大一点应该就g了。建议用贪心的方法!

【代码君】

//
//Created by just_sort 2016/10/28
//Copyright (c) 2016 just_sort.All Rights Reserved
//

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
using namespace __gnu_pbds;
typedef long long LL;
typedef pair<int, LL> pp;
#define MP(x,y) make_pair(x,y)
const int maxn = 200005;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>order_set;
//head

const int N =2e5;
map<pair<int,int>,pair<int,int> >mp;
int main()
{
    int n,l,w,h,f=1,ans=0,id1,id2;
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%d%d%d",&l,&w,&h);
        if(ans<min(min(l,w),h)) ans=min(min(l,w),h),id1=i,f=1;
        if(ans<min(min(l,w),h+mp[make_pair(l,w)].first)) f=2,ans=min(min(l,w),h+mp[make_pair(l,w)].first),id1=i,id2=mp[make_pair(l,w)].second;
        if(ans<min(min(l,w),h+mp[make_pair(w,l)].first)) f=2,ans=min(min(l,w),h+mp[make_pair(w,l)].first),id1=i,id2=mp[make_pair(w,l)].second;
        if(ans<min(min(l,h),w+mp[make_pair(l,h)].first)) f=2,ans=min(min(l,h),w+mp[make_pair(l,h)].first),id1=i,id2=mp[make_pair(l,h)].second;
        if(ans<min(min(l,h),w+mp[make_pair(h,l)].first)) f=2,ans=min(min(l,h),w+mp[make_pair(h,l)].first),id1=i,id2=mp[make_pair(h,l)].second;
        if(ans<min(min(h,w),l+mp[make_pair(h,w)].first)) f=2,ans=min(min(h,w),l+mp[make_pair(h,w)].first),id1=i,id2=mp[make_pair(h,w)].second;
        if(ans<min(min(h,w),l+mp[make_pair(w,h)].first)) f=2,ans=min(min(h,w),l+mp[make_pair(w,h)].first),id1=i,id2=mp[make_pair(w,h)].second;
        if(mp[make_pair(l,w)].first<h) mp[make_pair(l,w)].first=h,mp[make_pair(l,w)].second=i;
        if(mp[make_pair(w,l)].first<h) mp[make_pair(w,l)].first=h,mp[make_pair(w,l)].second=i;
        if(mp[make_pair(l,h)].first<w) mp[make_pair(l,h)].first=w,mp[make_pair(l,h)].second=i;
        if(mp[make_pair(h,l)].first<w) mp[make_pair(h,l)].first=w,mp[make_pair(h,l)].second=i;
        if(mp[make_pair(w,h)].first<l) mp[make_pair(w,h)].first=l,mp[make_pair(w,h)].second=i;
        if(mp[make_pair(h,w)].first<l) mp[make_pair(h,w)].first=l,mp[make_pair(h,w)].second=i;
    }
    if(f==1) printf("%d\n%d\n",f,id1);
    else printf("%d\n%d %d\n",f,id1,id2);
    return 0;
}




### 关于Codeforces Round 704 Div. 2 的信息 对于Codeforces Round 704 Div. 2的比赛,虽然未直接提及具体题目解析或参赛体验的内容,但是可以根据平台的一贯风格推测该轮比赛同样包含了多种算法挑战。通常这类赛事会涉及数据结构、动态规划、图论等方面的知识。 考虑到提供的参考资料并未覆盖到此特定编号的比赛详情[^1],建议访问Codeforces官方网站查询官方题解或是浏览社区论坛获取其他选手分享的经验总结。一般而言,在赛后不久就会有详细的解答发布出来供学习交流之用。 为了帮助理解同类型的竞赛内容,这里提供了一个基于过往相似赛事的例子——如何通过居中子数组特性来解决问题的方法: ```cpp // 假设有一个函数用于处理给定条件下的数组恢复问题 vector<int> restoreArray(vector<vector<int>>& adjacentPairs) { unordered_map<int, vector<int>> adj; for (auto& p : adjacentPairs){ adj[p[0]].push_back(p[1]); adj[p[1]].push_back(p[0]); } int start = 0; for(auto& [num, neighbors] : adj){ if(neighbors.size() == 1){ start = num; break; } } vector<int> res(adjacentPairs.size() + 1); unordered_set<int> seen; function<void(int,int)> dfs = [&](int node, int idx){ seen.insert(node); res[idx] = node; for(auto next : adj[node]){ if(!seen.count(next)){ dfs(next, idx + 1); } } }; dfs(start, 0); return res; } ``` 上述代码展示了利用深度优先搜索(DFS)重建原始序列的一种方式,这与某些情况下解决Codeforces比赛中遇到的问题思路相吻合[^4]。 #### 注意事项 由于缺乏针对Codeforces Round 704 Div. 2的具体材料支持,以上解释更多依赖于对同类活动的理解以及编程技巧的应用实例来进行说明。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值