洛谷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]都能拼出来

代码

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小衣同学

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值