Merge pull request #22 from Kiritow/master

Catch up with master
This commit is contained in:
KiritoTRw 2016-09-13 04:48:42 +08:00 committed by GitHub
commit e8c0901300
22 changed files with 1828 additions and 0 deletions

71
CDOJ/594.cpp Normal file
View File

@ -0,0 +1,71 @@
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
#define MAXN 50100
#define MAXH 110
#define MAXVAL_H 100
#define INF 0x3f3f3f3f
int dp[2][MAXH];
int bus[MAXH];
int main()
{
int N,C;
while(scanf("%d %d",&N,&C)==2)
{
/// Main Loop
int x;
scanf("%d",&x);
int index=0;
///由于人无法变矮因此这部分的花费记为INF
for(int i=0;i<x;i++) dp[index][i]=INF;
///大于等于x的时候可以内增高呀
for(int i=x;i<=MAXVAL_H;i++) dp[index][i]=(i-x)*(i-x);
N--;
while(N--)
{
///登记一个新的身高x
scanf("%d",&x);
index=!index;///反转index(复用dp空间)
int begin=0;
int end=0;
/// 单调队列 方向: 小(0) --> 大(N)
for(int k=0;k<=MAXVAL_H;k++)
{
int cost=dp[!index][k]-k*C;
/// Pop Back
while(begin<end&& bus[end-1] > cost) end--;
/// Push Back
bus[end++]=cost;
if(k<x) {dp[index][k]=INF;}
///队列最前面是值最小的
else {dp[index][k]=bus[begin]+k*C+(k-x)*(k-x);}
}
/// Empty
begin=end=0;
for(int k=MAXVAL_H;k>=0;k--)
{
int cost=dp[!index][k]+k*C;
/// Pop Back
while(begin<end&&bus[end-1] > cost) end--;
/// Push Back
bus[end++]=cost;
if(k>=x)
{
dp[index][k]=min(dp[index][k],bus[begin]-k*C+(k-x)*(k-x));
}
}
}
int ans=INF;
for(int i=0;i<=MAXVAL_H;i++)
{
ans=min(dp[index][i],ans);
}
printf("%d\n",ans);
}
return 0;
}

62
CDOJ/594_ka200812.cpp Normal file
View File

@ -0,0 +1,62 @@
#include<iostream>
#include<string>
#include<stdio.h>
#include<memory.h>
using namespace std;
#define inf 0xfffffff
#define min(a,b) a<b?a:b
#define max(a,b) a>b?a:b
int dp[2][101];
int n,c;
int q[101];
int head,tail,cur;
int main()
{
int i,j,x,nowf;
//freopen("D:\\in.txt","r",stdin);
while(scanf("%d%d",&n,&c)==2)
{
scanf("%d",&x);
cur=0;
for(i=0;i<x;i++)
dp[cur][i]=inf;
for(i=x;i<=100;i++)
dp[cur][i]=(x-i)*(x-i);
for(i=1;i<n;i++)
{
scanf("%d",&x);
cur=1-cur;
//比前一个人高
head=tail=0;
for(j=0;j<=100;j++) //当身高为j时候队列里便已经保存了0~j-1的信息注意是第i-1个人的信息
{
nowf=dp[1-cur][j]-j*c;
while(head<tail && q[tail-1]>nowf)
tail--;
q[tail++]=nowf;
if(j<x)
dp[cur][j]=inf;
else
dp[cur][j]=q[head]+j*c+(x-j)*(x-j);
}
//比前一个人矮
head=tail=0;
for(j=100;j>=0;j--) //当身高为j时候队列里便已经保存了100~j+1的信息正写反写是有技巧的
{
nowf=dp[1-cur][j]+j*c;
while(head<tail && q[tail-1]>nowf)
tail--;
q[tail++]=nowf;
if(j>=x)
dp[cur][j]=min(dp[cur][j],q[head]-j*c+(x-j)*(x-j));
}
}
int ans=inf;
for(i=0;i<=100;i++)
ans=min(ans,dp[cur][i]);
printf("%d\n",ans);
}
return 0;
}

4
CDOJ/Readme.md Normal file
View File

