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的博客的更多相关文章

  1. 转载来自朱小厮博客的 一文看懂Kafka消息格式的演变

    转载来自朱小厮博客的 一文看懂Kafka消息格式的演变     ✎摘要 对于一个成熟的消息中间件而言,消息格式不仅关系到功能维度的扩展,还牵涉到性能维度的优化.随着Kafka的迅猛发展,其消息格式也在 ...

  2. 从JavaScript 数组去重看兼容性有关问题,及性能优化(摘自玉伯博客)

    JavaScript 数组去重经常出现在前端招聘的笔试题里,比如: 有数组 var arr = ['a', 'b', 'c', '1', 0, 'c', 1, '', 1, 0],请用 JavaScr ...

  3. WordPress 性能优化:为什么我的博客比你的快

    WordPress 很慢? 很多博主都会感觉 WordPress 很慢?作为全世界最常用的建站和博客系统 WordPress 来说,在性能设计上肯定不会有太大的问题,WordPress 开发团队也肯定 ...

  4. 数论板子——来自Loi_black

    #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> usin ...

  5. 来自MarsEdit的博客测试

    使用MarsEdit编辑的第一个测试博客. 希望我们一帆风顺! 插图,在插图时可以调整尺寸:   六种公式写法,记得要在选项中打开-启用数学公式: \begin{equation}\sum\end{e ...

  6. 一句话为当前窗口客户区捉图: GetFormImage 来自万一的博客

    一句话为当前窗口客户区捉图: GetFormImage http://www.cnblogs.com/del/archive/2008/10/24/1318738.html unit Unit1; i ...

  7. ASP.NET中常用输出JS脚本的类(来自于周公博客)

    using System; using System.Collections.Generic; using System.Text; using System.Web; using System.We ...

  8. wdcp 打开网页显示 Apache 2 Test Page powered by CentOS -- 来自辉哥博客

    是因为更新过系统,安装并更新了系统自带的apache 执行这个命令即可 #ln -sf /www/wdlinux/init.d/httpd /etc/rc.d/init.d/httpd#reboot ...

  9. VuePress 博客之 SEO 优化(三)标题、链接优化

    前言 在 <一篇带你用 VuePress + Github Pages 搭建博客>中,我们使用 VuePress 搭建了一个博客,最终的效果查看:TypeScript 中文文档. 本篇讲讲 ...

随机推荐

  1. 指定Python线程数目

    可以通过threading.semaphore()来指定并行运行的Python线程的数目. #!/usr/bin/python2.7 #File: threadsNum.py #Author: lxw ...

  2. C语言预处理命令的使用

    cppreference.com -> 预处理命令 -> 详细说明 预处理命令 #,## # 和 ## 操作符是和#define宏使用的. 使用# 使在#后的首个参数返回为一个带引号的字符 ...

  3. 利用FFmpeg将RTSP转码成RTMP发布在RED5

    安装jdk,并设置环境  from:http://www.w3c.com.cn/%E5%88%A9%E7%94%A8ffmpeg%E5%B0%86-ipcamera-%E7%9A%84rtsp%E8% ...

  4. Python自然语言处理 - 系列四

    一 如何使用形式化语法来描述无限的句子集合的结构?    --上下位无关文法       1.1 一个例子:      grammar1 = nltk.parse_cfg(""&q ...

  5. ETL应用:使用Pro*C实现文件抽取的方法

    /******************************************* ***** 函数功能 : ***** 抽取数据库记录 ***** ********************** ...

  6. Ubuntu16.04下编译android6.0源码

    http://blog.csdn.net/cnliwy/article/details/52189349 作为一名合格的android开发人员,怎么能不会编译android源码呢!一定要来一次说编译就 ...

  7. P4121 [WC2005]双面棋盘

    题目 P4121 [WC2005]双面棋盘 貌似是刘汝佳出的题目?? 做法 线段树维护并查集 线段树分治\(1\)~\(n\)行,我们要考虑维护的肯定是黑.白各自的联通块数量 考虑区间合并,其实就与中 ...

  8. php多个数组同键名键值相加合并

    //任意多个相同键值的数组合并相加 //预先将所要合并的数组组装成一个新的数组 // $arr = array( // array( // 'user_id' => 100, // 'goods ...

  9. axios拦截器/http

    Interceptors //处理请求或响应之前拦截请求或响应. //添加一个请求拦截器 axios.interceptors.request.use(function (config) { //在请 ...

  10. K8s API

    https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.10/#daemonset-v1-apps http://docs.k ...