洛谷P3514 [POI 2011] LIZ-Lollipop(思维题)

题目

思路来源

乱搞ac

题解

注意到一个区间两个端点都是2的话,此时和是sum,并且只能删掉一个2,那么sum-1一定拼不出来

那么如果一个区间两个端点有一个端点是1的话,此时和是sum,那么1,sum都能拼出来

不妨左端点是1,右端点是2,首先sum可以拼出来,删左端点就能拼出sum-1,删右端点就能拼出sum-2,然后删掉右端点,递归的考虑1,sum-2

不妨左端点是1,右端点是1,首先sum可以拼出来,删右端点就能拼出sum-1,然后删掉右端点,递归的考虑1,sum-1

数学归纳可知,1,sum都能拼出来

代码

cpp 复制代码
#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<vector>
#include<map>
#include<set>
using namespace std;
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define per(i,a,b) for(int i=(a);i>=(b);--i)
typedef long long ll;
typedef double db;
typedef pair<ll,ll> P;
typedef array<ll,3> A;
#define fi first
#define se second
#define pb push_back
#define dbg(x) cerr<<(#x)<<":"<<x<<" ";
#define dbg2(x) cerr<<(#x)<<":"<<x<<endl;
#define SZ(a) (int)(a.size())
#define sci(a) scanf("%d",&(a))
#define pt(a) printf("%d",a);
#define pte(a) printf("%d\n",a)
#define ptlle(a) printf("%lld\n",a)
const int N=1e6+10,M=2e6+10;
int n,m,sum,v;
char s[N];
P ok[M];
int main(){
    sci(n),sci(m);
    scanf("%s",s+1);
    int l=1,r=n,fir=0,sec=0;
    rep(j,1,n){//TT
        if(s[j]=='T')sum+=2;
        else sum++;
        if(!fir && s[j]=='W')fir=j;
        if(s[j]=='W')sec=j;
    }
    ok[sum]=P(l,r);
    while(l<=r){
        if(s[l]==s[r]){
            if(s[l]=='T'){
                sum-=2;
                if(!sec)l++;
                else if(fir<n-sec+1)l++;
                else r--;
            }
            else{
                sum--,l++;
            }
        }
        else{
            if(s[l]=='T'){
                ok[sum-1]=P(l,r-1);
                sum-=2;
                l++;
            }
            else{
                ok[sum-1]=P(l+1,r);
                sum-=2;
                r--;
            }
        }
        ok[sum]=P(l,r);
    }
    while(m--){
        sci(v);
        if(ok[v]==P(0,0))puts("NIE");
        else printf("%d %d\n",ok[v].fi,ok[v].se);
    }
    return 0;
}
相关推荐
lsylalalala1 小时前
常见的排序算法1
数据结构·算法·排序算法
想吃火锅10052 小时前
【leetcode】54. 螺旋矩阵
算法·leetcode·矩阵
想吃火锅10052 小时前
【leetcode】300. 最长递增子序列
算法·leetcode·职场和发展
imaol12 小时前
数据结构---队列
java·数据结构·算法
数模竞赛Paid answer2 小时前
2026年电工杯数学建模A题绿电直连型电氢氨园区优化运行求解全过程论文及程序
算法·数学建模·电工杯
疯狂打码的少年2 小时前
【数据结构】串的模式匹配:KMP算法(重点)
数据结构·笔记·算法
晚风醉蝶4 小时前
第零篇-算法全景图
算法
疯狂打码的少年4 小时前
【数据结构】树的基本概念与二叉树定义
java·数据结构·笔记·算法
webmote334 小时前
用 NVIDIA Nemotron 3 Super + .NET 构建有记忆的多轮对话
后端·算法
Keven_114 小时前
算法札记:区分稀疏图与稠密图在经验上的数学计算差异
算法·稠密图·稀疏图