洛谷P1311 选择客栈(NOIP 2011)

题目描述 https://www.luogu.org/problemnew/show/P1311
这个题n的范围很大,所以O(kn^)是肯定不行的 ,由于k的范围很小,所以可以枚举每个客栈,记录这个色调上个客栈的位置、这个色调客栈的数量,以及当前最后的价钱符合条件的位置,每次枚举相加就好了。

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int N=55;
int n,k,p,now,last[N],cnt[N],sum[N],ans;
int main() 
{ 
   scanf("%d%d%d",&n,&k,&p);
   for(int i=1;i<=n;i++)
   {
   	  int col,price;
   	  scanf("%d%d",&col,&price);
   	  if(price<=p) now=i;
   	  if(last[col]<=now) sum[col]=cnt[col];
   	  last[col]=i;
   	  ans+=sum[col];
   	  cnt[col]++;
   }
   printf("%d",ans);   
 	
  return 0;	
}```

猜你喜欢

转载自blog.csdn.net/qq_42920122/article/details/88724083