B - Escape from Stones CodeForces - 264A

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.

Examples
Input
llrlr
Output
3
5
4
2
1
Input
rrlll
Output
1
2
5
4
3
Input
lrlrr
Output
2
4
5
3
1
Note
In the first example, the positions of stones 1, 2, 3, 4, 5 will be , respectively. So you should print the sequence: 3, 5, 4, 2, 1.

补一补昨晚的训练题。挺长时间没有写博客了,心里满满的负罪感。这个题目的意思不难理解,就是一个简单的二分。我一开始就是按照题目的要求,一点一点的分下去,样例过了,但是提交一发就wa了。我想了想,数据量是1e6╮(╯﹏╰)╭就一个数,分1e6次也会接近零,试了试long double,还是不行。就想新的方法了。还有要注意的就是cin和cout超时,在这儿又wa了一发。
代码如下:

#include<bits/stdc++.h>
using namespace std;

const int maxx=1e6+10;
char a[maxx];
struct node{
	long double pos;
	int ant;
}p[maxx];

int cmp(const node &a,const node &b)
{
	return a.pos<b.pos;
}

int main()
{
	while(scanf("%s",a)!=EOF)
	{
		int len=strlen(a);
		int maxx=len;
		int minn=1;
		for(int i=0;i<len;i++)
		{
			if(a[i]=='l')
			{
				p[i].pos=maxx--;
				p[i].ant=i+1;
			}
			else
			{
				p[i].pos=minn++;
				p[i].ant=i+1;
			}
		}
		sort(p,p+len,cmp);
		for(int i=0;i<len;i++)
		printf("%d\n",p[i].ant);
		//cout<<p[i].ant<<endl;
	}
}

努力加油a啊,(o)/~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
这段代码中有几个问题: 1. while 循环的判断条件应该是 `stones.length > 1`,因为只有至少有两个石头时才需要进行破碎的操作。所以应该将 `while( stones.length==1 || stones.length==0 )` 修改为 `while( stones.length > 1 )`。 2. 在交换石头的位置时,应该使用临时变量来保存一个石头的值,然后再进行交换。所以应该将 `a=stones[j];` 修改为 `int temp = stones[i];`,将 `stones[i]=stones[j];` 修改为 `stones[i]=stones[j];`,将 `stones[j]=a;` 修改为 `stones[j]=temp;`。 3. 在判断最后剩下的石头重量时,应该使用索引 `stones.length-1` 和 `stones.length-2` 来获取最后两个石头的重量。所以应该将 `if(stones[stones.length-1]<=stones[stones.length])` 修改为 `if(stones[stones.length-1]<=stones[stones.length-2])`,将 `stones.length=stones.length-2;` 修改为 `stones.length -= 2;`。 4. 最后返回的应该是 `stones[0]`,而不是 `stones[stones.length]`。所以应该将 `return stones[stones.length];` 修改为 `return stones[0];`。 修正后的代码如下: ```java class Solution { public int lastStoneWeight(int[] stones) { while (stones.length > 1) { for (int i = 0; i < stones.length; i++) { for (int j = 1; j < stones.length; j++) { if (stones[i] > stones[j]) { int temp = stones[i]; stones[i] = stones[j]; stones[j] = temp; } } } if (stones[stones.length - 1] <= stones[stones.length - 2]) { stones[stones.length - 2] -= stones[stones.length - 1]; stones = Arrays.copyOf(stones, stones.length - 1); } else { stones = Arrays.copyOf(stones, stones.length - 2); } } return stones[0]; } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

starlet_kiss

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

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

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

打赏作者

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

抵扣说明:

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

余额充值