@ -0,0 +1,4 @@
#CDOJ
*CDOJ is a UESTC Online Judge*
[CDOJ Home Page](http://acm.uestc.edu.cn/ "Welcome to CDOJ")

66
HDOJ/3401_acm_ted.cpp Normal file
View File

@ -0,0 +1,66 @@
#include<cstdio>
#include<cstring>
using namespace std;
#define MAX 2005
#define inf 0xfffff
#define max(a,b) ((a)>(b)?(a):(b))
int T,MaxP,W;
int APi[MAX],BPi[MAX],ASi[MAX],BSi[MAX];
int dp[MAX][MAX];//dp[i][j]第i天持有j股的最大值
//dp[i][j]=max{dp[i-1][j],max{dp[r][k]-APi[i]*(j-k)}(0<r<i-w,k<j),max{dp[r][k]+BPi[i]*(k-j)}(0<r<i-w,k>j)}
struct node
{
int x;//存dp[i-w-1][k]+APi[i]*k或dp[i-w-1][k]+BPi[i]*k
int p;//当前持股数
} q[2005],temp;
int front,back;
int main()
{
int cas;
scanf("%d",&cas);
for(; cas--;)
{
scanf("%d%d%d",&T,&MaxP,&W);
for(int i=1; i<=T; ++i)
scanf("%d%d%d%d",APi+i,BPi+i,ASi+i,BSi+i);
for(int i=0; i<=T; ++i)
for(int j=0; j<=MaxP; ++j)
dp[i][j]=-inf;
for(int i=1; i<=W+1; ++i)
for(int j=0; j<=ASi[i]; ++j)
dp[i][j]=(-APi[i]*j);
for(int i=2; i<=T; ++i)
{
for(int j=0; j<=MaxP; ++j)
dp[i][j]=max(dp[i][j],dp[i-1][j]);
if(i<=W+1) continue;
//买入
front=back=1;
for(int j=0; j<=MaxP; ++j)
{
temp.p=j;
temp.x=dp[i-W-1][j]+APi[i]*j;
for(;front<back&&q[back-1].x<temp.x;--back);
q[back++]=temp;
for(;front<back&&q[front].p+ASi[i]<j;++front);
dp[i][j]=max(dp[i][j],q[front].x-APi[i]*j);
}
//卖出
front=back=1;
for(int j=MaxP; j>=0; --j)
{
temp.p=j;
temp.x=dp[i-W-1][j]+BPi[i]*j;
for(;front<back&&q[back-1].x<temp.x;--back);
q[back++]=temp;
for(;front<back&&q[front].p-BSi[i]>j;++front);
dp[i][j]=max(dp[i][j],q[front].x-BPi[i]*j);
}
}
int ans=0;
for(int i=0;i<=MaxP;++i)
ans=max(ans,dp[T][i]);
printf("%d\n",ans);
}
return 0;
}

147
HDOJ/5868_zufezzt.cpp Normal file
View File

@ -0,0 +1,147 @@
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-6;
void File()
{
freopen("D:\\in.txt","r",stdin);
freopen("D:\\out.txt","w",stdout);
}
template <class T>
inline void read(T &x)
{
char c=getchar(); x=0;
while(!isdigit(c)) c=getchar();
while(isdigit(c)) {x=x*10+c-'0'; c=getchar();}
}
LL n,mod=1e9+7;
LL extend_gcd(LL a,LL b,LL &x,LL &y)
{
if(a==0&&b==0) return -1;
if(b==0){x=1;y=0;return a;}
LL d=extend_gcd(b,a%b,y,x);
y-=a/b*x;
return d;
}
LL mod_reverse(LL a,LL n)
{
LL x,y;
LL d=extend_gcd(a,n,x,y);
if(d==1) return (x%n+n)%n;
else return -1;
}
LL phi(LL n)
{
LL res=n,a=n;
for(int i=2;i*i<=a;i++)
{
if(a%i==0)
{
res=res/i*(i-1);
while(a%i==0) a/=i;
}
}
if(a>1) res=res/a*(a-1);
return res;
}
struct Matrix
{
LL A[3][3];
int R, C;
Matrix operator*(Matrix b);
};
Matrix X, Y, Z;
Matrix Matrix::operator*(Matrix b)
{
Matrix c;
memset(c.A, 0, sizeof(c.A));
int i, j, k;
for (i = 1; i <= R; i++)
for (j = 1; j <= b.C; j++)
for (k = 1; k <= C; k++)
c.A[i][j] = (c.A[i][j] + (A[i][k] * b.A[k][j]) % mod) % mod;
c.R = R; c.C = b.C;
return c;
}
void init()
{
memset(X.A, 0, sizeof X.A);
memset(Y.A, 0, sizeof Y.A);
memset(Z.A, 0, sizeof Z.A);
Y.R = 2; Y.C = 2;
for (int i = 1; i <= 2; i++) Y.A[i][i] = 1;
X.R = 2; X.C = 2;
X.A[1][1]=0; X.A[1][2]=1;
X.A[2][1]=1; X.A[2][2]=1;
Z.R = 1; Z.C = 2;
Z.A[1][1]=0; Z.A[1][2]=1;
}
LL work(int x)
{
x--;
while (x)
{
if (x % 2 == 1) Y = Y*X;
x = x >> 1;
X = X*X;
}
Z = Z*Y;
return Z.A[1][2];
}
LL fib(int x)
{
if(x==0) return 0;
init();
return work(x);
}
int main()
{
while(~scanf("%lld",&n))
{
if(n==1) { printf("2\n"); continue; }
LL ans=0;
for(LL i=1;i*i<=n;i++)
{
if(n%i!=0) continue;
LL t=phi(n/i)*((fib(i-1)+fib(i+1))%mod)%mod;
ans=(ans+t)%mod;
if(n/i!=i)
{
t=phi(i)*((fib(n/i-1)+fib(n/i+1))%mod)%mod;
ans=(ans+t)%mod;
}
}
LL ni=mod_reverse(n,mod);
ans=ans*ni%mod;
printf("%lld\n",ans);
}
return 0;
}

136
HDOJ/5869_kopyh.cpp Normal file
View File

@ -0,0 +1,136 @@
//kopyh
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define MOD 1000000007
#define N 112345
using namespace std;
int n,m,sum,res,flag;
#define root 1 , n , 1
#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1
struct node
{
int pos,val;
node(int x=0,int y=0){pos=x,val=y;}
friend bool operator < (node a, node b)
{
return a.val < b.val;
}
friend node operator + (node a, node b)
{
return node(a.pos,a.val+b.val);
}
}arr[N<<2];
int add[N<<2],tot;
void pushUp(int rt)
{
arr[rt] = arr[rt<<1]+arr[rt<<1|1];
}
void pushDown(int l,int r,int rt)
{
if(add[rt])
{
int m = (l+r)>>1;
add[rt<<1] += add[rt];
add[rt<<1|1] += add[rt];
arr[rt<<1].val += (m-l+1)*add[rt];
arr[rt<<1|1].val += (r-m)*add[rt];
add[rt] = 0;
}
}
void updata(int l,int r,int rt,int ql,int qr,int val)
{
if(l>qr||ql>r)return;
if(l>=ql&&r<=qr)
{
arr[rt].val += (r-l+1)*val;
add[rt] += val;
return;
}
pushDown(l,r,rt);
int m = (l+r)>>1;
if(ql<=m)updata(lson,ql,qr,val);
if(qr>m)updata(rson,ql,qr,val);
pushUp(rt);
}
void build(int l,int r,int rt)
{
add[rt]=0;
if(l == r)
{
arr[rt].val = 0;
arr[rt].pos = tot;
return;
}
int m = (l+r)>>1;
build(lson);
build(rson);
pushUp(rt);
}
node query(int l,int r,int rt,int ql,int qr)
{
if(l>qr||ql>r)
return node(0,0);
if(l>=ql&&r<=qr)
return arr[rt];
pushDown(l,r,rt);
int m = (l+r)>>1;
return query(lson,ql,qr)+query(rson,ql,qr);
}
int a[N],pos[N*10],ans[N];
vector<pair<int,int> >vc[N],qu[N];
int gcd(int x,int y)
{ return y?gcd(y,x%y):x; }
int main()
{
#ifndef ONLINE_JUDGE
freopen("test.txt","r",stdin);
#endif
int i,j,k,cas,T,t,x,y,z,now;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
vc[i].clear();
qu[i].clear();
}
for(i=1;i<=n;i++)
{
x=a[i]; y=i;
for(j=0;j<vc[i-1].size();j++)
{
t = gcd(vc[i-1][j].first,a[i]);
if(t!=x)
{
vc[i].push_back(make_pair(x,y));
x=t; y=vc[i-1][j].second;
}
}
vc[i].push_back(make_pair(x,y));
}
for(i=1;i<=m;i++)
{
scanf("%d%d",&x,&y);
qu[y].push_back(make_pair(x,i));
}
memset(pos,0,sizeof(pos));
build(root);
for(i=1;i<=n;i++)
{
for(j=0;j<vc[i].size();j++)
if(pos[vc[i][j].first] < vc[i][j].second)
{
if(pos[vc[i][j].first])updata(root,pos[vc[i][j].first],pos[vc[i][j].first],-1);
updata(root,vc[i][j].second,vc[i][j].second,1);
pos[vc[i][j].first] = vc[i][j].second;
// printf("pos=%d %d\n",vc[i][j].first,pos[vc[i][j].first]);
}
for(j=0;j<qu[i].size();j++)
ans[qu[i][j].second] = query(root,qu[i][j].first,i).val;
}
for(i=1;i<=m;i++)
printf("%d\n",ans[i]);
}
return 0;
}

