CodeForce 264 A. Escape from Stones

A. Escape from Stones
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Squirrel Liss lived in a forest peacefully, but unexpected trouble happens. Stones fall from a mountain. Initially Squirrel Liss occupies an interval [0, 1]. Next, n stones will fall and Liss will escape from the stones. The stones are numbered from 1 to n in order.

The stones always fall to the center of Liss's interval. When Liss occupies the interval [k - d, k + d] and a stone falls to k, she will escape to the left or to the right. If she escapes to the left, her new interval will be [k - d, k]. If she escapes to the right, her new interval will be [k, k + d].

You are given a string s of length n. If the i-th character of s is "l" or "r", when the i-th stone falls Liss will escape to the left or to the right, respectively. Find the sequence of stones' numbers from left to right after all the n stones falls.

Input

The input consists of only one line. The only line contains the string s (1 ≤ |s| ≤ 106). Each character in s will be either "l" or "r".

Output

Output n lines — on the i-th line you should print the i-th stone's number from the left.

Sample test(s)
input
llrlr
output
3
5
4
2
1
题目链接http://codeforces.com/contest/264/problem/A

 1 #include <iostream>
 2 #include <cstdlib>
 3 #include <cstdio>
 4 #include <cstring>
 5 
 6 using namespace std;
 7 
 8 char s[1000000+10];
 9 
10 int main(void)
11 {
12     //freopen("cf264.in", "r", stdin);
13     scanf("%s", s+1);
14     int len = strlen(s+1);
15     int a[len];
16     int left = 1, right = len; 
17     for (int i = 1; i <= len; ++i)
18     {
19         if (s[i] == 'l')
20             a[right--] = i;
21         else a[left++] = i; 
22     }
23     for (int i = 1; i <= len; ++i)
24         printf("%d\n", a[i]);
25 
26     return 0;
27 }

这道题目不能用暴力做,因为精度不够!看看人家的代码,短小精悍,这题目首先要想明白,抓住问题的关键!

对了,这里有一个小的trick,输入字符串的时候用的scanf("%s", s + 1);这样,以后用的时候就可以通过s[k]访问第k个字符了。但是,以后计算字符串长度的时候得用strlen(s+1)……

下面是暴力的代码,直接用数学做……不能过

 1 #include <iostream>
 2 #include <cstdlib>
 3 #include <cmath>
 4 #include <cstdio>
 5 #include <cstring>
 6 
 7 using namespace std;
 8 char str[1000000+10];
 9 long double b[1000000+10];
10 struct stone
11 {
12     long double data;
13     int index;
14 }c[1000000+10];
15 
16 int cmp(const void *a, const void *b)
17 {
18     return (*(stone*)a).data >  (*(stone*)b).data ? 1 : -1;
19 }
20 
21 int main(void)
22 {
23 //    freopen("cf264.in", "r", stdin);
24     scanf("%s", str);
25     b[0] = 0.0;
26     b[1] = 0.5;
27     for (int i = 0; str[i] != '\0'; ++i)
28     {
29         if (str[i] == 'l')
30             b[i+2] = b[i+1] - (fabs(b[i+1]-b[i]) * 0.5);
31         else b[i+2] = b[i+1] + (fabs(b[i+1]-b[i]) * 0.5);
32     }
33     int len = strlen(str);
34     for (int i = 1; i < len + 1; ++i)
35     {
36         c[i-1].index = i;
37         c[i-1].data = b[i];
38     }
39     qsort(c, len, sizeof(c[0]), cmp);
40     for (int i = 0; i < len; ++i)
41     {
42         printf("%d\n", c[i].index);
43     }
44 
45     return 0;
46 }

其中用到了结构体排序。

原文地址:https://www.cnblogs.com/liuxueyang/p/2869373.html