博客
关于我
BZOJ 1106 [POI2007]立方体大作战tet
阅读量:270 次
发布时间:2019-03-01

本文共 1104 字,大约阅读时间需要 3 分钟。

题目链接

思路

贪心,如果出现了下面情况:

1 2 2 1

那么显然先合并22,再合并11.

如果是下面的情况:

1 2 1 2

那么合并11和合并22的顺序是无关紧要的。

一遍从左扫到右,找到两个相同点就合并,用树状数组维护一下就好了。

代码

#include 
const int maxn=100000;int read(){ int x=0,f=1; char ch=getchar(); while((ch<'0')||(ch>'9')) { if(ch=='-') { f=-f; } ch=getchar(); } while((ch>='0')&&(ch<='9')) { x=x*10+ch-'0'; ch=getchar(); } return x*f;}int n,a,ans,pre[maxn+10];namespace tree_array{ int c[maxn+10]; inline int lowbit(int x) { return x&(-x); } inline int add(int pos,int x) { while(pos<=n) { c[pos]+=x; pos+=lowbit(pos); } return 0; } inline int sum(int pos) { int res=0; while(pos) { res+=c[pos]; pos-=lowbit(pos); } return res; }}int main(){ n=read()<<1; for(register int i=1; i<=n; ++i) { a=read(); if(!pre[a]) { pre[a]=i; tree_array::add(i,1); } else { ans+=tree_array::sum(i)-tree_array::sum(pre[a]-1)-1; tree_array::add(pre[a],-1); } } printf("%d\n",ans); return 0;}

转载地址:http://aywo.baihongyu.com/

你可能感兴趣的文章
Nginx+Keepalived实现简单版高可用主备切换
查看>>
nginx+mysql+redis+mongdb+rabbitmq 自动化部署脚本
查看>>
nginx+php的搭建
查看>>
nginx+tomcat+memcached
查看>>
Nginx+Tomcat实现动静分离
查看>>
nginx+Tomcat性能监控
查看>>
nginx+uwsgi+django
查看>>
nginx+vsftp搭建图片服务器
查看>>
Nginx-http-flv-module流媒体服务器搭建+模拟推流+flv.js在前端html和Vue中播放HTTP-FLV视频流
查看>>
nginx-vts + prometheus 监控nginx
查看>>
Nginx/Apache反向代理
查看>>
Nginx: 413 – Request Entity Too Large Error and Solution
查看>>
nginx: [emerg] getpwnam(“www”) failed 错误处理方法
查看>>
nginx: [emerg] the “ssl“ parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf:
查看>>
nginx:Error ./configure: error: the HTTP rewrite module requires the PCRE library
查看>>
Nginx、HAProxy、LVS
查看>>
Nginx下配置codeigniter框架方法
查看>>
Nginx中使用expires指令实现配置浏览器缓存
查看>>
nginx中配置root和alias的区别
查看>>
nginx主要流程(未完成)
查看>>