76
HDOJ/5869_zxhl.cpp Normal file
View File

@ -0,0 +1,76 @@
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
#define pii pair<int,int>
#define MP make_pair
typedef long long LL;
const long long INF = 1e18;
const double Pi = acos(-1.0);
const int N = 1e6+10, M = 1e2+11, mod = 1e9+7, inf = 2e9;
int n,q,a[N],ans[N];
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a%b);}
vector<pii> G[N];
struct QQ{
int l,r,id;
bool operator < (const QQ &a) const {
return a.r > r;
}
}Q[N];
int C[N],vis[N];
void update(int x,int c) {
for(int i =x; i < N; i+=i&(-i)) C[i] += c;
}
int ask(int x) {
int s =0 ;
for(int i = x; i; i-= i & (-i))s += C[i];
return s;
}
int main() {
while(scanf("%d%d",&n,&q)!=EOF) {
for(int i = 1; i <= n; ++i) scanf("%d",&a[i]);
for(int i = 0; i <= n; ++i) G[i].clear();
for(int i = 1; i <= n; ++i) {
int x = a[i];
int y = i;
for(int j = 0; j < G[i-1].size(); ++j) {
int res = gcd(x,G[i-1][j].first);
if(x != res) {
G[i].push_back(MP(x,y));
x = res;
y = G[i-1][j].second;
}
}
G[i].push_back(MP(x,y));
}
memset(C,0,sizeof(C));
memset(vis,0,sizeof(vis));
for(int i = 1; i <= q; ++i) {scanf("%d%d",&Q[i].l,&Q[i].r),Q[i].id = i;}
sort(Q+1,Q+q+1);
for(int R = 0, i = 1; i <= q; ++i) {
while(R < Q[i].r) {
R++;
for(int j = 0; j < G[R].size(); ++j) {
int res = G[R][j].first;
int ids = G[R][j].second;
if(vis[res]) update(vis[res],-1);
vis[res] = ids;
update(vis[res],1);
}
}
ans[Q[i].id] = ask(R) - ask(Q[i].l-1);
}
for(int i = 1; i <= q; ++i) cout<<ans[i]<<endl;
}
return 0;
}

152
HDOJ/5870_fzmh.cpp Normal file
View File

