Partial Teacher
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.
Examples
Input
5 LRLR
Output
2 1 2 1 2
Input
5 =RRR
Output
1 1 2 3 4
题意:一个老师给 n 个学生发糖,假设第 i 个学生得到的糖果数为 a[i] ... 给一个长度为 n - 1 的字符串(下标从1开始),第 i 个字符为 R 代表a[i + 1] > a[i],L代表 a[i] > a[i + 1], = 代表 a[i] == a[i + 1] ,,每个人至少得到一个糖果,输入每个人的糖果数量,要求使用总糖果数最少。
思路:题目的条件其实是 不等式,转化一下可以有
R : a[i + 1] - a[i] >= 1,L : a[i] - a[i + 1] >= 1 , = : a[i] - a[i + 1] >= 0 && a[i + 1] - a[i] >= 0
那么可以差分约数,转化为最短路问题。
R :建立一条 i + 1 到 i 边权为 -1 的边。L:建立一条 i 到 i + 1 边权为 -1 的边。=:建立双向边,边权为 0
那么 u 到 v 的最短路表示 a[u] - a[v] 的最大差值
PS:由于某些点之间没有联系,所以需要跑若干遍spfa,最后输入 -dis[i] + 1 即是 answer[i]
Code:
#include<bits/stdc++.h>
#define debug(x) cout << "[" << #x <<": " << (x) <<"]"<< endl
#define pii pair<int,int>
#define clr(a,b) memset((a),b,sizeof(a))
#define rep(i,a,b) for(int i = a;i < b;i ++)
#define pb push_back
#define MP make_pair
#define LL long long
#define ull unsigned LL
#define ls i << 1
#define rs (i << 1) + 1
#define fi first
#define se second
#define CLR(a) while(!(a).empty()) a.pop()
using namespace std;
const int maxn = 1e3 + 10;
const int inf = 0x3f3f3f3f;
int head[maxn],cnt = 0;
char str[maxn];
struct xx{
int v,w,nex;
xx(int v = 0,int w = 0,int nex = 0):
v(v),w(w),nex(nex){}
}edge[maxn << 2];
int dis[maxn],vis[maxn];
void spfa(int s){
dis[s] = 0;
queue<int> Q;
Q.push(s); vis[s] = 1;
while(!Q.empty()){
int now = Q.front();
Q.pop(); vis[now] = 0;
for(int i = head[now]; ~i;i = edge[i].nex){
int v = edge[i].v,w = edge[i].w;
if(dis[v] > dis[now] + w){
dis[v] = dis[now] + w;
if(!vis[v]){
vis[v] = 1;
Q.push(v);
}
}
}
}
}
void init(){
clr(head,-1); cnt = 0;
clr(dis,inf); clr(vis,0);
}
int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
int n;
while(~scanf("%d",&n)){
init();
scanf("%s",str + 1);
for(int i = 1;i <= n - 1;++ i){
if(str[i] == 'L'){
edge[cnt] = xx(i,-1,head[i + 1]);
head[i + 1] = cnt ++;
}
else if(str[i] == 'R'){
edge[cnt] = xx(i + 1,-1,head[i]);
head[i] = cnt ++;
}
else {
edge[cnt] = xx(i,0,head[i + 1]);
head[i + 1] = cnt ++;
edge[cnt] = xx(i + 1,0,head[i]);
head[i] = cnt ++;
}
}
for(int i = 1;i <= n;++ i)
if(dis[i] == inf)
spfa(i);
// for(int i = 1;i <= n;++ i)
// debug(dis[i]);
for(int i = 1;i <= n;++ i)
printf("%d ",-dis[i] + 1);
printf("\n");
}
return 0;
}