zoukankan      html  css  js  c++  java
  • D. Fragmentation merging 题解(思维)

    题目链接

    题目思路

    其实自己写出来了,但是写法实在是太搓了

    其实现在问题就变成选取两个不相交的子区间(A,B)((A,B)可有一个为空)使得(C=A|B)

    注意(C)为一个集合

    (C)中的元素排完序后都是连续的,那么ans++

    直接枚举([L,R])作为(C)判断是否合法

    我们就需要判断这些元素所在的位置是否所在的段小于等于两个段

    然后利用双指针和并查集的思想写

    (n=1)特判

    代码

    #include<bits/stdc++.h>
    #define fi first
    #define se second
    #define debug cout<<"I AM HERE"<<endl;
    using namespace std;
    typedef long long ll;
    const int maxn=5e3+5,inf=0x3f3f3f3f,mod=1e9+7;
    const double eps=1e-6;
    int n;
    int a[maxn],b[maxn];
    bool vis[maxn];
    signed main(){
        int _;scanf("%d",&_);
        while(_--){
            scanf("%d",&n);
            for(int i=1;i<=n;i++){
                scanf("%d",&a[i]);
                b[a[i]]=i;
            }
            if(n==1){
                printf("0
    ");
                continue;
            }
            int ans=0;
            for(int i=1;i<=n;i++){
                memset(vis,0,sizeof(vis));
                int cnt=0;
                for(int j=i;j<=n;j++){
                    int pos=b[j];
                    vis[pos]=1;
                    if(vis[pos-1]&&vis[pos+1]){
                        cnt--;
                    }else if(!vis[pos-1]&&!vis[pos+1]){
                        cnt++;
                    }
                    if(cnt<=2) ans++;
                }
            }
            printf("%d
    ",ans);
        }
        return 0;
    }
    
    
    不摆烂了,写题
  • 相关阅读:
    go相关
    mac下使用vscode技巧
    mac下secureCRT的使用技巧
    python subprocess实时输出
    python中多级目录导入模块问题
    python的print与sys.stdout
    python中类相关笔记
    python中sys.stdout.flush()的作用
    nginx+uwsgi配置
    虚拟机的 基本配置
  • 原文地址:https://www.cnblogs.com/hunxuewangzi/p/15239444.html
Copyright © 2011-2022 走看看