@ -0,0 +1,152 @@
#include<cstdio>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#define mp make_pair
#define pb push_back
#define fi first
#define sc second
using namespace std;
const int N = 201010;
const int M = 2010101;
const int inf = 2100000000;
typedef pair<int,int> P;
priority_queue<P,vector<P>,greater<P> > Q;
int n,m,i,a,b,c,dis[N],cnt[N],dis1[N],cnt1[N],dis2[N],cnt2[N],X;
int id[N],ID[N],Id[N];
vector<P> e[N];
void gao(int x)
{
int i;
for (i=1;i<=n;i++)
dis[i]=inf,cnt[i]=0;
dis[x]=0;
cnt[x]=1;
Q.push(mp(0,x));
while (!Q.empty())
{
P tmp=Q.top();
Q.pop();
x=tmp.sc;
if (dis[x]!=tmp.fi) continue;
for (i=0;i<e[x].size();i++)
if (dis[x]+e[x][i].sc<dis[e[x][i].fi])
{
dis[e[x][i].fi]=dis[x]+e[x][i].sc;
cnt[e[x][i].fi]=cnt[x];
Q.push(mp(dis[e[x][i].fi],e[x][i].fi));
}
else
if (dis[x]+e[x][i].sc==dis[e[x][i].fi])
{
cnt[e[x][i].fi]+=cnt[x];
if (cnt[e[x][i].fi]>X) cnt[e[x][i].fi]=X;
}
}
}
int ls[M],rs[M],s[M],tot,root[N];
void build(int &x,int a,int b)
{
x=++tot;
ls[x]=rs[x]=s[x]=0;
if (b-a>1)
{
int m=(a+b)>>1;
build(ls[x],a,m);
build(rs[x],m,b);
}
}
void insert(int y,int &x,int a,int b,int l,int r)
{
x=++tot;
ls[x]=ls[y];rs[x]=rs[y];s[x]=s[y]+1;
if ((a<=l)&&(r<=b))
return;
int m=(l+r)>>1;
if (a<m) insert(ls[y],ls[x],a,b,l,m);
if (m<b) insert(rs[y],rs[x],a,b,m,r);
}
int query(int x,int a,int b,int l,int r)
{
if ((a<=l)&&(r<=b))
return s[x];
int m=(l+r)>>1,ans=0;
if (a<m) ans+=query(ls[x],a,b,l,m);
if (m<b) ans+=query(rs[x],a,b,m,r);
return ans;
}
bool cmp(int a,int b)
{
return dis2[a]<dis2[b];
}
bool CMP(int a,int b)
{
return cnt2[a]>cnt2[b];
}
int main()
{
while (~scanf("%d%d",&n,&m))
{
if (n+m==0) return 0;
for (i=1;i<=n;i++) e[i].clear();
scanf("%d",&X);
for (i=1;i<=m;i++)
{
scanf("%d%d%d",&a,&b,&c);
e[a].pb(mp(b,c));
e[b].pb(mp(a,c));
}
gao(1);
for (i=1;i<=n;i++)
dis1[i]=dis[i],cnt1[i]=cnt[i];
gao(n);
for (i=1;i<=n;i++)
dis2[i]=dis[i],cnt2[i]=cnt[i];
tot=0;
build(root[0],0,n);
for (i=1;i<=n;i++)
id[i]=i;
sort(id+1,id+1+n,cmp);
for (i=1;i<=n;i++)
ID[id[i]]=i;
for (i=1;i<=n;i++)
Id[i]=i;
sort(Id+1,Id+1+n,CMP);
for (i=1;i<=n;i++)
insert(root[i-1],root[i],ID[Id[i]]-1,ID[Id[i]],0,n);
long long ans=0;
for (i=1;i<=n;i++)
{
int l=1,r=n;
while (l<=r)
{
m=(l+r)>>1;
if (dis2[id[m]]+dis1[i]+1<=dis1[n]) l=m+1;else r=m-1;
}
int j=r;
l=1;r=n;
while (l<=r)
{
m=(l+r)>>1;
if (1LL*cnt1[i]*cnt2[Id[m]]>=X) l=m+1;else r=m-1;
}
if (j) ans=ans+query(root[r],0,j,0,n);
for (int k=0;k<e[i].size();k++)
if (dis2[e[i][k].fi]+1+dis1[i]<=dis1[n])
if (1LL*cnt1[i]*cnt2[e[i][k].fi]>=X) ans--;
if (dis1[i]+dis2[i]+1<=dis1[N])
if (1LL*cnt1[i]*cnt2[i]>=X) ans--;
}
printf("%lld\n",ans);
}
}

22
HDOJ/5872_kopyh.cpp Normal file
View File

@ -0,0 +1,22 @@
//kopyh
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define MOD 1000000007
#define N 1123456
using namespace std;
long long n,m,sum,res,flag;
int main()
{
#ifndef ONLINE_JUDGE
freopen("test.txt","r",stdin);
#endif
long long i,j,cas,T,t,x,y,z,h,l,k;
while(scanf("%I64d%I64d%I64d",&h,&l,&k)!=EOF)
{
x=k/h;//最大人数时一排几个部门
y=k/(x+1);//多放一个部门时每个部门最多的人数
res=y+1;//加一个人保证不能多放一个部门
printf("%I64d\n",l/(k-k%res)+bool(l%(k-k%res)));//ok
}
return 0;
}

38
HDOJ/5873_kopyh.cpp Normal file
View File

@ -0,0 +1,38 @@
//kopyh
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define MOD 1000000007
#define N 1123456
using namespace std;
long long n,m,sum,res,flag,a[N];
int main()
{
#ifndef ONLINE_JUDGE
freopen("test.txt","r",stdin);
#endif
int i,j,k,cas,T,t,x,y,z;
while(scanf("%I64d",&n)!=EOF)
while(n--)
{
flag=1; sum=0;
scanf("%I64d",&m);
for(i=0;i<m;i++)
{
scanf("%I64d",&a[i]);
sum+=a[i];
}
if(sum!=m*(m-1))flag=0;
else
{
sort(a,a+m);
res=0;
for(i=0;i<m;i++)
{
res+=a[i];
if(res<i*(i+1)){flag=0;break;}
}
}
printf(flag?"T\n":"F\n");
}
return 0;
}

15
HDOJ/5874_qq_33184171.cpp Normal file
View File

@ -0,0 +1,15 @@
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
int main()
{
LL m,n;
while(~scanf("%I64d %I64d",&m,&n))
{
LL ans = m/2*(m-m/2);
if( ans <= n) puts("T");
else puts("F");
}
return 0;
}

48
HDOJ/5875_chen9510.cpp Normal file
View File

