diff --git a/CDOJ/594.cpp b/CDOJ/594.cpp new file mode 100644 index 0000000..9310c91 --- /dev/null +++ b/CDOJ/594.cpp @@ -0,0 +1,71 @@ +#include +#include +#include +#include +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 大(N) + for(int k=0;k<=MAXVAL_H;k++) + { + int cost=dp[!index][k]-k*C; + /// Pop Back + while(begin cost) end--; + /// Push Back + bus[end++]=cost; + if(k=0;k--) + { + int cost=dp[!index][k]+k*C; + /// Pop Back + while(begin 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; +} diff --git a/CDOJ/594_ka200812.cpp b/CDOJ/594_ka200812.cpp new file mode 100644 index 0000000..f25789f --- /dev/null +++ b/CDOJ/594_ka200812.cpp @@ -0,0 +1,62 @@ +#include +#include +#include +#include +using namespace std; +#define inf 0xfffffff +#define min(a,b) ab?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;inowf) + tail--; + q[tail++]=nowf; + if(j=0;j--) //当身高为j时候,队列里便已经保存了100~j+1的信息,正写反写是有技巧的 + { + nowf=dp[1-cur][j]+j*c; + while(headnowf) + 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; +} diff --git a/CDOJ/Readme.md b/CDOJ/Readme.md new file mode 100644 index 0000000..7350ed6 --- /dev/null +++ b/CDOJ/Readme.md @@ -0,0 +1,4 @@ +#CDOJ +*CDOJ is a UESTC Online Judge* + +[CDOJ Home Page](http://acm.uestc.edu.cn/ "Welcome to CDOJ") diff --git a/HDOJ/3401_acm_ted.cpp b/HDOJ/3401_acm_ted.cpp new file mode 100644 index 0000000..5f288b3 --- /dev/null +++ b/HDOJ/3401_acm_ted.cpp @@ -0,0 +1,66 @@ +#include +#include +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)}(0j)} +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=0; --j) + { + temp.p=j; + temp.x=dp[i-W-1][j]+BPi[i]*j; + for(;frontj;++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; +} diff --git a/HDOJ/5868_zufezzt.cpp b/HDOJ/5868_zufezzt.cpp new file mode 100644 index 0000000..59038d9 --- /dev/null +++ b/HDOJ/5868_zufezzt.cpp @@ -0,0 +1,147 @@ +#pragma comment(linker, "/STACK:1024000000,1024000000") +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +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 +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; +} diff --git a/HDOJ/5869_kopyh.cpp b/HDOJ/5869_kopyh.cpp new file mode 100644 index 0000000..6904932 --- /dev/null +++ b/HDOJ/5869_kopyh.cpp @@ -0,0 +1,136 @@ +//kopyh +#include +#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 >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 +#include +#include +#include +#include +#include +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 +#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 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< +#include +#include +#include +#include +#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 P; +priority_queue,greater

