[关闭]
@ysner 2018-11-07T03:10:55.000000Z 字数 974 阅读 1471

noip2011选择客栈

暴力


题面

戳我

解析

一开始想了一个读入完再处理的暴力方法,比较麻烦。

其实可以考虑增量地看待这个问题。
如果当前客栈,则前面所有同色客栈都可为作出的贡献。
否则,就是上一个的客栈出现前,前面所有同色客栈的数量。

所以我们对每种颜色统计两个量:

每当碰到一个时,可以把所有更新为
然后每次答案加上同色的即可。

然而有个细节,在时,当前点对答案没贡献,对有贡献。
复杂度
也可以优化到

  1. #include<cstdio>
  2. #include<cstdlib>
  3. #include<cmath>
  4. #include<cstring>
  5. #include<algorithm>
  6. #include<queue>
  7. #define ll long long
  8. #define re register
  9. #define il inline
  10. #define fp(i,a,b) for(re int i=a;i<=b;++i)
  11. #define fq(i,a,b) for(re int i=a;i>=b;--i)
  12. using namespace std;
  13. const int N=55;
  14. int n,k,p,num[N],las[N],now;
  15. ll ans;
  16. il ll gi()
  17. {
  18. re ll x=0,t=1;
  19. re char ch=getchar();
  20. while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
  21. if(ch=='-') t=-1,ch=getchar();
  22. while(ch>='0'&&ch<='9') x=x*10+ch-48,ch=getchar();
  23. return x*t;
  24. }
  25. il int min(re int x,re int y){return x<y?x:y;}
  26. int main()
  27. {
  28. n=gi();k=gi();p=gi();
  29. fp(i,1,n)
  30. {
  31. re int col=gi(),val=gi();
  32. if(val<=p)
  33. fp(j,0,k-1) las[j]=num[j];
  34. ans+=las[col];
  35. if(val<=p) ++las[col];
  36. ++num[col];
  37. }
  38. printf("%lld\n",ans);
  39. return 0;
  40. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注