@ -0,0 +1,48 @@
// From http://www.cnblogs.com/chen9510/p/5862834.html
// ONLY For Test
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <set>
using namespace std;
int A[100005];
int pos[100005];
int s[100005];
int main()
{
int T,N,M;
cin>>T;
while(T--)
{
scanf("%d",&N);
for(int i=1;i<=N;i++)
scanf("%d",&A[i]);
int num=1;
A[N+1]=-1;
s[1]=N+1;
for(int i=N;i>=1;i--)///单调栈;
{
while(A[i]<A[s[num]]) num--;
pos[i]=s[num];
s[++num]=i;
}
scanf("%d",&M);
while(M--)
{
int l,r;
scanf("%d%d",&l,&r);
int tmp=A[l];
while(l<=r)
{
if(tmp==0) break;
if(pos[l]>r) break;
tmp=tmp%A[pos[l]];
l=pos[l];
}
printf("%d\n",tmp);
}
}
return 0;
}

View File

@ -0,0 +1,98 @@
#include <iostream>
#include <stdio.h>
#include <queue>
#include <string.h>
#include <stdlib.h>
#include <set>
using namespace std;
const int maxn=200000*5,inf=0x3f3f3f3f;
struct Edge
{
int v,next,cap;
} edge[maxn];
int tot,head[maxn];
int vis[maxn],du[maxn],dis[maxn];
void init()
{
tot=0;
memset(head,-1,sizeof(head));
}
void add_edge(int u,int v,int cap)
{
edge[tot].v=v;
edge[tot].cap=cap;
edge[tot].next=head[u];
head[u]=tot++;
}
void bfs(int start,int n)
{
set<int>s1;
set<int>s2;
set<int>::iterator it;
for(int i=1;i<=n;i++)
{
if(i!=start)
s1.insert(i);
}
queue<int>que;
que.push(start);
dis[start]=0;
while(!que.empty())
{
int u=que.front();
que.pop();
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].v;
if(!s1.count(v))
continue;
s1.erase(v);
s2.insert(v);
}
for(it=s1.begin();it!=s1.end();it++)
{
que.push(*it);
dis[*it]=dis[u]+1;
}
s1.swap(s2);
s2.clear();
}
}
int main()
{
int s,n,t,m;
int u,v;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
init();
memset(du,0,sizeof(du));
for(int i=0; i<m; i++)
{
scanf("%d%d",&u,&v);
add_edge(u,v,1);
add_edge(v,u,1);
}
scanf("%d",&s);
bfs(s,n);
for(int i=1; i<=n; i++)
{
if(i==s)
continue;
if(dis[i]==inf)
dis[i]=-1;
printf("%d",dis[i]);
if(i==n)
printf("\n");
else
printf(" ");
}
}
return 0;
}

319
HDOJ/5877_angon823.cpp Normal file
View File

