【codeforces 67A】Partial Teacher

【题目链接】:http://codeforces.com/problemset/problem/67/A

【题意】

给一个长度为n-1的字符串;
每个字符串是’L’,’R’,’=’这3种字符中的一个;
分别表示第i个元素比第i+1个元素大,小,相等;
让你构造出这么一个长度为n的序列;
使得每个位置上的数字的和最小;
(每个数字最少为1);

【题解】

ans[1]=1;
然后对于每一个位置;
“相等”->ans[i+1]=ans[i];
“R”->ans[i+1] = ans[i]+1;
“L”->
则先让ans[i+1]最小=1
然后看看ans[i]是不是比ans[i+1]来得大;
是的话就继续;
否则的话进行调整;
调整的步骤是;
先令ans[i]+1;
从前一步往前扫描;
如果s[j]==’=’则ans[j]=ans[j+1];
如果s[j]==’l’则如果ans[j]>ans[j+1] 则结束,否则ans[j]++;
如果s[j]==’R”则结束;(因为可以看到,我们在调整的时候都是至少让ans[j]递增的;所以s[j]==’R’的是肯定满足的;

【Number Of WA

1

【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)
#define Open() freopen("F:\\rush.txt","r",stdin)
#define Close() ios::sync_with_stdio(0),cin.tie(0)

typedef pair<int,int> pii;
typedef pair<LL,LL> pll;

const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 1100;

int f[N],n;
char s[N];

int main(){
    //Open();
    Close();//scanf,puts,printf not use
    //init??????
    cin >> n;
    cin >> (s+1);
    f[1] = 1;
    rep1(i,1,n-1){
        if (s[i]=='='){
            f[i+1] = f[i];
        }
        else
            if (s[i]=='R'){
                f[i+1] = f[i]+1;
            }
            else{
                //s[i]=='L'
                f[i+1] = 1;
                if (f[i]>f[i+1])
                    continue;
                else
                {
                    f[i]++;
                    rep2(j,i-1,1){
                        if (s[j]=='R') break;
                        if (s[j]=='L'){
                            if (f[j]==f[j+1])
                                f[j]++;
                            else
                                break;
                        }
                        else
                            f[j] = f[j+1];
                    }
                }
            }
    }

    rep1(i,1,n)
        cout << f[i] << (i==n?'\n':' ');
    return 0;
}
posted @ 2017-10-04 18:44  AWCXV  阅读(190)  评论(0编辑  收藏  举报