一些神奇的(优化)板子——来自Loi_black的博客
deque<int>q;
void spfa(int s)
{
for(int i=;i<=n;i++)
d[i]=1e9;
d[s]=;
q.push_back(s);
used[s]=;
while(!q.empty())
{
int x=q.front();
q.pop_front();
used[x]=;
for(int i=first[x];i;i=next[i])
{
int u=hh[i].t;
if(d[u]>d[x]+hh[i].c)
{
d[u]=d[x]+hh[i].c;
if(!used[u])
{
used[u]=;
if(!q.empty())
{
if(d[u]<d[q.front()])
q.push_front(u);
else
q.push_back(u);
}
else
q.push_back(u);
}
}
}
}
}
//spfa+(slf优化)
int tim[maxn];
bool spfa(int s)
{
d[s]=;
q.push(s);
used[s]=;
while(!q.empty())
{
int x=q.front();
q.pop();
used[x]=;
for(int i=first[x];i;i=next[i])
{
int u=hh[i].t;
if(d[u]>d[x]+hh[i].c)
{
d[u]=d[x]+hh[i].c;
if(!used[u])
{
if(++tim[u]>n)
return false;
q.push(u);
used[u]=;
}
}
}
}
return true;
} //spfa判负环
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<queue>
using namespace std;
const int maxn=;
struct dqs
{
int f,t,c;
}hh[maxn];
struct dqm
{
int num,dis;
};
bool operator <(dqm a,dqm b)
{
return a.dis>b.dis;
}
int tot=,first[maxn],next[maxn],d[maxn];
bool used[maxn];
void build(int f,int t,int c)
{
hh[++tot]=(dqs){f,t,c};
next[tot]=first[f];
first[f]=tot;
}
priority_queue<dqm>q;
void dij(int s)
{
d[s]=;
q.push({s,d[s]});
while(!q.empty())
{
int head = q.top().num;
q.pop();
used[head]=;
for(int i=first[head];i;i=next[i])
{
int u=hh[i].t;
if(d[u]>d[head]+hh[i].c)
{
d[u]=d[head]+hh[i].c;
if(!used[u])
q.push((dqm){u,d[u]});
}
}
}
}
int main()
{
int n,m,s,e;
scanf("%d%d%d%d",&n,&m,&s,&e);
......
}
//dijkstra+ 堆
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn=;
struct dqs
{
int f,t,c;
}hh[maxn<<];
int tot=,fa[maxn][],next[maxn],first[maxn],f[maxn],d[maxn];
void build(int ff,int tt,int cc)
{
hh[++tot]=(dqs){ff,tt,cc};
next[tot]=first[ff];
first[ff]=tot;
}
int deep[maxn];
void dfs(int x,int sd)
{
deep[x]=sd;
int u;
for(int i=first[x];i;i=next[i])
{
u=hh[i].t;
if(!deep[u]&&u)
{
f[u]=x;
d[u]=d[x]+hh[i].c;
dfs(u,sd+);
}
}
}
int lca(int x,int y)
{
if(deep[x]<deep[y])
swap(x,y);
int deepcha=deep[x]-deep[y];
for(int i=;i<=;i++)
{
if(<<i&deepcha)
x=fa[x][i];
}
for(int i=;i>=;i--)
{
if(fa[x][i]!=fa[y][i])
{
x=fa[x][i];
y=fa[y][i];
}
}
if(x!=y)
return f[x];
return x;
}
int main()
{
int n;
scanf("%d",&n);
int u,v,c;
for(int i=;i<n;i++)
{
scanf("%d%d%d",&u,&v,&c);
build(u,v,c);
build(v,u,c);
}
dfs(,);
for(int i=;i<n;i++)
fa[i][]=f[i];
for(int j=;j<=;j++)
for(int i=;i<=n;i++)
fa[i][j]=fa[fa[i][j-]][j-];
int m;
scanf("%d",&m);
for(int i=;i<=m;i++)
{
scanf("%d%d",&u,&v);
int xx=lca(u,v);
printf("%d\n",d[u]+d[v]-*d[xx]);
}
return ;
} //倍增lca
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<queue>
using namespace std;
const int maxn=;
struct dqs
{
int f,t,c;
}hh[maxn];
int tot=,first[maxn],next[maxn],du[maxn];
void build(int f,int t)
{
hh[++tot]=(dqs){f,t};
next[tot]=first[f];
first[f]=tot;
}
queue<int>q;
void tp()
{
while(!q.empty())
{
int x=q.front();
q.pop();
printf("%d ",x);
for(int i=first[x];i;i=next[i])
{
int u=hh[i].t;
du[u]--;
if(!du[u])
q.push(u);
}
}
}
int main()
{
int n;
scanf("%d",&n);
for(int i=;i<=n;i++)
{
int x;
while(true)
{
scanf("%d",&x);
if(x==)
break;
build(i,x);
du[x]++;
}
}
for(int i=;i<=n;i++)
if(!du[i])
q.push(i);
tp();
return ;
}
//拓扑排序
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
const int maxn=;
struct dqs
{
int f,t;
}hh[maxn];
int tot=;
int first[maxn],next[maxn];
void build(int f,int t)
{
hh[++tot]=(dqs){f,t};
next[tot]=first[f];
first[f]=tot;
}
int dfn[maxn],low[maxn],stack[maxn],size[maxn],du[maxn],jlqlt[maxn];
bool in_stack[maxn];
int tot1=,cnt=,snum=;
void group(int x)
{
dfn[x]=low[x]=++tot1;
stack[++snum]=x;
in_stack[x]=;
for(int i=first[x];i;i=next[i])
{
int u=hh[i].t;
if(!dfn[u])
{
group(u);
low[x]=min(low[x],low[u]);
}
else if(in_stack[u])
low[x]=min(low[x],dfn[u]);
}
if(dfn[x]==low[x])
{
cnt++;
while(true)
{
jlqlt[stack[snum]]=cnt;
in_stack[stack[snum]]=;
size[cnt]++;
snum--;
if(stack[snum+]==x)
break;
}
}
}
int main()
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++)
{
int a,b;
scanf("%d%d",&a,&b);
build(a,b);
}
for(int i=;i<=n;i++)
if(!dfn[i])
group(i);
for(int i=;i<=n;i++)
for(int j=first[i];j;j=next[j])
{
int u=hh[j].t;
if(jlqlt[i]!=jlqlt[u])
du[jlqlt[i]]++;
}
int sum1=,sum2=,x;
for(int i=;i<=cnt;i++)
{
if(size[i]>)
sum1++;
if(!du[i])
{
sum2++;
x=i;
}
}
printf("%d\n",sum1);
if(sum2==&&size[x]!=)
{
for(int i=;i<=n;i++)
{
if(jlqlt[i]==x)
printf("%d ",i);
}
}
else
printf("-1\n");
return ;
} //trajan
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
const int maxn=;
long long tmp[maxn],a[maxn];
long long ans=;
void merge(int l,int mid,int r)
{
int i=l,j=mid+,k=l;
while(i<=mid&&j<=r)
{
if(a[i]>a[j])
{
tmp[k++]=a[j++];
ans+=mid+-i;
}
else
tmp[k++]=a[i++];
}
while(i<=mid)
tmp[k++]=a[i++];
while(j<=r)
tmp[k++]=a[j++];
for(int i=l;i<=r;i++)
a[i]=tmp[i];
}
void merge_sort(int l,int r)
{
if(l<r)
{
int mid=(l+r)>>;
merge_sort(l,mid);
merge_sort(mid+,r);
merge(l,mid,r);
}
}
int main()
{
int n;
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%lld",&a[i]);
merge_sort(,n);
for(int i=;i<=n;i++)
cout<<a[i]<<" ";
cout<<endl;
printf("%lld",ans);
} //归并排序
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
const int maxn=;
int heap[maxn],cnt=;
void push(int x)
{
cnt++;
int now=cnt;
heap[now]=x;
while(now>)
{
if(heap[now]<heap[now/])
{
swap(heap[now],heap[now/]);
now/=;
}
else break;
}
}
void pop()
{
heap[]=heap[cnt];
int now=;
while(now*+<=cnt)
{
int l=now*,r=now*+;
if(heap[l]<heap[now])
{
if(heap[r]<heap[l])
swap(l,r);
swap(heap[l],heap[now]);
now=l;
}
else if(heap[r]<heap[now])
{
swap(heap[r],heap[now]);
now=r;
}
else break;
}
cnt--;
}
int main()
{
int n;
scanf("%d",&n);
for(int i=;i<=n;i++)
{
int x;
scanf("%d",&x);
push(x);
}
for(int i=;i<=n;i++)
{
printf("%d ",heap[]);
pop();
}
} //手打最小堆(最大堆同理)
一些神奇的(优化)板子——来自Loi_black的博客的更多相关文章
- 转载来自朱小厮博客的 一文看懂Kafka消息格式的演变
转载来自朱小厮博客的 一文看懂Kafka消息格式的演变 ✎摘要 对于一个成熟的消息中间件而言,消息格式不仅关系到功能维度的扩展,还牵涉到性能维度的优化.随着Kafka的迅猛发展,其消息格式也在 ...
- 从JavaScript 数组去重看兼容性有关问题,及性能优化(摘自玉伯博客)
JavaScript 数组去重经常出现在前端招聘的笔试题里,比如: 有数组 var arr = ['a', 'b', 'c', '1', 0, 'c', 1, '', 1, 0],请用 JavaScr ...
- WordPress 性能优化:为什么我的博客比你的快
WordPress 很慢? 很多博主都会感觉 WordPress 很慢?作为全世界最常用的建站和博客系统 WordPress 来说,在性能设计上肯定不会有太大的问题,WordPress 开发团队也肯定 ...
- 数论板子——来自Loi_black
#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> usin ...
- 来自MarsEdit的博客测试
使用MarsEdit编辑的第一个测试博客. 希望我们一帆风顺! 插图,在插图时可以调整尺寸: 六种公式写法,记得要在选项中打开-启用数学公式: \begin{equation}\sum\end{e ...
- 一句话为当前窗口客户区捉图: GetFormImage 来自万一的博客
一句话为当前窗口客户区捉图: GetFormImage http://www.cnblogs.com/del/archive/2008/10/24/1318738.html unit Unit1; i ...
- ASP.NET中常用输出JS脚本的类(来自于周公博客)
using System; using System.Collections.Generic; using System.Text; using System.Web; using System.We ...
- wdcp 打开网页显示 Apache 2 Test Page powered by CentOS -- 来自辉哥博客
是因为更新过系统,安装并更新了系统自带的apache 执行这个命令即可 #ln -sf /www/wdlinux/init.d/httpd /etc/rc.d/init.d/httpd#reboot ...
- VuePress 博客之 SEO 优化(三)标题、链接优化
前言 在 <一篇带你用 VuePress + Github Pages 搭建博客>中,我们使用 VuePress 搭建了一个博客,最终的效果查看:TypeScript 中文文档. 本篇讲讲 ...
随机推荐
- python2 和 pyhton3 输入语句写法
Python的输入语句类型 1 python2的输入语句 在python2中有两种常见的输入语句,input()和raw_input(). (1)input()函数 可以接收不同类型的参数,而且返回的 ...
- HackerRank - lonely-integer 【水】
题意 给出一系列数字,输出那个出现次数为奇数次的数字 思路 用MAP标记一下,在输入的时候判断一下 之前有没有输入过,如果有,就抹掉 最后剩下的那个 就是出现次数为奇数的 或者可以用 位运算 AC代码 ...
- matlab 三维绘制
1. mesh(Z)语句 mesh(Z)语句可以给出矩阵Z元素的三维消隐图,网络表面由Z坐标点定义,与前面叙述的x-y平面的线格相同,图形由邻近的点连接而成.它可用来显示用其它方式难以输出的包含大量数 ...
- java配置好jdk-bash: /usr/bin/java: No such file or directory
在 Linux 系统中安装 JDK 环境,配置好环境变量后,输入 java.javac 或者 java -version 等时,都提示如下错误: -bash: /usr/local/java/bin/ ...
- linux下安装eclipse并使用xstart远程使用(centos7)
1 eclipse安装 1)到官网下载eclipse的linux版 http://www.eclipse.org/downloads/packages/eclipse-ide-java-ee-deve ...
- JavaWeb Cookie
1. Cookie 1.1. Cookie概述 Cookie译为小型文本文件或小甜饼,Web应用程序利用Cookie在客户端缓存服务器端文件.Cookie是以键值对形式存储在客户端主机硬盘中,由服务器 ...
- [转载]Runtime详解
Runtime的特性主要是消息(方法)传递,如果消息(方法)在对象中找不到,就进行转发,具体怎么实现的呢.我们从下面几个方面探寻Runtime的实现机制. Runtime介绍 Runtime消息传 ...
- PhotoShopCs5启动 需要使用Adobe Application Manager 启动试用版
解决办法:下载Application Manager 7.0 地址:http://download.adobe.com/pub/adobe/creativesuite/cs/win/Applicati ...
- hadoop 知识点总结
关于元数据的checkpoint 每隔一段时间,会由secondary namenode将namenode上积累的所有edits和一个最新的fsimage下载到本地,并加载到内存进行merge(这个过 ...
- 【51nod1519】拆方块[Codeforces](dp)
题目传送门:1519 拆方块 首先,我们可以发现,如果第i堆方块被消除,只有三种情况: 1.第i-1堆方块全部被消除: 2.第i+1堆方块全部被消除:(因为两侧的方块能够保护这一堆方块在两侧不暴露) ...