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

【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;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值