diff --git a/HDOJ/1078.cpp b/HDOJ/1078.cpp new file mode 100644 index 0000000..113de59 --- /dev/null +++ b/HDOJ/1078.cpp @@ -0,0 +1,57 @@ +#include +#include +#include +#include +using namespace std; +#define MAXN 101 +int map[MAXN][MAXN]; +int dp[MAXN][MAXN]; +int N; +int K; +int dfs(int y,int x) +{ + if(dp[y][x]!=0) return dp[y][x]; + dp[y][x]=map[y][x]; + for(int i=1;i<=K;i++) + { + if(x+i=0&&map[y][x]=0&&map[y][x] +#include +#include +using namespace std; + + +int C[131072]; + +char cmd[32]; + +int getsum(int left,int right,int pos,int leftbound,int rightbound) +{ + if(left<=leftbound&&right>=rightbound) + { + return C[pos]; + } + if(rightrightbound) + { + return 0; + } + int a=getsum(left,right,2*pos+1,leftbound,(leftbound+rightbound)/2); + int b=getsum(left,right,2*pos+2,(leftbound+rightbound)/2+1,rightbound); + return a+b; +} +int main() +{ + int t; + scanf("%d",&t); + for(int itt=1;itt<=t;itt++) + { + printf("Case %d:\n",itt); + int N; + scanf("%d",&N); + int realn=1; + while(realn0) + { + int L=start+1; + for(int i=0;i0) + { + C[pos]+=b; + pos=(pos-1)/2; + } + C[0]+=b; + } + else if(strcmp(cmd,"Sub")==0) + { + int pos=realn-1+a-1; + while(pos>0) + { + C[pos]-=b; + pos=(pos-1)/2; + } + C[0]-=b; + } + } + }/// end of for(...) + + return 0; +} diff --git a/HDOJ/1501_CSDN_dfs.cpp b/HDOJ/1501_CSDN_dfs.cpp new file mode 100644 index 0000000..c41f7c0 --- /dev/null +++ b/HDOJ/1501_CSDN_dfs.cpp @@ -0,0 +1,42 @@ +#include +#include +//#define LOCAL +using namespace std; + +const int MAXN=205; + +char a[MAXN],b[MAXN],all[2*MAXN-5]; +int lenA,lenB,lenAll; + +bool DFS(int posA,int posB,int posAll) { + if(posA==lenA&&posB==lenB) + return true; + if(posA +#include +#include + +using namespace std; + +int n,m,al,bl,cl,lim; +char a[205],b[205],c[405]; +bool dp[205][205];//dp[i][j]表示串a的前i个字符与串b的前j个字符是否能形成串c的前i+j个字符 + +int main() { + int T,kase=0; + scanf("%d",&T); + while(kase +#include +#include +using namespace std; + +char a[300]; +char b[300]; +char c[700]; + +int lena,lenb,lenc; +bool dfs(int pos_in_a,int pos_in_b,int pos_in_c) +{ + if(pos_in_a==lena&&pos_in_b==lenb) + { + return true; + } + if(pos_in_a +#include +#include + +#include +#include +using namespace std; + +struct cash +{ + int value; + int amount; +}; + +bool cmp(const cash& a,const cash& b) +{ + return a.value& vec,int C) +{ + memset(need,0,sizeof(int)*64); + int N=vec.size(); + int left=C; + for(int i=N-1;i>=0;i--) + {// + if(vec.at(i).amount>0&&left>0) + { + int x=left/vec.at(i).value; + x=min(x,vec.at(i).amount); + need[i]=x; + left-=x*vec.at(i).value; + } + } + if(left>0) + { + for(int i=0;ineed[i]&&left>0) + { + int x=left/vec.at(i).value; + if(x==0) + { + /// try to make x = 1 + need[i]++; + left=0; + break; + } + } + } + } + if(left>0) + { + return -1; + } + int canpayday=2<<29; + for(int i=0;i<64;i++) + { + if(need[i]>0) + { + canpayday=min(canpayday,vec.at(i).amount/need[i]); + } + } + if(canpayday<=0) return -1; + for(int i=0;i0) + { + vec.at(i).amount-=canpayday*need[i]; + } + } + return canpayday; + +} +int main() +{ + int N,C; + scanf("%d %d",&N,&C); + vector vec; + int ans=0; + for(int i=0;i=0;i--) + { + if(vec.at(i).value>=C) + { + ans+=vec.at(i).amount; + vec.pop_back(); + } + else break; + } + int tmp; + while((tmp=makedayfrom(vec,C))>0) + { + ans+=tmp; + } + printf("%d\n",ans); + return 0; +} diff --git a/POJ/3040_aiuxian.cpp b/POJ/3040_aiuxian.cpp new file mode 100644 index 0000000..7277aea --- /dev/null +++ b/POJ/3040_aiuxian.cpp @@ -0,0 +1,72 @@ +#include +#include +#include +#include +using namespace std; +const int maxn=30; +struct node +{ + int x,y;//存储钱的大小和数量 + bool operator<(const node c)const + { + return x>c.x; + } +}num[maxn]; +int need[maxn],n,c,kj; +//need[]函数是是存储需要哪些钱币的组合 +int main() +{ + while(scanf("%d%d",&n,&c)!=EOF) + { + for(int i=0;i0) + { + int t=sum/num[i].x; + if(t==0) + continue; + t=min(t,num[i].y); + need[i]=t; + sum-=t*num[i].x; + } + } + + if(sum>0)//如果在上面的组合中没有使sum变为0,则从小到大选择最小的面值组合,这样使面值超过的最小化 + { + for(int i=n-1;i>=0;i--) + if(num[i].y&&num[i].x>=sum) + { + sum=0; + need[i]++; + break; + } + } + if(sum>0)//如果组合不能达到c,怎么说明搜索结束 + break; + int cc=2e9; + for(int i=n-1;i>=0;i--)//找出这个组合成的面值中最小的数量,同时减去它们。 + { + if(need[i]) + { + cc=min(cc,num[i].y/need[i]); + } + } + kj+=cc; + for(int i=0;i +#include +#include +#include +using namespace std; + +typedef pair Coin; // 硬币 面值和数量 +Coin coin[20]; +int need[20]; + +///////////////////////////SubMain////////////////////////////////// +int main(int argc, char *argv[]) +{ +#ifndef ONLINE_JUDGE + freopen("in.txt", "r", stdin); + freopen("out.txt", "w", stdout); +#endif + int N, C; + cin >> N >> C; + for (int i = 0; i < N; ++i) + { + cin >> coin[i].first >> coin[i].second; + } + int week = 0; + // 面额不小于C的一定可以支付一周 + for (int i = 0; i < N; ++i) + { + if (coin[i].first >= C) + { + week += coin[i].second; + coin[i].second = 0; + } + } + sort(coin, coin + N, greater()); + while(true) + { + int sum = C; // 等待凑足的sum + memset(need, 0, sizeof(need)); + // 从大到小 + for (int i = 0; i < N; ++i) + { + if (sum > 0 && coin[i].second > 0) + { + int can_use = min(coin[i].second, + sum / coin[i].first); + if (can_use > 0) + { + sum -= can_use * coin[i].first; + need[i] = can_use; + } + } + } + // 从小到大 + for (int i = N - 1; i >= 0; --i) + { + if (sum > 0 && coin[i].second > 0) + { + int can_use = min(coin[i].second - need[i], // 上个loop用掉了一些 + (sum + coin[i].first - 1) / coin[i].first); // 允许多出不超过一个面值的金额 + if (can_use > 0) + { + sum -= can_use * coin[i].first; + need[i] += can_use; + } + } + } + + if(sum > 0) + { + break; + } + + int add_up = numeric_limits::max(); // 凑起来的week数 + // add_up多少个最优的week 受限于 每种面值能满足最优解下的需求个数多少次 + for (int i = 0; i < N; ++i) + { + if (need[i] == 0) + { + continue; + } + add_up = min(add_up, coin[i].second / need[i]); + } + week += add_up; + // 最优解生效,更新剩余硬币数量 + for (int i = 0; i < N; ++i) + { + if (need[i] == 0) + { + continue; + } + coin[i].second -= add_up * need[i]; + } + } + cout << week << endl; +#ifndef ONLINE_JUDGE + fclose(stdin); + fclose(stdout); + system("out.txt"); +#endif + return 0; +} +///////////////////////////End Sub////////////////////////////////// diff --git a/POJ/3040_tuicool.cpp b/POJ/3040_tuicool.cpp new file mode 100644 index 0000000..2cc1eb2 --- /dev/null +++ b/POJ/3040_tuicool.cpp @@ -0,0 +1,81 @@ +#include +#include +#include +#include +#include +const int INF=0x3f3f3f3f; +using namespace std; + +struct node +{ + int val,mou;//面值和数目 +} mon[25]; +int n,c; +int need[25];//一种方案所需各面值的数目 +bool cmp(node a,node b) +{ + return a.val=0;i--) + if(mon[i].val>=c)//如果面值大于c直接加数目就行了 + ans+=mon[i].mou; + else + { + lim=i; + break; + } + while(1) + { + memset(need,0,sizeof need); + rest=c; + for(i=lim;i>=0;i--)//尽可能用大面值凑够 + { + if(!mon[i].mou||!rest) + continue; + ti=rest/mon[i].val; + ti=min(ti,mon[i].mou); + need[i]=ti; + rest-=ti*mon[i].val; + } + if(rest)//没凑够的话。只能往回找最小的面值凑够。保证损失的钱最少 + { + for(i=0;i<=lim;i++) + { + if(mon[i].val>=rest&&(mon[i].mou-need[i])) + { + need[i]++; + rest=0; + break; + } + } + if(rest) + break; + } + mday=INF; + for(i=0;i<=lim;i++) + if(need[i]) + mday=min(mday,mon[i].mou/need[i]);//得出该种方案能支付的最大周数 + ans+=mday; + for(i=0;i<=lim;i++) + mon[i].mou-=mday*need[i]; + } + printf("%d\n",ans); + } + return 0; +} +/* +11 +6 1 +4 1 +*/ diff --git a/POJ/3190.cpp b/POJ/3190.cpp new file mode 100644 index 0000000..2615acb --- /dev/null +++ b/POJ/3190.cpp @@ -0,0 +1,74 @@ + +#include +#include +#include +#include +using namespace std; + + +struct cow +{ + int start,over; + int pos; + bool operator <(const cow& b) const + { + if(over==b.over) + return start>b.start; + return over>b.over; + } +}; + +bool cmp(const cow& a,const cow& b) +{ + if(a.start==b.start) return a.over bus; +int use[50001]; +cow cowx[50001]; + +int cnt=1; + +int main() +{ + int n; + while(scanf("%d",&n)==1) + { + + for(int i=0; i +#include +#include +#include +#include +using namespace std; +const int maxn=60000; +int n,use[maxn]; +struct Node +{ + int l; + int r; + int pos; + bool operator <(const Node &a)const + { + if(r==a.r) + return l>a.l; + return r>a.r; + } +}a[maxn]; +priority_queue q; +bool cmp(Node a,Node b) +{ + if(a.l==b.l) + return a.r +#include +#include +#include +using namespace std; + +#define MAXM 100000 +#define MAXN 20000 +struct ppk +{ +    int a,b,c; +}; +bool cmp(const ppk& a,const ppk& b) +{ +    return a.c>b.c; +} +int f[MAXN*2]; +ppk X[MAXM]; + +int get(int x) +{ +    if(x==f[x]) return x; +    else +    { +        f[x]=get(f[x]); +        return f[x]; +    } +} + +int main() +{ +    int n,m; +    scanf("%d %d",&n,&m); +    for(int i=1;i<=n*2;i++) +    { +        f[i]=i; +    } +    for(int i=0;i +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +const int N=2e6+7; +int a[N],b[N]; +long long sum[N]; +int main() +{ + //freopen("in.txt","r",stdin); + int n,x; + while(~scanf("%d",&n)) + { + int ma=0; + memset(a,0,sizeof(a)); + memset(b,0,sizeof(b)); + memset(sum,0,sizeof(sum)); + for(int i=0;i +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +const int N=100005; +int n,a[N],b[N],tree[N<<2]; +void build1(int node,int l,int r) +{ + if(l==r) + { + tree[node]=N; + return ; + } + int m=(l+r)>>1; + build1(node<<1,l,m); + build1(node<<1|1,m+1,r); + tree[node]=min(tree[node<<1],tree[node<<1|1]); +} +void update1(int node,int l,int r,int p) +{ + if(l==r) + { + tree[node]=b[p]; + return ; + } + int m=(l+r)>>1; + if(a[p]<=m) update1(node<<1,l,m,p); + else update1(node<<1|1,m+1,r,p); + tree[node]=min(tree[node<<1],tree[node<<1|1]); +} +int query1(int node,int l,int r,int p) +{ + if(l>=p) return tree[node]; + int m=(l+r)>>1,ans; + ans=query1(node<<1|1,m+1,r,p); + if(p<=m) ans=min(ans,query1(node<<1,l,m,p)); + return ans; +} +void build2(int node,int l,int r) +{ + if(l==r) + { + tree[node]=0; + return ; + } + int m=(l+r)>>1; + build2(node<<1,l,m); + build2(node<<1|1,m+1,r); + tree[node]=max(tree[node<<1],tree[node<<1|1]); +} +void update2(int node,int l,int r,int p) +{ + if(l==r) + { + tree[node]=b[p]; + return ; + } + int m=(l+r)>>1; + if(a[p]<=m) update2(node<<1,l,m,p); + else update2(node<<1|1,m+1,r,p); + tree[node]=max(tree[node<<1],tree[node<<1|1]); +} +int query2(int node,int l,int r,int p) +{ + if(r<=p) return tree[node]; + int m=(l+r)>>1,ans; + ans=query2(node<<1,l,m,p); + if(p>m) ans=max(ans,query2(node<<1|1,m+1,r,p)); + return ans; +} +int main() +{ + //freopen("in.txt","r",stdin); + while(~scanf("%d",&n)) + { + int x,flag=1; + for(int i=1; i<=n; i++) + { + scanf("%d",&x); + a[x]=i; + } + for(int i=1; i<=n; i++) + { + scanf("%d",&x); + b[x]=i; + } + build1(1,1,n); + for(int i=n; i>0; i--) + { + if(a[i]<=b[i]&&b[i]>query1(1,1,n,a[i])) + { + flag=0; + break; + } + update1(1,1,n,i); + } + if(!flag) + { + printf("NO\n"); + continue; + } + build2(1,1,n); + for(int i=1; i<=n; i++) + { + if(a[i]>=b[i]&&b[i] +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +#define inf 0x3f3f3f3f +#define maxn 1010 +struct node +{ + int v,d; + node(int a=0,int b=0):v(a),d(b){} + bool operator < (const node &a) const + { + return d>a.d; + } +}; +struct edge +{ + int v,cost; + edge(int a=0,int b=0):v(a),cost(b){} +}; +vector eg[maxn]; +bool vis[maxn]; +int n,m,s,a,b,t,c,dis[maxn],ss[maxn],tt[maxn]; +void dijkstra(int x) +{ + memset(vis,false,sizeof(vis)); + for(int i=0; i<=n; i++) + dis[i]=inf; + priority_queueq; + while(!q.empty()) q.pop(); + dis[x]=0; + q.push(node(x,0)); + node tmp; + while(!q.empty()) + { + tmp=q.top(); + q.pop(); + int u=tmp.v; + if(vis[u]) continue; + vis[u]=true; + for(int i=0; idis[u]+cost) + { + dis[v]=dis[u]+cost; + q.push(node(v,dis[v])); + } + } + } +} +int main() +{ + //freopen("in.txt","r",stdin); + while(~scanf("%d%d%d%d",&n,&m,&s,&t)&&n) + { + for(int i=1;i<=n;i++) + eg[i].clear(); + while(m--) + { + scanf("%d%d%d",&a,&b,&c); + eg[a].push_back(edge(b,c)); + eg[b].push_back(edge(a,c)); + } + for(int i=1;i<=s;i++) + scanf("%d",&ss[i]); + for(int i=1;i<=t;i++) + scanf("%d",&tt[i]); + int ans=inf; + for(int i=1;i<=s;i++) + { + dijkstra(ss[i]); + for(int j=1;j<=t;j++) + ans=min(ans,dis[tt[j]]); + } + if(ans!=inf) printf("%d\n",ans); + else printf("What a pity!\n"); + } + return 0; +} diff --git a/SDUSTOJ/1800_cnblogs.cpp b/SDUSTOJ/1800_cnblogs.cpp new file mode 100644 index 0000000..ee07cf3 --- /dev/null +++ b/SDUSTOJ/1800_cnblogs.cpp @@ -0,0 +1,50 @@ +/* *********************************************** +Author :devil +Created Time :2016/4/26 21:58:52 +************************************************ */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +const int N=1010; +int dp[N],a[N],b[N],c[N]; +int main() +{ + //freopen("in.txt","r",stdin); + int t,n; + scanf("%d",&t); + while(t--) + { + scanf("%d",&n); + for(int i=1; i<=n; i++) + scanf("%d",&a[i]); + int ma=0; + memset(c,0,sizeof(c)); + c[1]=n; + for(int i=1; i<=n; i++) + { + b[i]=dp[i]=1; + for(int j=1; ja[j]&&dp[i]a[j]&&dp[i]==dp[j]+1) b[i]+=b[j]; + } + if(dp[i]>1) c[dp[i]]+=b[i]; + ma=max(ma,dp[i]); + } + printf("%d\n",c[ma]); + } + return 0; +} diff --git a/SDUSTOJ/1801_cnblogs.cpp b/SDUSTOJ/1801_cnblogs.cpp new file mode 100644 index 0000000..ecfca56 --- /dev/null +++ b/SDUSTOJ/1801_cnblogs.cpp @@ -0,0 +1,62 @@ +/* *********************************************** +Author :devil +Created Time :2016/4/26 22:18:57 +************************************************ */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +const int N=110; +const int mod=1e9+9; +LL dp[N][N],dp2[N][N]; +int a[N],b[N]; +int main() +{ + //freopen("in.txt","r",stdin); + int t,n,m; + scanf("%d",&t); + while(t--) + { + scanf("%d%d",&n,&m); + for(int i=1; i<=n; i++) + scanf("%d",&a[i]); + memset(dp,0,sizeof(dp)); + memset(dp2,0,sizeof(dp2)); + memset(b,-1,sizeof(b)); + b[a[1]]=dp[a[1]][1]=1; + for(int i=2;i<=n;i++) + { + dp[a[i]][1]=1; + int mi=min(i,m); + for(int k=2;k<=mi;k++) + { + for(int j=1;j=mod) dp[a[i]][k]-=mod; + } + if(b[a[i]]!=-1) + { + dp[a[i]][k]-=dp2[a[i]][k]; + if(dp[a[i]][k]<0) dp[a[i]][k]+=mod; + } + dp2[a[i]][k]=dp[a[i]][k]; + } + b[a[i]]=i; + } + LL ans=0; + for(int i=1;i<=n;i++) + ans=(ans+dp[i][m])%mod; + printf("%lld\n",ans); + } + return 0; +} diff --git a/SDUSTOJ/1802.cpp b/SDUSTOJ/1802.cpp new file mode 100644 index 0000000..5e6c2ec --- /dev/null +++ b/SDUSTOJ/1802.cpp @@ -0,0 +1,69 @@ +#include +#include +#include +using namespace std; +/* +bool check(int inc) +{ + if(inc%36!=0) return false; + char buff[32]; + memset(buff,0,32); + sprintf(buff,"%d",inc); + if(strstr(buff,"36")==NULL) return false; + int len=strlen(buff); + for(int i=0;i +#include +#include +#include +using namespace std; + +#define MAXM 100000 +#define MAXN 20000 +struct ppk +{ + int a,b,c; +}; +bool cmp(const ppk& a,const ppk& b) +{ + return a.c>b.c; +} +int f[MAXN*2]; +ppk X[MAXM]; + +int get(int x) +{ + if(x==f[x]) return x; + else + { + f[x]=get(f[x]); + return f[x]; + } +} + +int main() +{ + int n,m; + scanf("%d %d",&n,&m); + for(int i=1;i<=n*2;i++) + { + f[i]=i; + } + for(int i=0;i +#include +#include +#include +#include + +using namespace std; + +const int M = 100000; +const int N = 20000; + +struct EDGE { + int a; + int b; + int v; +} edge[M]; + +int f[N * 2]; + +bool Compare(const EDGE &a, const EDGE &b) +{ + return a.v < b.v; +} + +int Find(int x) +{ + if (f[x] == x) { + return f[x]; + } else { + return f[x] = Find(f[x]); + } +} + +int main() +{ + int n, m; + + ios::sync_with_stdio(false); + + cin >> n; + cin >> m; + for (int i = 0; i < m; i++) { + cin >> edge[i].a; + cin >> edge[i].b; + cin >> edge[i].v; + } + + for (int i = 0; i < n * 2; i++) { + f[i] = i; + } + + sort(edge, edge + m, Compare); + + for (int i = m - 1; i >= 0; i--) { + int x = Find(edge[i].a); + int y = Find(edge[i].b); + + if (x == y) { + cout << edge[i].v << endl; + + return 0; + } + + int xx = Find(edge[i].a + n); + int yy = Find(edge[i].b + n); + + f[x] = yy; + f[y] = xx; + } + + cout << "0" << endl; + + return 0; +} diff --git a/TYVJ/1403_hzwer.cpp b/TYVJ/1403_hzwer.cpp new file mode 100644 index 0000000..840b165 --- /dev/null +++ b/TYVJ/1403_hzwer.cpp @@ -0,0 +1,40 @@ +#include +#include +using namespace std; +int n,m,f[40001],x,y; +struct data +{ +int a,b,c; +}e[100001]; +int gz(const data &a,const data &b) +{ + if(a.c>b.c)return 1; + else return 0; +} +int find(int x) +{ + return f[x]==x?x:f[x]=find(f[x]); +} +int main() +{ + cin>>n>>m; + for(int i=1;i<=m;i++) + cin>>e[i].a>>e[i].b>>e[i].c; + for(int i=1;i<=n*2;i++) + f[i]=i; + sort(e+1,e+m+1,gz); + for(int i=1;i<=m;i++) + { + x=find(e[i].a); + y=find(e[i].b); + if(x==y) + { + cout<