Codeforces 67A Partial Teacher(模拟)

Codeforces 67A Partial Teacher(模拟)

链接:


题目

Time Limit:1000MS Memory Limit:262144KB
Description
A teacher decides to give toffees to his students. He asks n students to stand in a queue. Since the teacher is very partial, he follows the following rule to distribute toffees.

He looks at the first two students and gives more toffees to the student having higher marks than the other one. If they have the same marks they get the same number of toffees. The same procedure is followed for each pair of adjacent students starting from the first one to the last one.

It is given that each student receives at least one toffee. You have to find the number of toffees given to each student by the teacher such that the total number of toffees is minimum.

Input
The first line of input contains the number of students n(2≤n≤1000). The second line gives (n - 1) characters consisting of “L”,”R” and “=”. For each pair of adjacent students “L” means that the left student has higher marks, “R” means that the right student has higher marks and “=” means that both have equal marks.

Output
Output consists of n integers separated by a space representing the number of toffees each student receives in the queue starting from the first one to the last one.

Sample Input
5
LRLR
Sample Output
2 1 2 1 2
Sample Input
5
=RRR
Sample Output
1 1 2 3 4


题意

给n个学生分配糖果,L、=和R代表两个学生之间的重要性关系,相邻学生要保证重要性高的获得的糖果更多。


分析

直接遍历一下逐个比较大小关系即可。需要向两端进行拓展。本体数据范围不大,也可以直接暴力模拟,循环100000次,每次比较两个之间大小即可。


源码

  • 暴力模拟
#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#include<vector>
#include<algorithm>
#include<string>
#include<cmath>
#include<set>
#include<map>
#include<vector>
#include<stack>
#include<utility>
#include<sstream>
#define mem0(x) memset(x,0,sizeof x)
#define mem1(x) memset(x,1,sizeof x)
#define mem11(x) memset(x,-1,sizeof x)

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int INF = 0x7fffffff;
const int MAXN = 1e6+10;
const int MOD = 1000000007;

string s;
int n;
int num[1010];

void pr(){
    for(int i = 0; i < n; ++i){
        if(i!=0)
            cout << " ";
        cout << num[i]+1;
    }
    cout << endl;
}

int main(){
    cin >> n;
    cin >> s;
    memset(num,0,sizeof num);
    //pr();
    for(int k = 1; k < 100010; ++k){
        for(int i = 0; i < n-1; ++i){
            if(s[i] == 'L'){
                while(num[i]<=num[i+1]){
                    num[i]++;
                }
            }
            else if(s[i] == 'R'){
                while(num[i]>=num[i+1]){
                    num[i+1]++;
                }
            }
            else if(s[i] == '='){
                while(num[i]!=num[i+1]){
                    if(num[i]<num[i+1])
                        num[i]++;
                    else if(num[i]>num[i+1])
                        num[i+1]++;
                }
            }
        }
    }
    pr();
    return 0;
}
  • 建立伪指针向两边拓展
#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#include<vector>
#include<algorithm>
#include<string>
#include<sstream>
#include<cmath>
#include<set>
#include<map>
#include<vector>
#include<stack>
#include<utility>
#include<sstream>
#define mem0(x) memset(x,0,sizeof x)
#define mem1(x) memset(x,1,sizeof x)
#define mem11(x) memset(x,-1,sizeof x)
#define dbug cout<<"here"<<endl;
//#define LOCAL

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int INF = 0x7fffffff;
const int MAXN = 1e6+10;
const int MOD = 1000000007;

int a[1010];

int main(){
    #ifdef LOCAL
        freopen("C:\\Users\\asus-z\\Desktop\\input.txt","r",stdin);
        freopen("C:\\Users\\asus-z\\Desktop\\output.txt","w",stdout);
    #endif
    int n;
    string s;
    cin >> n >> s;
    for (int i=0;i<n;++i){
        int l=i-1,r=i;
        int ans1=1,ans2=1;
        while (l>=0 && s[l]!='L'){
            if (s[l]=='R')
                ++ans1;
            --l;
        }
        while (r<n-1 && s[r]!='R'){
            if (s[r]=='L')
                ++ans2;
            ++r;
        }
        cout << max(ans1,ans2) << " ";
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值