#include <stdio.h>
#include <stdlib.h>
/*单调栈 旋转寿司
3 15 6 14
3 21 9 17
*/
int main()
{
int i = 0;
int len = 0;
int data = 0;
int nums[501];
char c ;
while(scanf("%d",&nums[i]) == 1)
{
i++;
len++;
c = getchar();
if(c == '\n')
break;
}
int *out = NULL;
int *stack = NULL;
int top = 0;
int flag = 1;
out = (int *)malloc(len*sizeof(int));
stack = (int *)malloc(len*sizeof(int));
for(i=0; i<len; i++)//这个for只能确定数组右边有没有比它小的数
{
if(i == 1)
{
stack[top] = i;
top++;
}
else
{
while(top>0 && (nums[i]< nums[stack[top-1]]))//出栈的操作
{
out[stack[top-1]] = nums[i] + nums[stack[top-1]];
top--;
}
stack[top] = i;//进栈
top++;
}
}
while(top>1)//剩下stack里面的下标对应的数 是从小到大排序的,所以栈顶的下一个最小值一定在栈底
{
out[stack[top-1]]= nums[stack[top-1]] + nums[stack[0]];//每个栈顶都出栈
top--;
}
out[stack[0]]= nums[stack[0]];
for(i=0; i<len; i++)
{
printf("%d;",out[i]);
}
return 0;
}
12-22
2844
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
08-18
217
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
08-14
723
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
01-07