P6704 [COCI2010-2011#7] GITARA(栈)

P6704 [COCI2010-2011#7] GITARA

题目传送门

解题思路

这题可以利用的思想

每一种弦都用一种栈(本人太蒟,不会用STL)

如果有数进来时

就和栈顶比较大小

如果栈顶>进来的数

就要出栈

注意:

当1 5 1 6 1 5时,虽然已经按过1 5,但是也要先松开1 6的键,然后可以不用按1 5 (用其他方法的小朋友们!敲黑板!做笔记!)

AC代码(有注释)

#include<iostream>
#include<cstdio>
using namespace std;
int n,p,x,y,s,sum[7],a[7][300005];
int main()
{
    
    
	scanf("%d%d",&n,&p);
	for(int i=1;i<=n;i++)
	{
    
    
		scanf("%d%d",&x,&y);
		while(sum[x]>=1&&a[x][sum[x]]>y)//判断栈是否空,还有比较栈顶
		{
    
    
			sum[x]--;//减减
			s++;//累加次数	
		}
		if(a[x][sum[x]]==y)continue;//如果与栈顶相同就跳过
		a[x][++sum[x]]=y;//入栈
		s++;//累加次数
	} 
	printf("%d",s);//输出次数
	return 0;
} 

谢谢

猜你喜欢

转载自blog.csdn.net/weixin_45524309/article/details/108541871