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. Linxu下jenkins部署和基本配置

    一.OpenJdk1.8安装(tomcat  和 jenkins都依赖与java) ubuntu apt-cache search openjdk       #使用apt-cache搜索可以直接使用 ...

  2. 【WEB HTTP】缓存

    1. HTTP并不支持兄弟缓存,所以人们通过一些协议对HTTP进行了扩展,比如因特网缓存协议(Internet Cache Protocol, ICP)和超文本缓存协议(HyperText Cachi ...

  3. SDWebImage浅析

    第一部分 SDWebImage库的作用: 通过对UIImageView的类别扩展来实现异步加载替换图片的工作. 主要用到的对象: 1)UIImageView(WebCache)类别,入口封装,实现读取 ...

  4. for update排他锁详解

    使用场景: 高并发并且对于数据的准确性很有要求. 落实到mysql就是在事务中使用,只有使用InnoDB时才用,在begin于commit之间使用(只有此引擎支持事务). 本质: 给表或行上个锁以便接 ...

  5. 20145230《java学习笔记》第七周学习总结

    20145230 <Java程序设计>第7周学习总结 教材学习内容 Lambda语法概览 我们在许多地方都会有按字符串长度排序的需求,如果在同一个方法内,我们可以使用一个byName局部变 ...

  6. GIT截图

    GIT截图 今天首次成功用了GIT上传了JAVA代码,感觉一下次就能上传这么多代码,确实比在网页上方便.自己一开始根本摸不着头脑,不知道怎样使用GIT软件,但在学姐博客的指导下,在同学热情且耐心地指导 ...

  7. BYTE、HANDEL、DWORD等的定义

    有时候引入第三方库之后,可能会存在标题的这些变量没有定义,原来这些变量都定义在windows.h里面,包含进去就行了(Qt的MSVC编译器)

  8. 斯坦福机器学习视频笔记 Week9 异常检测和高斯混合模型 Anomaly Detection

    异常检测,广泛用于欺诈检测(例如“此信用卡被盗?”). 给定大量的数据点,我们有时可能想要找出哪些与平均值有显着差异. 例如,在制造中,我们可能想要检测缺陷或异常. 我们展示了如何使用高斯分布来建模数 ...

  9. 【P2514】工厂选址(贪心)

    看到题了不首先应该看看数据范围确定一下算法么,这个题的数据范围大约可以支持到O(nmlogm),所以肯定不是搜索什么的,DP貌似至少也要n^2m,所以可以想一些其他的.对于题目的输入,我们发现这些输入 ...

  10. 剑指Offer——字符串的排序

    Question 输入一个字符串,按字典序打印出该字符串中字符的所有排列.例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba. 输入描 ...