@ -0,0 +1,319 @@
#include<stdio.h>
#include<stdlib.h>
#define MAXN 200005
int cnt = 1, rt = 0;
int Add[MAXN];
struct Tree{
long long key;
int num, size, fa, son[2];
}T[MAXN];
inline void PushUp(int x)
{
T[x].size = T[T[x].son[0]].size + T[T[x].son[1]].size + T[x].num;
}
inline void PushDown(int x)
{
if (Add[x])
{
if (T[x].son[0])
{
T[T[x].son[0]].key += Add[x];
Add[T[x].son[0]] += Add[x];
}
if (T[x].son[1])
{
T[T[x].son[1]].key += Add[x];
Add[T[x].son[1]] += Add[x];
}
Add[x] = 0;
}
}
inline int Newnode(long long key, int fa) //新建一个节点并返回
{
++cnt;
T[cnt].key = key;
T[cnt].num = T[cnt].size = 1;
T[cnt].fa = fa;
T[cnt].son[0] = T[cnt].son[1] = 0;
return cnt;
}
inline void Rotate(int x, int p) //0左旋 1右旋
{
int y = T[x].fa;
PushDown(y);
PushDown(x);
T[y].son[!p] = T[x].son[p];
T[T[x].son[p]].fa = y;
T[x].fa = T[y].fa;
if (T[x].fa)
T[T[x].fa].son[T[T[x].fa].son[1] == y] = x;
T[x].son[p] = y;
T[y].fa = x;
PushUp(y);
PushUp(x);
}
void Splay(int x, int To) //将x节点移动到To的子节点中
{
while (T[x].fa != To)
{
if (T[T[x].fa].fa == To)
Rotate(x, T[T[x].fa].son[0] == x);
else
{
int y = T[x].fa, z = T[y].fa;
int p = (T[z].son[0] == y);
if (T[y].son[p] == x)
Rotate(x, !p), Rotate(x, p); //之字旋
else
Rotate(y, p), Rotate(x, p); //一字旋
}
}
if (To == 0) rt = x;
}
int GetPth(int p, int To) //返回第p小的节点 并移动到To的子节点中
{
if (!rt || p > T[rt].size) return 0;
int x = rt;
while (x)
{
PushDown(x);
if (p >= T[T[x].son[0]].size + 1 && p <= T[T[x].son[0]].size + T[x].num)
break;
if (p > T[T[x].son[0]].size + T[x].num)
{
p -= T[T[x].son[0]].size + T[x].num;
x = T[x].son[1];
}
else
x = T[x].son[0];
}
Splay(x, 0);
return x;
}
int Find(long long key) //返回值为key的节点 若无返回0 若有将其转移到根处
{
if (!rt) return 0;
int x = rt;
while (x)
{
PushDown(x);
if (T[x].key == key) break;
x = T[x].son[key > T[x].key];
}
if (x) Splay(x, 0);
return x;
}
int Prev() //返回根节点的前驱 非重点
{
if (!rt || !T[rt].son[0]) return 0;
int x = T[rt].son[0];
while (T[x].son[1])
{
PushDown(x);
x = T[x].son[1];
}
Splay(x, 0);
return x;
}
int Succ() //返回根结点的后继 非重点
{
if (!rt || !T[rt].son[1]) return 0;
int x = T[rt].son[1];
while (T[x].son[0])
{
PushDown(x);
x = T[x].son[0];
}
Splay(x, 0);
return x;
}
void Insert(long long key) //插入key值
{
if (!rt)
rt = Newnode(key, 0);
else
{
int x = rt, y = 0;
while (x)
{
PushDown(x);
y = x;
if (T[x].key == key)
{
T[x].num++;
T[x].size++;
break;
}
T[x].size++;
x = T[x].son[key > T[x].key];
}
if (!x)
x = T[y].son[key > T[y].key] = Newnode(key, y);
Splay(x, 0);
}
}
void Delete(long long key) //删除值为key的节点1个
{
int x = Find(key);
if (!x) return;
if (T[x].num>1)
{
T[x].num--;
PushUp(x);
return;
}
int y = T[x].son[0];
while (T[y].son[1])
y = T[y].son[1];
int z = T[x].son[1];
while (T[z].son[0])
z = T[z].son[0];
if (!y && !z)
{
rt = 0;
return;
}
if (!y)
{
Splay(z, 0);
T[z].son[0] = 0;
PushUp(z);
return;
}
if (!z)
{
Splay(y, 0);
T[y].son[1] = 0;
PushUp(y);
return;
}
Splay(y, 0);
Splay(z, y);
T[z].son[0] = 0;
PushUp(z);
PushUp(y);
}
int GetRank(long long key) //获得值<=key的节点个数
{
if (!Find(key))
{
Insert(key);
int tmp = T[T[rt].son[0]].size;
Delete(key);
return tmp;
}
else
return T[T[rt].son[0]].size + T[rt].num;
}
void Delete(int l, int r) //删除值在[l, r]中的所有节点 l!=r
{
if (!Find(l)) Insert(l);
int p = Prev();
if (!Find(r)) Insert(r);
int q = Succ();
if (!p && !q)
{
rt = 0;
return;
}
if (!p)
{
T[rt].son[0] = 0;
PushUp(rt);
return;
}
if (!q)
{
Splay(p, 0);
T[rt].son[1] = 0;
PushUp(rt);
return;
}
Splay(p, q);
T[p].son[1] = 0;
PushUp(p);
PushUp(q);
}
#define MAX 100005
#include<vector>
#include<memory.h>
using namespace std;
int v[MAX];
int N;
long long K;
long long res = 0;
struct Edge
{
int b;
int next;
}edge[MAX];
int edgeCnt = 1;
int adj[MAX];
void AddEdge(int a, int b)
{
edge[edgeCnt].b = b;
edge[edgeCnt].next = adj[a];
adj[a] = edgeCnt++;
}
void Search(int now)
{
res += GetRank(K / v[now]);
Insert(v[now]);
for(int p = adj[now]; p; p = edge[p].next)
{
Search(edge[p].b);
}
Delete(v[now]);
}
int in[MAX];
int main() {
int T;
while(~scanf("%d", &T))
{
int i;
int a, b;
for (; T; T--)
{
scanf("%d%lld", &N, &K);
memset(adj, 0, (N + 1) * sizeof(int));
memset(in, 0, (N + 1) * sizeof(int));
edgeCnt = 1;
for (i = 1; i <= N; i++)
{
scanf("%d", &v[i]);
}
for (i = 1; i < N; i++)
{
scanf("%d%d", &a, &b);
in[b]++;
AddEdge(a, b);
}
for (i = 1; i <= N; i++)
{
if (in[i] == 0)
{
break;
}
}
cnt = 1, rt = 0;
res = 0;
Search(i);
printf("%lld\n", res);
}
}
return 0;
}

130
POJ/1957.cpp Normal file
View File

@ -0,0 +1,130 @@
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
struct Point2D
{
int x,y;
};
int nextvec[6][2]=
{
{
1,0
},
{
1,1
},
{
0,1
},
{
-1,0
},
{
-1,-1
},
{
0,-1
}
};
char stra[1024];
char strb[1024];
char buff[1024];
Point2D pa[2000];
Point2D pb[2000];
bool cmp(const Point2D& a,const Point2D& b)
{
if(a.x==b.x)
{
return a.y < b.y;
}
return a.x < b.x;
}
int main()
{
int N;
scanf("%d%*c",&N);
while(N--)
{
memset(pa,0,sizeof(pa));
memset(pb,0,sizeof(pb));
gets(stra);
gets(strb);
gets(buff);
int lena=strlen(stra);
int lenb=strlen(strb);
if(lena==0&&lenb==0)
{
printf("true\n");
continue;
}
else if(lena!=lenb)
{
printf("false\n");
continue;
}
int x=0;
int y=0;
for(int i=0;i<lena;i++)
{
int dir=(stra[i]-'a');
x+=nextvec[dir][0];
y+=nextvec[dir][1];
pa[i].x=x;
pa[i].y=y;
}
pa[lena].x=0;
pa[lena].y=0;
sort(pa,pa+lena+1,cmp);
int flag=0;
for(int dirCLT=0;dirCLT<6;dirCLT++)
{
for(int startID=0;startID<=lena;startID++)
{
x=pa[startID].x;
y=pa[startID].y;
for(int i=0;i<lenb;i++)
{
int dir=(strb[i]-'a'+dirCLT)%6;
x+=nextvec[dir][0];
y+=nextvec[dir][1];
pb[i].x=x;
pb[i].y=y;
}
pb[lenb].x=pa[startID].x;
pb[lenb].y=pa[startID].y;
sort(pb,pb+lenb+1,cmp);
int tflag=0;
for(int i=0;i<=lena;i++)
{
if(pa[i].x!=pb[i].x||pa[i].y!=pb[i].y)
{
tflag=1;
break;
}
}
if(tflag==0)
{
flag=1;
break;
}
}
if(flag)
break;
}
if(flag)
{
printf("true\n");
}
else
{
printf("false\n");
}
}
return 0;
}