> 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

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;iX) 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>1,ans=0; + if (acnt2[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=X) ans--; + + if (dis1[i]+dis2[i]+1<=dis1[N]) + if (1LL*cnt1[i]*cnt2[i]>=X) ans--; + + } + printf("%lld\n",ans); + } +} diff --git a/HDOJ/5872_kopyh.cpp b/HDOJ/5872_kopyh.cpp new file mode 100644 index 0000000..2bfc47e --- /dev/null +++ b/HDOJ/5872_kopyh.cpp @@ -0,0 +1,22 @@ +//kopyh +#include +#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; +} diff --git a/HDOJ/5873_kopyh.cpp b/HDOJ/5873_kopyh.cpp new file mode 100644 index 0000000..b41b042 --- /dev/null +++ b/HDOJ/5873_kopyh.cpp @@ -0,0 +1,38 @@ +//kopyh +#include +#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 +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; +} diff --git a/HDOJ/5875_chen9510.cpp b/HDOJ/5875_chen9510.cpp new file mode 100644 index 0000000..780de3d --- /dev/null +++ b/HDOJ/5875_chen9510.cpp @@ -0,0 +1,48 @@ +// From http://www.cnblogs.com/chen9510/p/5862834.html +// ONLY For Test +#include +#include +#include +#include +#include +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]r) break; + tmp=tmp%A[pos[l]]; + l=pos[l]; + } + printf("%d\n",tmp); + } + } + return 0; +} diff --git a/HDOJ/5876_yuanjunlai141.cpp b/HDOJ/5876_yuanjunlai141.cpp new file mode 100644 index 0000000..12ee4eb --- /dev/null +++ b/HDOJ/5876_yuanjunlai141.cpp @@ -0,0 +1,98 @@ +#include +#include +#include +#include +#include +#include +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) +{ + sets1; + sets2; + set::iterator it; + for(int i=1;i<=n;i++) + { + if(i!=start) + s1.insert(i); + } + + queueque; + 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 +#include +#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 +#include +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; +} diff --git a/POJ/1957.cpp b/POJ/1957.cpp new file mode 100644 index 0000000..e80e4ac --- /dev/null +++ b/POJ/1957.cpp @@ -0,0 +1,130 @@ +#include +#include +#include +#include +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 +#include +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; +} diff --git a/SCU/4444_lvshubao1314.cpp b/SCU/4444_lvshubao1314.cpp new file mode 100644 index 0000000..ee328ed --- /dev/null +++ b/SCU/4444_lvshubao1314.cpp @@ -0,0 +1,112 @@ +/** +SCU - 4444 别样最短路径-大数据完全图 +题目大意:给定一个完全图,其中有两种边,长度为a(不超过5e5)或长度为b(剩下的),求有1~n的最短路径(数据范围1e5) +解题思路:如果1和n之间连边为a那么答案一定为a和一条最短的全由b组成的路径的较小者,如果1和n之间连边为b,那么答案一定 + 为b和一条最短的全由a组成的路径的较小者。对于第1种情况直接跑spfa就可以了,第二种情况由于边数较多,不能直接spfa + 从1开始搜索与其相连的边权为b的边,用set维护一下,由于每个点只入队1次,复杂度还是允许的。这种处理方法还是第一 + 次做,感觉很巧妙 +*/ +#include +#include +#include +#include +#include +#include +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]; +setst,ts; +set::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() +{ + queueq; + 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); + queueq; + 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;iv)swap(u,v); + addedge(u,v); + addedge(v,u); + if(u==1&&v==n)flag=1; + } + if(flag)bfs(); + else spfa(); + } + return 0; +} diff --git a/SCU/4444_woshinannan741.cpp b/SCU/4444_woshinannan741.cpp new file mode 100644 index 0000000..e48985f --- /dev/null +++ b/SCU/4444_woshinannan741.cpp @@ -0,0 +1,102 @@ +#include +#include +#include +#include +#include +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_queueQ; + 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]+wta,tb; + queueQ;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::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 +#include +#include +#include +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 大(N) + for(int k=0;k<=MAXVAL_H;k++) + { + int cost=dp[!index][k]-k*C; + /// Pop Back + while(begin cost) end--; + /// Push Back + bus[end++]=cost; + if(k=0;k--) + { + int cost=dp[!index][k]+k*C; + /// Pop Back + while(begin 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; +} diff --git a/UESTC/Readme.md b/UESTC/Readme.md new file mode 100644 index 0000000..755889f --- /dev/null +++ b/UESTC/Readme.md @@ -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") diff --git a/hihoCoder/Weekly/115_jianrenfang.cpp b/hihoCoder/Weekly/115_jianrenfang.cpp new file mode 100644 index 0000000..0a10100 --- /dev/null +++ b/hihoCoder/Weekly/115_jianrenfang.cpp @@ -0,0 +1,71 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#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() +{ + queueq; + 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<