79
POJ/2823_alongela.cpp Normal file
View File

@ -0,0 +1,79 @@
#include <iostream>
#include <cstdio>
using namespace std;
const int N = 1000005;
struct Elem
{
int val;
int pos;
};
Elem maxque[N];
Elem minque[N];
int maxhead, minhead, maxtail, mintail;
int maxans[N];
int minans[N];
int cur;
int main()
{
int n, w, num;
scanf("%d%d", &n, &w);
minhead = mintail = 0;
maxhead = maxtail = 0;
cur = 0;
for (int i = 0; i < w; ++i)
{
scanf("%d", &num);
while (minhead < mintail && minque[mintail - 1].val >= num) --mintail;
minque[mintail].val = num;
minque[mintail].pos = i;
++mintail;
while (maxhead < maxtail && maxque[maxtail - 1].val <= num) --maxtail;
maxque[maxtail].val = num;
maxque[maxtail].pos = i;
++maxtail;
}
for (int i = w; i < n; ++i)
{
minans[cur] = minque[minhead].val;
maxans[cur] = maxque[maxhead].val;
++cur;
scanf("%d", &num);
while (minhead < mintail && i - minque[minhead].pos >= w) ++minhead;
while (minhead < mintail && minque[mintail - 1].val >= num) --mintail;
minque[mintail].val = num;
minque[mintail].pos = i;
++mintail;
while (maxhead < maxtail && i - maxque[maxhead].pos >= w) ++maxhead;
while (maxhead < maxtail && maxque[maxtail - 1].val <= num) --maxtail;
maxque[maxtail].val = num;
maxque[maxtail].pos = i;
++maxtail;
}
minans[cur] = minque[minhead].val;
maxans[cur] = maxque[maxhead].val;
++cur;
for (int i = 0; i < cur; ++i)
{
if (i > 0) putchar(' ');
printf("%d", minans[i]);
}
printf("\n");
for (int i = 0; i < cur; ++i)
{
if (i > 0) putchar(' ');
printf("%d", maxans[i]);
}
printf("\n");
return 0;
}

112
SCU/4444_lvshubao1314.cpp Normal file
View File

@ -0,0 +1,112 @@
/**
SCU - 4444 -
a5e5b1~n的最短路径1e5
1n之间连边为a那么答案一定为a和一条最短的全由b组成的路径的较小者1n之间连边为b
b和一条最短的全由a组成的路径的较小者1spfa就可以了spfa
1b的边set维护一下1
*/
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <set>
#include <queue>
using namespace std;
typedef long long LL;
const int INF=1e9+7;
const int maxn=100100;
int n,m,vis[maxn];
LL a,b,dis[maxn];
set<int>st,ts;
set<int>::iterator it;
int head[maxn],ip;
void init()
{
memset(head,-1,sizeof(head));
ip=0;
}
struct note
{
int v,next;
}edge[maxn*10];
void addedge(int u,int v)
{
edge[ip].v=v,edge[ip].next=head[u],head[u]=ip++;
}
void spfa()
{
queue<int>q;
for(int i=0;i<=n;i++)dis[i]=INF;
memset(vis,0,sizeof(vis));
dis[1]=0;
q.push(1);
while(!q.empty())
{
int u=q.front();
q.pop();
vis[u]=0;
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].v;
if(dis[v]>dis[u]+1)
{
dis[v]=dis[u]+1;
if(!vis[v])
{
vis[v]=1;
q.push(v);
}
}
}
}
printf("%lld\n",min(dis[n]*a,b));
}
void bfs()
{
dis[n]=INF;
st.clear(),ts.clear();
for(int i=2;i<=n;i++)st.insert(i);
queue<int>q;
q.push(1);
dis[1]=0;
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].v;
if(st.count(v)==0)continue;
st.erase(v),ts.insert(v);
}
for(it=st.begin();it!=st.end();it++)
{
q.push(*it);
dis[*it]=dis[u]+1;
}
st.swap(ts);
ts.clear();
}
printf("%lld\n",min(dis[n]*b,a));
}
int main()
{
while(~scanf("%d%d%lld%lld",&n,&m,&a,&b))
{
init();
int flag=0;
for(int i=0;i<m;i++)
{
int u,v;
scanf("%d%d",&u,&v);
if(u>v)swap(u,v);
addedge(u,v);
addedge(v,u);
if(u==1&&v==n)flag=1;
}
if(flag)bfs();
else spfa();
}
return 0;
}

102
SCU/4444_woshinannan741.cpp Normal file
View File

@ -0,0 +1,102 @@
#include<cstdio>
#include<cstring>
#include<queue>
#include<set>
#include<algorithm>
using namespace std;
const int MAXN = 5e5+5;
const int INF = 0x3f3f3f3f;
typedef long long LL;
int head[MAXN],e;
LL dis[MAXN];
bool vis[MAXN];
struct Edge{
int v,nxt,w;
}E[MAXN<<1];
void init()
{
e=0;
memset(head,-1,sizeof head);
memset(vis,false,sizeof vis);
}
void add(int u,int v,int w)
{
E[e].v = v;
E[e].w = w;
E[e].nxt = head[u];
head[u] = e++;
}
struct QNode{
int v,c;
QNode(int _v,int _c){
v = _v;c=_c;
}
bool operator < (const QNode &a) const{
return c>a.c;
}
};
LL dijkstra(int n)
{
memset(dis,INF,sizeof dis);
priority_queue<QNode>Q;
dis[1] = 0;
Q.push(QNode(1,0));
while(!Q.empty())
{
QNode tmp = Q.top();Q.pop();
int u = tmp.v;
if(vis[u]) continue;
vis[u] = true;
for(int i=head[u];~i;i=E[i].nxt){
int v = E[i].v;
int w = E[i].w;
if(vis[v]) continue;
if(dis[u]+w<dis[v]){
dis[v] = dis[u]+w;
Q.push(QNode(v,dis[v]));
}
}
}
return dis[n];
}
LL BFS(int n,LL val)
{
set<int>ta,tb;
queue<int>Q;Q.push(1);
dis[1] = 0,dis[n] = INF;
for(int i=2;i<=n;i++) ta.insert(i);
while(!Q.empty())
{
int u = Q.front();Q.pop();
for(int i=head[u];~i;i=E[i].nxt){
int v = E[i].v;
if(!ta.count(v)) continue;
ta.erase(v);tb.insert(v);
}
for(set<int>::iterator it=ta.begin();it!=ta.end();it++){
Q.push(*it);
dis[*it] = dis[u] + val;
}
ta.swap(tb);tb.clear();
}
return dis[n];
}
int main()
{
int N,M;
LL A,B;
int u,v;
while(~scanf("%d%d%lld%lld",&N,&M,&A,&B))
{
init();
bool flag = false;
for(int i=0;i<M;i++){
scanf("%d%d",&u,&v);
add(u,v,A);add(v,u,A);
if(min(u,v)==1&&max(u,v)==N)flag =true;
}
if(!flag) printf("%lld\n",min(dijkstra(N),(LL)B));
else printf("%lld\n",min(BFS(N,B),(LL)A));
}
return 0;
}

3
SCU/Readme.md Normal file
View File

@ -0,0 +1,3 @@
#Sichuan University Online Judge 四川大学
[Home Page](http://acm.scu.edu.cn/soj/ "Sichuan University Online Judge")
[ACM Home Page](http://acm.scu.edu.cn/ "四川大学 - ACM首页")

71
UESTC/1685.cpp Normal file
View File

@ -0,0 +1,71 @@
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
#define MAXN 50100
#define MAXH 110
#define MAXVAL_H 100
#define INF 0x3f3f3f3f
int dp[2][MAXH];
int bus[MAXH];
int main()
{
int N,C;
while(scanf("%d %d",&N,&C)==2)
{
/// Main Loop
int x;
scanf("%d",&x);
int index=0;
///由于人无法变矮因此这部分的花费记为INF
for(int i=0;i<x;i++) dp[index][i]=INF;
///大于等于x的时候可以内增高呀
for(int i=x;i<=MAXVAL_H;i++) dp[index][i]=(i-x)*(i-x);
N--;
while(N--)
{
///登记一个新的身高x
scanf("%d",&x);
index=!index;///反转index(复用dp空间)
int begin=0;
int end=0;
/// 单调队列 方向: 小(0) --> 大(N)
for(int k=0;k<=MAXVAL_H;k++)
{
int cost=dp[!index][k]-k*C;
/// Pop Back
while(begin<end&& bus[end-1] > cost) end--;
/// Push Back
bus[end++]=cost;
if(k<x) {dp[index][k]=INF;}
///队列最前面是值最小的
else {dp[index][k]=bus[begin]+k*C+(k-x)*(k-x);}
}
/// Empty
begin=end=0;
for(int k=MAXVAL_H;k>=0;k--)
{
int cost=dp[!index][k]+k*C;
/// Pop Back
while(begin<end&&bus[end-1] > cost) end--;
/// Push Back
bus[end++]=cost;
if(k>=x)
{
dp[index][k]=min(dp[index][k],bus[begin]-k*C+(k-x)*(k-x));
}
}
}
int ans=INF;
for(int i=0;i<=MAXVAL_H;i++)
{
ans=min(dp[index][i],ans);
}
printf("%d\n",ans);
}
return 0;
}

6
UESTC/Readme.md Normal file
View File

@ -0,0 +1,6 @@
#University Of Electronic Science And Technology Of China
#中国电子科技大学
*UESTC OJ no longer exist. Submit on CDOJ or vjudge instead.*
[View On VJudge](http://acm.hust.edu.cn/vjudge/problem/#OJId=UESTC_old&probNum=&title=&source= "Problems - Virtual Judge")

View File

@ -0,0 +1,71 @@
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <time.h>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#define inf 0x3f3f3f3f
#define mod 10000
typedef long long ll;
using namespace std;
const int N=505;
const int M=100005;
int n,m,k,ans,t;
int pre[N];
struct man
{
int c,f;
}w[N][N];
bool bfs()
{
queue<int>q;
q.push(1);
memset(pre,0,sizeof(pre));
pre[1]=1;
while(!q.empty()){
int u=q.front();q.pop();
for(int i=1;i<=t;i++){
if( !pre[i] && w[u][i].c>w[u][i].f){
pre[i]=pre[u]+1;
q.push(i);
}
}
}
return pre[t]!=0;
}
int dfs(int u,int sum)
{
if(u==t||sum==0)return sum;
int tmp=sum,minn;
for(int i=1;i<=t;i++){
if(pre[i]==pre[u]+1&&w[u][i].c>w[u][i].f){
minn=dfs(i,min(tmp,w[u][i].c-w[u][i].f));
w[u][i].f+=minn;
w[i][u].f -=minn;
tmp-=minn;
}
}
return sum-tmp;
}
void Dinic()
{
ans=0;
while(bfs())ans+=dfs(1,inf);
cout<<ans<<endl;
}
int main() {
scanf("%d%d",&n,&m);
memset(w,0,sizeof(w));
int a,b,v;
t=n;
while(m--)scanf("%d%d%d",&a,&b,&v),w[a][b].c+=v;
Dinic();
return 0;
}