[51Nod] 51Nod 被虐之路

由于本Cho太过懒惰,不再更新之前已经AC以及个人认为没有很大必要写的题目;

请善用 Ctrl + F qwq

排名先后仅看做题顺序

 

基础题

#1.1 1085 背包问题

01背包模板

 1 #include<cstdio>
 2 #include<iostream>
 3 using namespace std;
 4 
 5 int n,maxx,DP[1000000],w,v;
 6 
 7 int main(){
 8     scanf("%d%d",&n,&maxx);
 9     
10     for(int i = 1;i <= n;i++){
11         scanf("%d%d",&w,&v);
12         for(int j = maxx;j >= w;j--)
13             DP[j] = max(DP[j],DP[j-w]+v);
14     }
15     
16     cout << DP[maxx];
17     
18     return 0;
19 }
01背包 + 一维表示

#1.2 1086 背包问题 V2

多重背包 + 二进制优化

什么是二进制优化呢?当前物品有数量 c ,将其拆分成 1 2 4 8 16 ... (剩下) ,然后一个个做01背包

决策每一个数字的时候 (1,2,4...) ,选与不选,相当于该物品已有数量加不加当前数字,而我们将决策 c 的二进制表示的每一位,那么所有的 1,2,4,... 是可以组合表示出 ≤c 的所有数量的状态的

这样就优化成一层 log 

 1 #include<cstdio>
 2 #include<iostream>
 3 using namespace std;
 4 
 5 int n,maxx,DP[1000000],w,v,c;
 6 
 7 int main(){
 8     scanf("%d%d",&n,&maxx);
 9     
10     for(int i = 1;i <= n;i++){
11         scanf("%d%d%d",&w,&v,&c);
12         int d = 1;
13         while(d <= c){
14             for(int j = maxx;j >= d*w;j--)
15                 DP[j] = max(DP[j],DP[j-d*w]+d*v);
16             c -= d;
17             d *= 2;
18         }for(int j = maxx;c && j >= c*w;j--)
19             DP[j] = max(DP[j],DP[j-c*w]+c*v);
20     }
21     
22     cout << DP[maxx];
23     
24     return 0;
25 }
二进制优化 + 一维表示

#1.3 1257 背包问题 V3

跟分数规划打了场遭遇战= =

数学分析目标式,二分答案

此处解题报告 [51nod] 1257 背包问题 V3

 1 #include<stdio.h>
 2 #include<math.h>
 3 #include<iostream>
 4 #include<algorithm>
 5 using namespace std;
 6 
 7 int n,k;
 8 long long ansv,answ;
 9 
10 double eps = 0.000000001;
11 
12 struct data{
13     long long w,v;
14     double vrw;
15 }arr[1000000];
16 
17 bool cmp(const data &a,const data &b){ return a.vrw > b.vrw; }
18 
19 bool check(long long &sumv,long long &sumw,double line){
20     for(int i = 1;i <= n;i++)
21         arr[i].vrw = 1.0*arr[i].v-line*arr[i].w;
22     
23     sort(arr+1,arr+1+n,cmp);
24     
25     double tmp = 0;
26     sumv = 0,sumw = 0;
27     for(int i = 1;i <= k;i++)
28         sumv += arr[i].v,
29         sumw += arr[i].w,
30         tmp += arr[i].vrw;
31     
32     return tmp >= 0;
33 }
34 
35 long long gcd(long long a,long long b){
36     if(a < b) swap(a,b);
37     return !b?a:gcd(b,a%b);
38 }
39 
40 int main(){
41     
42     scanf("%d%d",&n,&k);
43     
44     for(int i = 1;i <= n;i++)
45         scanf("%lld%lld",&arr[i].w,&arr[i].v);
46     
47     double L = 0,R = 1000000;
48     long long sumv,sumw;
49     while(R-L > eps){
50 //        printf("%.8lf %.8lf\n",L,R);
51         double mid = (L+R)/2;
52         if(check(sumv,sumw,mid)) ansv = sumv, answ = sumw, L = mid;
53         else R = mid;
54     }
55     
56     long long d = gcd(ansv,answ);
57     printf("%lld/%lld",ansv/d,answ/d);
58     
59     return 0;
60 }
分数规划

#2.1 1136 欧拉函数

Emmmm,现在有 m 与 p 互质且 p 为质数,那么 phi( p ) = p-1 ,phi( mp ) = phi( m )*phi( p ) = phi( m )*( p-1 )

然后对于 phi( mpp ) = phi( mp )*p

Emmmm 这个我居然没法证明

我们知道如果 phi( mpp ) = phi ( mp )* ??? ,那么 ??? = phi( mpp ) / phi( mp )

而 mpp 和 mp 的质因子是一样的

所以根据公式 phi ( x ) = x*(1-.........) 我们知道对于 x 后面的部分,phi ( mpp ) 和 phi ( mp )是相同的,那么两元相除得 phi ( mpp ) / phi ( mp ) = p

 1 #include<stdio.h>
 2 #include<iostream>
 3 using namespace std;
 4 
 5 //int prime[100000],primesize;
 6 //bool isprime[100000];
 7 //void getlist(int listsize){
 8 //    listsize = (int)sqrt(listsize)+10;
 9 //    isprime[
10 //}
11 
12 int phi(int &x){
13     int ans = 1;
14     for(int i = 2;i*i <= x;i++){
15         if(x%i) continue;
16         ans *= (i-1);
17         x /= i;
18         while(x%i == 0){
19             ans *= i;
20             x /= i;
21         }
22     }if(x > 1) ans *= x-1; // 这条和 i*i <= x 可以防止x为大质数导致TLE
23     return ans;
24 }
25 
26 int main(){
27     int n;
28     scanf("%d",&n);
29     
30     cout << phi(n);
31     
32     return 0;
33 }
朴素欧拉函数

#3.1 1242 斐波那契数列的第N项

矩阵快速幂

 1 #include<cstdio>
 2 #include<iostream>
 3 #define mod 1000000009
 4 using namespace std;
 5 
 6 struct MAT{
 7     long long d[5][5];
 8     MAT(){ d[1][1] = d[1][2] = d[2][1] = d[2][2] = 0; }
 9     void Init(){ 
10         d[1][2] = d[2][1] = d[2][2] = 1;
11         d[1][1] = 0;
12     }
13 }I;
14 
15 MAT mul(MAT A,MAT B){
16     MAT C;
17     for(int i = 1;i <= 2;i++)
18         for(int j = 1;j <= 2;j++)
19             for(int k = 1;k <= 2;k++)
20                 C.d[i][j] = (C.d[i][j]+A.d[i][k]*B.d[k][j]%mod)%mod;
21     return C;
22 }
23 
24 MAT ksm(MAT A,long long k){
25     MAT B = A;
26     k--;
27     while(k){
28         if(k&1) B = mul(B,A);
29         A = mul(A,A);
30         k >>= 1;
31     }return B;    
32 }
33 
34 int main(){
35     long long n;
36     scanf("%lld",&n);
37     
38     if(n <= 2) return printf("1"),0;
39     
40     MAT I;
41     I.Init();
42     
43     I = ksm(I,n-1);
44     
45     long long c = (I.d[1][1]+I.d[1][2])%mod;
46     
47     cout << c;
48     
49     return 0;
50 }
矩阵快速幂

#4 1106 质数检测

Miller-Rabbin 素性测试,二次探测才是核心 

 1 #include<stdio.h>
 2 #include<iostream>
 3 #define LL long long
 4 using namespace std;
 5 
 6 int prime[8] = {2,3,5,7,11,13,17,19};
 7 
 8 LL ksm(LL x,LL k,LL m){
 9     if(k == 1) return x%m;
10     if(!k) return 1;
11     LL b = 1;
12     while(k){
13         if(k&1) b = b*x%m;
14         x = x*x%m;
15         k >>= 1;
16     }return b%m;
17 }
18 
19 bool TD(LL x,LL a){
20     if(x == a) return true;
21     LL m = x-1;
22     while(m % 2 == 0) m >>= 1;
23     LL t = ksm(a,m,x);
24     while(m != x-1 && t != 1 && t != x-1)
25         t = t*t%x,m <<= 1;
26     return (m&1 || t == x-1);
27 }
28 
29 bool isPrime(LL x){
30     if(x == 2) return true;
31     if(x < 1 || x % 2 == 0) return false;
32     for(int i = 0;i < 8;i++)
33         if(!TD(x,prime[i])) return false;
34     return true;
35 }
36 
37 int main(){
38     int n;
39     scanf("%d",&n);
40     
41     for(int i = 1;i <= n;i++){
42         long long x; scanf("%lld",&x);
43         if(isPrime(x)) printf("Yes\n");
44         else printf("No\n");
45     }
46     
47     return 0;
48 }
Miller Rabbin

 #5 1088 最长回文子串

Hash+二分,具体请看这里:

 1 #include<stdio.h>
 2 #include<iostream>
 3 #include<cstring>
 4 #define maxn 421357
 5 #define base 21707
 6 #define ULL unsigned long long
 7 using namespace std;
 8 
 9 ULL stad[maxn],anti[maxn];
10 
11 int n,ans = 1,lens; char s[maxn];
12 
13 ULL Pow(ULL x,int k){
14     if(!k) return 1;
15     if(k == 1) return x;
16     ULL b = 1;
17     while(k){
18         if(k&1) b = b*x;
19         x = x*x;
20         k >>= 1;
21     }return b;
22 }
23 
24 ULL getcode(int L,int R){
25     if(!L) return stad[R];
26     else return stad[R]-stad[L-1]*Pow(base,R-L+1);
27 }
28 
29 ULL getanti(int L,int R){
30     if(R == n) return anti[L];
31     else return anti[L]-anti[R+1]*Pow(base,R-L+1);
32 }
33 
34 int find(int pos){
35     int L = 0,R = min(lens-1-pos,pos),mid;
36     while(L < R){
37         mid = (L+R+1)/2;
38 //        printf("Checking... [%d , %d]\n",pos-mid,pos+mid);
39 //        ULL cntcode = (getanti(pos+1,pos+mid)*base+s[pos])*Pow(base,mid)+getcode(pos+1,pos+mid);
40         if(getcode(pos-mid,pos-1) == getanti(pos+1,pos+mid)) L = mid;
41         else R = mid-1;
42     }return L*2+1;
43 }
44 
45 int exfind(int pos){
46     if(pos == lens-1) return 0;
47     int L = 0,R = min(lens-1-pos,pos+1),mid;
48     while(L < R){
49         mid = (L+R+1)/2;
50 //        printf("Checking... [%d , %d]\n",pos-mid,pos+mid);
51 //        ULL cntcode = (getanti(pos+1,pos+mid)*base+s[pos])*Pow(base,mid)+getcode(pos+1,pos+mid);
52         if(getcode(pos-mid+1,pos) == getanti(pos+1,pos+mid)) L = mid;
53         else R = mid-1;
54     }return L*2;
55 }
56 
57 int main(){
58     scanf("%s",s);
59     lens = strlen(s);
60     
61     ULL cntcode = 0;
62     for(int i = 0;i < lens;i++)
63         cntcode = cntcode*base+s[i],
64         stad[i] = cntcode;
65         
66     cntcode = 0;
67     for(int i = lens-1;i >= 0;i--)
68         cntcode = cntcode*base+s[i],
69         anti[i] = cntcode;
70         
71     for(int pos = 0;pos < lens;pos++)
72         ans = max(ans,find(pos)),
73         ans = max(ans,exfind(pos));
74     cout << ans;
75     
76     return 0;
77 }
Hash+二分

 

3级算法题

#1 1267 4个数和为0

 Hash+加强的check函数

 1 #include<stdio.h>
 2 #include<iostream>
 3 #define ULL unsigned long long
 4 #define maxn 1000007
 5 using namespace std;
 6 
 7 struct data{ ULL val,pos; }sum[maxn];
 8 int m,n,arr[maxn];
 9 
10 struct edge{ ULL val,pos; int from; }e[maxn*4];
11 int tot,first[maxn];
12 
13 bool noequ(ULL A,ULL B){
14     return (A/10000 != B/10000) && (A%10000 != B%10000);
15 }
16 
17 bool check(ULL val,ULL pos){
18     int w = val%maxn;
19     for(int i = first[w];i;i = e[i].from){
20         if(e[i].val == val && noequ(e[i].pos,pos)) return true;
21     }return false;
22 }
23 
24 void insert(int pos1,int pos2,int suuum){
25     if(pos1 > pos2) swap(pos1,pos2);
26     ULL val = (ULL)(suuum+1e9);
27     ULL kcode = pos1*10000+pos2;
28     
29     if(check(val,kcode)) return;
30     
31     int w = val%maxn;
32     
33     tot++; e[tot].from = first[w];
34     e[tot].val = val; e[tot].pos = kcode;
35     first[w] = tot;
36     sum[++m].val = val;
37     sum[m].pos = kcode;
38 }
39 
40 int main(){
41     scanf("%d",&n);
42     
43     for(int i = 1;i <= n;i++)
44         scanf("%d",&arr[i]);
45     
46     for(int i = 1;i <= n;i++)
47         for(int j = i+1;j <= n;j++)
48             insert(i,j,arr[i]+arr[j]);
49     
50 //    for(int i = 1;i <= m;i++) printf("%I64u %I64u\n",sum[i].val,sum[i].pos);
51 //    
52 //    cout << "Hash:" << endl;
53 //    
54 //    for(int i = 1;i <= tot;i++) printf("%I64u %I64u\n",e[i].val,e[i].pos);
55     
56     for(int i = 1;i <= m;i++){
57         if(check(2e9-sum[i].val,sum[i].pos)){
58             cout << "Yes";
59             return 0;
60         }
61     }
62     
63     cout << "No";
64     
65     return 0;
66 }
Hash+补丁

 

4级算法题

#1 1113 矩阵快速幂

蜜汁4级,,,敲一个模板即可过。注意传参的时候推荐使用引用传参而不是直接传参。

 1 #include<stdio.h>
 2 #include<iostream>
 3 #define LL long long
 4 using namespace std;
 5 
 6 LL mod = 1000000007;
 7 int n,k;
 8 
 9 struct mat{
10     LL d[200][200];
11 //    mat(){ for(int i = 1;i <= n;i++) for(int j = 1;j <= n;j++) d[i][j] = 0; }
12 };
13 
14 mat mul(mat &A,mat &B){
15     mat C;
16     for(int i = 1;i <= n;i++){
17         for(int j = 1;j <= n;j++){
18             C.d[i][j] = 0;
19             for(int k = 1;k <= n;k++)
20                 C.d[i][j] = (C.d[i][j]+A.d[i][k]*B.d[k][j])%mod;
21         }
22     }return C;
23 }
24 
25 mat ksm(mat &A,int k){
26 //    cout<<k<<endl;
27     if(k == 1) return A;
28 //    cout<<k<<endl;
29     mat B = A; k--;
30     while(k){
31 //    cout<<k<<endl;
32         if(k&1) B = mul(B,A);
33         A = mul(A,A);
34         k >>= 1;
35     }return B;
36 }
37 
38 int main(){
39     scanf("%d%d",&n,&k);
40     
41     mat ans;
42     for(int i = 1;i <= n;i++)
43         for(int j = 1;j <= n;j++)
44             scanf("%lld",&ans.d[i][j]);
45     
46     ans = ksm(ans,k);
47 //    return 233;
48     
49     for(int i = 1;i <= n;i++){
50         for(int j = 1;j <= n;j++)
51             printf("%lld ",ans.d[i][j]%mod);
52         cout << endl;
53     }
54     
55     return 0;
56 }
矩阵快速幂

#2 1051 最大子矩阵和

Emmmmmm 解法比较坑(说明我的基础已经被steam腐蚀殆尽了qwq)

首先给个优化:用前缀和储存子矩阵的和,于是枚举子矩阵的顶行和底行,那么我们就把这个矩阵在计算的时候在某种意义上压成了一行

嘿嘿嘿,然后就是最大字段和了

 1 #include<stdio.h>
 2 #include<iostream>
 3 using namespace std;
 4 
 5 int n,m;
 6 long long cnt,ans,sum[505][505] = {0};
 7 
 8 int main(){
 9     scanf("%d%d",&m,&n);
10     
11     for(int i = 1;i <= n;i++){
12         for(int j = 1;j <= m;j++){
13             scanf("%lld",&sum[i][j]);
14             sum[i][j] += sum[i-1][j];
15         }
16     }
17     
18 //    for(int i = 1;i <= n;i++){
19 //        for(int j = 1;j <= m;j++)
20 //            printf("%lld ",sum[i][j]);
21 //        printf("\n");
22 //    }
23     
24     for(int up = 0;up <= n-1;up++){
25         for(int dw = up+1;dw <= n;dw++){
26             cnt = 0;
27             for(int i = 1;i <= m;i++){
28                 cnt = max(cnt+sum[dw][i]-sum[up][i],sum[dw][i]-sum[up][i]);
29                 ans = max(ans,cnt);
30 //                if(cnt+(sum[dw][i]-sum[up][i]) < 0){
31 //                    ans = max(ans,cnt); cnt = 0;
32 //                }else cnt += sum[dw][i]-sum[up][i];
33 //                printf("#[%d-%d]%d: %lld\n",up,dw,i,cnt);
34             }
35 //            ans = max(ans,cnt);
36         }
37     }printf("%lld",ans);
38     
39     return 0;
40 }
最大子矩阵和

#3 1287 加农炮

一道标准线段树,,, 由于最近学了一些玄学操作一开始总是想到权值线段树,然后又想到平衡树,,, = =

然而标签二分法已经揭露了一切:只需要一个普通的区间最大值定位和单点修改即可

如果左区间的最大值能够挡下这颗炮弹就朝左边找,反之右边;再反之说明这颗炮弹太高,返回一个异常值即可,然后跳过

数据不坑

 1 #include<stdio.h>
 2 #include<iostream>
 3 #define mid (L+R)/2
 4 #define lc (rt<<1)
 5 #define rc (rt<<1|1)
 6 #define maxn 1000000
 7 using namespace std;
 8 
 9 int n,m,connon;
10 
11 struct node{
12     int maxx;
13 };
14 
15 struct SegmentTree{
16     int qL,qR,val,pos;
17     node T[maxn*4];
18     
19     void build(int rt,int L,int R){
20         if(L == R){ scanf("%d",&T[rt].maxx); }
21         else{
22             build(lc,L,mid); build(rc,mid+1,R);
23             T[rt].maxx = max(T[lc].maxx,T[rc].maxx);
24         }
25     }
26     
27     int query(int rt,int L,int R){
28         if(L == R) return L;
29         else{            
30             if(val <= T[lc].maxx) return query(lc,L,mid);
31             else if(val <= T[rc].maxx) return query(rc,mid+1,R);
32             else return 0;
33         }
34     }
35     
36     void modify(int rt,int L,int R){
37         if(L == R) T[rt].maxx++;
38         else{
39             if(pos <= mid) modify(lc,L,mid);
40             else modify(rc,mid+1,R);
41             T[rt].maxx = max(T[lc].maxx,T[rc].maxx);
42         }
43     }
44     
45     void print(int rt,int L,int R){
46         if(L == R) printf("%d ",T[rt].maxx);
47         else{ print(lc,L,mid); print(rc,mid+1,R); }
48     }
49     
50     void Print(){ print(1,1,n); }
51     
52     void Build(){ build(1,1,n); }
53     
54     int Query(int heigh){ val = heigh; return query(1,1,n); }
55 
56     void Modify(int Emmm){ pos = Emmm; modify(1,1,n); }
57 }A;
58 
59 int main(){
60     scanf("%d%d",&n,&m);
61     
62     A.Build();
63     
64     for(int i = 1;i <= m;i++){
65         scanf("%d",&connon);
66         int pos = A.Query(connon);
67         if(pos <= 1) continue;
68         A.Modify(pos-1);
69     }A.Print();
70     
71     return 0;
72 }
普通线段树

 #4 1463 找朋友

这道题,跟线段树有什么关系呢??????

正解:离线 + 扫描线

注释:枚举 K 集合,将答案保存至左端点,将右端点排升序,然后计算区间 [ L,R ] 里答案的最大值(此处即可用线段树优化)

这题实在好题,思路之巧妙搞得我都想写题解了

 1 #include<stdio.h>
 2 #include<iostream>
 3 #include<algorithm>
 4 #define mid (L+R)/2
 5 #define lc (rt << 1)
 6 #define rc (rt<<1|1)
 7 #define maxn 1000000
 8 using namespace std;
 9 
10 int arr[maxn],brr[maxn],cnt,bos[maxn],sto[maxn],k[maxn],n,m,q;
11 
12 struct node{
13     int maxx;
14 }T[maxn*4];
15 
16 void build(int rt,int L,int R){
17     if(L == R) T[rt].maxx = arr[L];
18     else{
19         build(lc,L,mid); build(rc,mid+1,R);
20         T[rt].maxx = max(T[lc].maxx,T[rc].maxx);
21     }
22 }
23 
24 void modify(int rt,int L,int R,int pos,int val){
25     if(L == R) T[rt].maxx = val,sto[pos] = val;
26     else{
27         if(pos > R || pos < 1) return;
28         if(pos <= mid) modify(lc,L,mid,pos,val);
29         else modify(rc,mid+1,R,pos,val);
30         T[rt].maxx = max(T[lc].maxx,T[rc].maxx);
31     }
32 }
33 
34 int query(int rt,int L,int R,int qL,int qR){
35     if(qL <= L && R <= qR) return T[rt].maxx;
36     else{
37         int ans = 0;
38         if(qL <= mid) ans = max(ans,query(lc,L,mid,qL,qR));
39         if(qR > mid) ans = max(ans,query(rc,mid+1,R,qL,qR));
40         return ans;
41     }
42 }
43 
44 struct ask{
45     int from,L,R,ans,ord; // first[R]
46 }e[maxn];
47 int tot,first[maxn];
48 void insert(int L,int R,int ord){
49     tot++;
50     e[tot].ord = ord,
51     e[tot].L = L,
52     e[tot].R = R,
53     e[tot].from = first[R];
54     first[R] = tot;
55 }
56 
57 bool cmp(const ask &A,const ask &B){ return A.ord < B.ord; }
58 
59 int main(){
60     scanf("%d%d%d",&n,&q,&m);
61     
62     for(int i = 1;i <= n;i++) scanf("%d",&arr[i]);
63     for(int i = 1;i <= n;i++) scanf("%d",&brr[i]),bos[brr[i]] = i;
64     for(int i = 1;i <= m;i++) scanf("%d",&k[i]);
65     
66     for(int i = 1;i <= q;i++){
67         int L,R;
68         scanf("%d%d",&L,&R);
69         insert(L,R,i);
70     }
71     
72 //    printf("#bos: "); for(int i = 1;i <= n;i++) printf("%d ",bos[i]); cout << endl;
73     
74     for(int i = 2;i <= n;i++){
75         for(int j = 1;j <= m;j++){
76             int ppos = bos[brr[i]-k[j]]; //printf("~#%d: ppos %d i-k[j] %d\n",brr[i],ppos,i-k[j]);
77             if(ppos >= 1 && ppos < i && sto[ppos] < arr[ppos]+arr[i])
78                 modify(1,1,n,ppos,arr[ppos]+arr[i]);
79             ppos = bos[brr[i]+k[j]]; //printf("~#%d: ppos %d i-k[j] %d\n",brr[i],ppos,i-k[j]);
80             if(ppos >= 1 && ppos < i && sto[ppos] < arr[ppos]+arr[i])
81                 modify(1,1,n,ppos,arr[ppos]+arr[i]);
82         }for(int p = first[i];p;p = e[p].from){
83             e[p].ans = query(1,1,n,e[p].L,e[p].R);
84         }//printf("#%d: ",i); for(int j = 1;j <= n;j++) printf("%d ",sto[j]); cout << endl;
85     }sort(e+1,e+1+tot,cmp);
86     
87     for(int i = 1;i <= tot;i++) printf("%d\n",e[i].ans);
88     
89     return 0;
90 }
离线 + 扫描线 + 线段树优化

 

#5 1076 2条不相交的路径

 

第一次写了判桥求边双...

第二次使用Tarjan求强联通的代码强行Ac...

 

 1 #include<stdio.h>
 2 #include<iostream>
 3 #define maxn 2000000
 4 using namespace std;
 5 
 6 int dfn[maxn],low[maxn],TIM,s[maxn],poi,color[maxn],COL,n,m,q;
 7 
 8 struct edge{ int from, u, v; }e[maxn];
 9 int tot,first[maxn]; void insert(int u,int v){
10     tot++; e[tot].from = first[u]; e[tot].v = v; e[tot].u = u; first[u] = tot;
11 }
12 
13 void dfs(int u,int fa){
14 //    printf("Now deep in #%d\n",u);
15     dfn[u] = low[u] = ++TIM;
16     s[poi++] = u;
17     for(int i = first[u];i;i = e[i].from){
18         int v = e[i].v; if(!dfn[v]){
19 //            printf("#%d; get in Poi B\n",u);
20             dfs(v,u); low[u] = min(low[u],low[v]);
21         }else if(dfn[v] < dfn[u] && v != fa) low[u] = min(low[u],dfn[v]);
22     }//printf("#%d: get in poi A\n",u);
23     
24     if(low[u] == dfn[u]){
25 //        printf("#%d get the first!\n",u);
26         color[u] = ++COL;
27         while(s[poi-1] != u && poi){
28             poi--; color[s[poi]] = COL;
29         }if(poi) poi--;
30     }
31 }
32 
33 int main(){
34     scanf("%d%d",&n,&m);
35     
36     for(int i = 1;i <= m;i++){
37         int u,v; scanf("%d%d",&u,&v);
38         insert(u,v); insert(v,u);
39     }for(int i = 1;i <= n;i++)
40         if(!color[i]) dfs(i,i);
41     
42 //    for(int i = 1;i <= n;i++) printf("%d ",color[i]); cout << endl;
43     
44     scanf("%d",&q);
45     
46     for(int i = 1;i <= q;i++){
47         int u,v; scanf("%d%d",&u,&v);
48         if(color[u] == color[v]) printf("Yes\n");
49         else printf("No\n");
50     }
51     
52     
53     
54     
55     return 0;
56 }
Tarjan求强联通

 

 1 #include<stdio.h>
 2 #include<iostream>
 3 #define maxn 1000000
 4 using namespace std;
 5 
 6 int dfn[maxn],low[maxn],TIM,COL,color[maxn],n,m,q;
 7 bool iscut[2*maxn],buck[maxn*2];
 8 
 9 struct edge{ int from,u,v,bri; }e[maxn*2];
10 int tot,first[maxn]; void insert(int u,int v){
11     tot++; e[tot].bri = 0; e[tot].u = u; e[tot].from = first[u]; e[tot].v = v; first[u] = tot;
12 }
13 
14 void dfs(int u,int fa){ // To tag the bri
15     dfn[u] = low[u] = ++TIM;
16     int child = 0;
17     for(int i = first[u];i;i = e[i].from){
18         int v = e[i].v; //if(v == fa) continue;
19         if(v != fa) child++;
20         if(!dfn[v]){
21             dfs(v,u);
22             low[u] = min(low[u],low[v]);
23             if(low[v] >= dfn[u]) iscut[u] = true;
24         }else if(dfn[v] < dfn[u] && v != fa) low[u] = min(low[u],dfn[v]);
25     }if(fa == u && child > 1) iscut[u] = false;
26 //    printf("#%d: child %d iscut %d\n",u,child,iscut[u]);
27 }
28 
29 void tagg(){
30     for(int i = 1;i <= tot;i++)
31         if(iscut[e[i].u] && iscut[e[i].v])
32             e[i].bri = 1, buck[i] = true;
33 }
34 
35 void dfs1(int u,int fa){
36     color[u] = COL;
37     for(int i = first[u];i;i = e[i].from){
38         if(e[i].v == fa || buck[i] || color[e[i].v]) continue;
39         dfs1(e[i].v,u);
40     }
41 }
42 
43 int main(){
44 //    freopen("16.txt","r",stdin);
45 //    freopen("16.out","w",stdout);
46     
47     scanf("%d%d",&n,&m);
48     
49     for(int i = 1;i <= m;i++){
50         int u,v; scanf("%d%d",&u,&v);
51         insert(u,v); insert(v,u);
52     }for(int i=1;i<=n;i++)if(!dfn[i])
53     dfs(i,i); for(int i = 1;i <= n;i++)
54         if(!e[first[i]].from) iscut[i] = true;
55 //    for(int i = 1;i <= n;i++) printf("%d ",iscut[i]); cout << endl;
56     tagg(); 
57     
58 //    for(int i = 1;i <= tot;i++) printf("#%d:%d->%d $%d$\n",i,e[i].u,e[i].v,buck[i]);
59 //    for(int i = 1;i <= n;i++) printf("%d ",color[i]); cout << endl;
60     
61     for(int i = 1;i <= n;i++)
62         if(!color[i]){ ++COL; dfs1(i,i); }
63     
64     scanf("%d",&q);
65     
66     for(int i = 1;i <= q;i++){
67         int u,v; scanf("%d%d",&u,&v);
68         if(color[u] == color[v]) printf("Yes\n");
69         else printf("No\n");
70     }
71     
72     return 0;
73 }
判桥求边双

 

 

5级算法题

#1 1494 选举拉票

Orz 这道题,当真是思考题

具体请看这里:[BZOJ] 1494 选举拉票 #算法设计策略

 1 #include<stdio.h>
 2 #include<queue>
 3 #include<algorithm>
 4 #include<iostream>
 5 #define mid (L+R)/2
 6 #define lc (rt<<1)
 7 #define rc (rt<<1|1)
 8 #define maxn 200000
 9 using namespace std;
10 
11 long long n,list[maxn],qwq;
12 bool vis[maxn];
13 
14 struct nodd{
15     long long num,sum;
16 }T[maxn*4];
17 
18 void modify(long long rt,long long L,long long R,long long pos,long long val){
19     if(pos < L || pos > R) return;
20     if(L == R) T[rt].num += val,T[rt].sum += val*pos;
21     else{
22         /*if(pos <= mid)*/ modify(lc,L,mid,pos,val);
23         /*else*/ modify(rc,mid+1,R,pos,val);
24         T[rt].num = T[lc].num+T[rc].num;
25         T[rt].sum = T[lc].sum+T[rc].sum;
26     }
27 }
28 
29 long long query(long long rt,long long L,long long R,long long pos){
30     if(pos <= 0) return 0;
31     if(pos > T[rt].num) return 2e9;
32     if(L == R) return L*pos;//T[rt].sum;
33     else if(pos <= T[lc].num) return query(lc,L,mid,pos);
34     else return T[lc].sum + query(rc,mid+1,R,pos-T[lc].num);
35 }
36 
37 priority_queue<long long,vector<long long> ,greater<long long> > heap[maxn];
38 
39 bool cmp1(long long A,long long B){ return heap[A].size() > heap[B].size(); }
40 
41 int main(){
42     scanf("%lld",&n);
43     
44     for(int i = 1;i <= n;i++){
45         long long x,y;
46         scanf("%lld%lld",&x,&y);
47         heap[x].push(y);
48         if(x&&!vis[x]) list[++qwq] = x,vis[x] = true;
49         if(x) modify(1,0,20000,y,1);
50     }
51     
52     sort(list+1,list+1+qwq,cmp1);
53 //    cout << "list";
54 //    for(int i = 1;i <= qwq;i++) printf("%d ",list[i]); cout << endl;
55     long long s = heap[0].size(),num = 0,sum = 0,ans = 2e9;
56     
57     for(int i = n;i >= max(1LL,s);i--){
58         for(int j = 1;j <= qwq;j++){
59             if(heap[list[j]].size() < i) break;
60             while(heap[list[j]].size() >= i){
61                 sum += heap[list[j]].top();
62                 modify(1,0,20000,heap[list[j]].top(),-1);
63                 heap[list[j]].pop();
64                 num++;
65             }
66         }ans = min(ans,sum+query(1,0,20000,i-(num+s)));
67 //        printf("#%d: ans %d\n",i,ans);
68     }
69     
70     printf("%lld",ans);
71     
72     return 0;
73 }
算法设计策略

#2 1199 Money out of Thin Air

使用线段树维护树的欧拉序

  1 #include<stdio.h>
  2 #include<iostream>
  3 #define mid (L+R)/2
  4 #define lc (rt<<1)
  5 #define rc (rt<<1|1)
  6 #define maxn 1000000
  7 #define LL long long
  8 using namespace std;
  9 
 10 int dfn[maxn],dfl[maxn],TIM,sz[maxn],fa[maxn],n,m;
 11 LL vval[maxn],arr[maxn],ans[maxn];
 12 
 13 struct edge{
 14     int from,v;
 15 }e[maxn]; int tot,first[maxn];
 16 void insert(int u,int v){ tot++; e[tot].from = first[u]; e[tot].v = v; first[u] = tot; }
 17 
 18 void dfs(int u){
 19     dfn[u] = ++TIM;
 20     arr[TIM] = vval[u];
 21     sz[u] = 1;
 22     for(int i = first[u];i;i = e[i].from){
 23         int v = e[i].v;
 24         dfs(v);
 25         sz[u] += sz[v];
 26     }dfl[u] = TIM;
 27 }
 28 
 29 struct node{
 30     LL sum,lazy;
 31 }T[maxn*4];
 32 
 33 void pushdown(int rt,int L,int R){
 34     if(!T[rt].lazy) return;
 35     T[lc].lazy += T[rt].lazy;
 36     T[rc].lazy += T[rt].lazy;
 37     T[lc].sum += T[rt].lazy*(mid-L+1);
 38     T[rc].sum += T[rt].lazy*(R-mid);
 39     T[rt].lazy = 0;
 40 }
 41 
 42 void build(int rt,int L,int R){ // 1,1,TIM
 43     if(L == R) T[rt].sum = arr[L];
 44     else{
 45         build(lc,L,mid); build(rc,mid+1,R);
 46         T[rt].sum = T[lc].sum+T[rc].sum;
 47     }
 48 }
 49 
 50 LL query(int rt,int L,int R,int qL,int qR){
 51     pushdown(rt,L,R);
 52     if(qL <= L && R <= qR) return T[rt].sum;
 53     else{
 54         LL ret = 0;
 55         if(qL <= mid) ret += query(lc,L,mid,qL,qR);
 56         if(qR > mid) ret += query(rc,mid+1,R,qL,qR);
 57         return ret;
 58     }
 59 }
 60 
 61 void modify(int rt,int L,int R,int qL,int qR,LL val){
 62     pushdown(rt,L,R);
 63     if(qL <= L && R <= qR){
 64         T[rt].sum += val*(R-L+1);
 65         T[rt].lazy += val;
 66     }else{
 67         if(qL <= mid) modify(lc,L,mid,qL,qR,val);
 68         if(qR > mid) modify(rc,mid+1,R,qL,qR,val);
 69         T[rt].sum = T[lc].sum + T[rc].sum;
 70     }
 71 }
 72 
 73 void print(int rt,int L,int R){
 74     pushdown(rt,L,R);
 75     if(L == R) ans[L] = T[rt].sum;
 76     else{
 77         print(lc,L,mid);
 78         print(rc,mid+1,R);
 79     }
 80 }
 81 
 82 void final(){
 83     for(int i = 1;i <= n;i++) printf("%lld\n",ans[dfn[i]]);
 84 }
 85 
 86 int main(){
 87 //    freopen("1.in","r",stdin);
 88     
 89     scanf("%d%d",&n,&m);
 90     
 91     for(int i = 2;i <= n;i++){
 92         scanf("%d%lld",&fa[i],&vval[i]);
 93         insert(fa[i]+1,i);
 94 //        insert(fa[i],i);
 95     }
 96     
 97     dfs(1);
 98     build(1,1,TIM);
 99     
100     for(int i = 1;i <= m;i++){
101         char ctr; int x; LL z; double y;
102         cin >> ctr; scanf("%d%lf%lld",&x,&y,&z);
103         
104         x++;
105         if(ctr == 'A'){
106             LL sum = query(1,1,TIM,dfn[x],dfl[x]);
107             if(1.0*sum/(dfl[x]-dfn[x]+1) < y) modify(1,1,TIM,dfn[x],dfl[x],z);
108         }else if(1.0*query(1,1,TIM,dfn[x],dfn[x]) < y) modify(1,1,TIM,dfn[x],dfn[x],z);
109 //        cout << "BBB" << endl;
110     }
111     
112     print(1,1,TIM);
113     final();
114     
115     return 0;
116 }
DFS序+线段树

#3 1378 夹克老爷的愤怒

树形DP

具体请看这里:[51nod] 1378 夹克老爷的愤怒 #树形DP

 1 #include<stdio.h>
 2 #include<iostream>
 3 #define maxn 1000000
 4 using namespace std;
 5 
 6 int DP[maxn][5],buoy[maxn],anch[maxn],depth[maxn],sz[maxn],n,k;
 7 
 8 struct edge{
 9     int from,v;
10 }e[maxn*2]; int tot,first[maxn];
11 void insert(int u,int v){ tot++; e[tot].from = first[u]; e[tot].v = v; first[u] = tot; }
12 
13 void dfs(int u,int pre){
14     int sum = 0; sz[u] = 1;
15     DP[u][0] = DP[u][1] = 0; buoy[u] = 1e9; anch[u] = -1e9;
16     for(int i = first[u];i;i = e[i].from){
17         int v = e[i].v; if(v == pre) continue;
18         depth[v] = depth[u]+1;
19         dfs(v,u);
20         sz[u] += sz[v];
21         buoy[u] = min(buoy[u],buoy[v]);
22         anch[u] = max(anch[u],anch[v]);
23         sum += DP[v][0];
24     }
25     
26     if(sz[u] == 1){
27         DP[u][1] = 1;
28         DP[u][0] = 0;
29         if(!k) DP[u][0] = 1;
30         buoy[u] = anch[u] = depth[u];
31         return;
32     }
33     
34 //    printf("#%d: buoy%d anch%d\n",u,buoy[u],anch[u]);
35     if(anch[u]-depth[u] >= k){ // Must set
36 //        printf("#%d: Poi A\n",u);
37         DP[u][0] = DP[u][1] = sum+1;
38         buoy[u] = depth[u]-k;
39         anch[u] = buoy[u]-1;
40     }else if(2*depth[u]-buoy[u] < anch[u]){ // Can choose
41 //        printf("#%d: Poi B\n",u);
42         DP[u][0] = sum;
43         DP[u][1] = sum+1;
44     }else{ // Needn't
45 //        printf("#%d: Poi C\n",u);
46         DP[u][0] = DP[u][1] = sum;
47         anch[u] = buoy[u]-1;
48     }
49 }
50 
51 int main(){
52     scanf("%d%d",&n,&k);
53     
54     for(int i = 1;i < n;i++){
55         int u,v; scanf("%d%d",&u,&v);
56         u++,v++; insert(u,v); insert(v,u);
57     }depth[1] = 1;
58     
59     dfs(1,1);
60     
61     printf("%d\n",DP[1][1]);
62     
63 //    for(int i = 1;i <= n;i++){
64 //        printf("#%d: 0:%d 1:%d buoy:%d anch:%d\n",i,DP[i][0],DP[i][1],buoy[i],anch[i]);
65 //    }
66     
67     return 0;
68 }
树形DP

 #4 1424 零树

树形DP,这道题是看着题解过的因此并不想写博客

DP[ u ][ 1/0 ]:1和0分别表示到这个点需要增加和减少的操作次数

显然,直接一次操作最值即可

= =

 1 #include<stdio.h>
 2 #include<iostream>
 3 #define maxn 1000000
 4 #define heng(x) (x<0?(-x):x)
 5 using namespace std;
 6 
 7 long long DP[maxn][5],val[maxn],n;
 8 
 9 struct edge{
10     int from,v;
11 }e[maxn*2]; int first[maxn],tot;
12 void insert(int u,int v){ tot++; e[tot].from = first[u]; e[tot].v = v; first[u] = tot; }
13 
14 void dfs(int u,int pre){
15     DP[u][0] = DP[u][1] = 0;
16 //    if(val[u] < 0) DP[u][0] = -val[u],DP[u][1] = 0;
17 //    else DP[u][0] = 0,DP[u][1] = val[u];
18     
19     for(int i = first[u];i;i = e[i].from){
20         int v = e[i].v; if(v == pre) continue;
21         dfs(v,u);
22         DP[u][0] = max(DP[u][0],DP[v][0]);
23         DP[u][1] = max(DP[u][1],DP[v][1]);
24     }int del = DP[u][1]-DP[u][0]+val[u];
25     DP[u][del<0] += heng(del);
26 }
27 
28 int main(){
29     scanf("%lld",&n);
30     for(int i = 1;i < n;i++){
31         int u,v; scanf("%d%d",&u,&v);
32         insert(u,v); insert(v,u);
33     }for(int i = 1;i <= n;i++) scanf("%lld",&val[i]);
34     
35     dfs(1,1);
36     
37     
38     printf("%lld",DP[1][0]+DP[1][1]);
39     
40     return 0;
41 }
树形DP = =

 

转载于:https://www.cnblogs.com/Chorolop/p/7716347.html

51nod 1352:集合计数 1352 集合计数 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题  收藏  关注 给出N个固定集合{1,N},{2,N-1},{3,N-2},...,{N-1,2},{N,1}.求出有多少个集合满足:第一个元素是A的倍数且第二个元素是B的倍数。 提示: 对于第二组测试数据,集合分别是:{1,1 阅读详情

相关推荐

【选举拉票】

0 ≤ bi ≤ 10^4),表示第 i 个选民选的是第 ai 号候选人,想要让他选择自己就要花 bi 的钱。你是 0 号候选人(所以,如果一个选民选你的话 ai 就是 0,这个时候 bi 也肯定是 0 )。你已经知道每一个人要选的人是谁,以及要花多少钱才能让这个人选你。现在你想要花最少的钱使得你当上县长。你当选的条件是你的票数比任何一个其它候选人的多(严格的多,不能和他们中最多的相等)。第一行有一个整数 n(1 ≤ n ≤ 10^5),表示这个县的选民数目。输出一个整数表示花费的最少的钱。

CSDN 1575

51nod动态规划教程

51nod动态规划教程 最后,我们来提供输入输出数据,由你来写一段程序,实现这个算法,只有写出了正确的程序,才能继续后面的课程。 输入 第1行:字符串A 第2行:字符串B (A,B的长度 &amp;lt;= 1000) 输出 输出最长的子序列,如果有多个,随意输出1个。 输入示例 abcicba abdkscab 输出示例 ...

旺旺的碎冰冰~的博客 725

51nod:51nod.com的代码

51点 51nod.com的代码

推荐一个算法编程学习中文社区-51NOD【算法分级,支持多语言,可在线编译】...

最近偶尔发现一个算法编程学习的论坛,刚开始有点好奇,也只是注册了一下。最近有时间好好研究了一下,的确非常赞,所以推荐给大家。功能和介绍看下面介绍吧。首页的标题很给劲,很纯粹的Coding社区。。。。虽然目前人气可能一般,但这里面题目和资源还是比较丰富的,希望给初学者一个帮助。 本文原文地址:【推荐】一个算法编程学习中文社区-51NOD【算法分级,支持多语言,可在线编译】 ...

alad64007的博客 422

沈阳计算机研究所复试,沈阳计算所复试试题附答案.doc

沈阳计算所复试试题附答案目录数据结构1200612007220085200914操作系统16200616200718200818200919组成原理20200620200720200821200921经典算法22数据结构2006数据结构部分(共25分,第1题10分,第2题15分)在(下图)所示的有向图中:v1 v2v4 ...

weixin_42511053的博客 879

Frida实战:SSL Pinning绕过技术全解析

本文详细解析了如何使用Frida工具绕过SSL Pinning技术,包括环境搭建、四种实战绕过方案及常见问题排查指南。通过具体代码示例和实战经验,帮助安全研究人员快速掌握SSL Pinning绕过技术,适用于金融类App的安全测试场景。

weixin_42530732的博客 192

51nod - 1515 明辨是非

给n组操作,每组操作形式为x y p。 当p为1时,如果第x变量和第y个变量可以相等,则输出YES,并限制他们相等;否则输出NO,并忽略此次操作。 当p为0时,如果第x变量和第y个变量可以不相等,则输出YES,并限制他们不相等 ;否则输出NO,并忽略此次操作。 输入 输入一个数n表示操作的次数(n&lt;=1*10^5) 接下来n行每行三个数x,y,p(x,y&lt;=1*10^8,p=...

brucehb的专栏 301

无感FOC之高频注入法——永磁同步电机控制

1.摘要电机运行在低速或零速时,磁链产生的反电动势很微弱,可用信号的信噪比太低,因而难以从反电动势中提取转子的位置和速度信息。因而在电机处于低速或零速时,需要借助电机的凸极特性来获取转子的位置信息。内置式永磁同步电机或磁密较大的表贴式永磁同步电机具有这种凸极特性,可借助高频电压注入法获取转子位置。2.凸极特性凸极电机内部的气隙是不均匀的,d轴和q轴的有效气隙是不同的,通俗来说在凸极电机里,q轴电感往往大于d轴电感,而在大部分表贴式永磁同步电机,气隙分布相对均匀,q轴电感近似于d轴电感。凸极率常用q轴电感和d

qq_2545470942的博客 2万+

51Nod - 1596

题目链接:51Nod - 1596 水题,肯定是能组合就组合。 AC代码: #pragma GCC optimize("-Ofast","-funroll-all-loops") #include<bits/stdc++.h> //#define int long long using namespace std; const int N=1e6+50; int cnt[N],n,...

青烟绕指柔的博客 440

037、有源钳位反激变换器(ACF)原理

文章摘要 有源钳位反激变换器(ACF)是一种克服常规反激电路漏感能量处理瓶颈的高效解决方案。文章从实际炸机案例切入,指出RCD钳位电路在高压、宽范围、高功率场景下的局限性。ACF通过增加钳位管和钳位电容,将漏感能量回收而非消耗,实现效率提升2-3%、精准电压钳位和更好EMI性能。文章详细解析了ACF四阶段工作过程、关键波形特征,并指出设计陷阱:漏感需保持1%-3%特定范围、钳位电容ESR要求严格、需专用控制IC支持互补驱动。最后给出实用调试建议,强调ACF适用于65W+功率场景,低压验证后再逐步升压的调试方

Jack010169的博客 284

基于AnimeGAN模型生成宫崎骏风格动漫照片

用于动画风格纹理和线条转换的灰度样式丢失;新颖的颜色重建损失,以保留内容图像的颜色;新的灰度对抗损失防止生成的图像从显示为灰度图像;使用深度可分卷积和反向残块的轻量级生成器,以实现更快的传输。实验表明,AnimeGAN可以对真实世界的照片进行变换场景动画风格的图像与高质量的动画视觉效果和显著优于最先进的风格化方法。# 生成宫崎骏风格图片 model = hub . Module('animegan_v1_hayao_60' , use_gpu = True)images:输入的图片数据。

CSDN 精品推荐 3635

51Nod 2059 上台阶 easy 斐波那契数列

斐波那契数列

Curren的博客 838

【MATLAB代码】雷达观测目标跟踪模型|H-infinity Filter(H无穷大滤波)与EKF(扩展卡尔曼滤波)的对比。附完整代码,订阅专栏后可直接查看

对比了经典扩展卡尔曼滤波(EKF)与H∞优化的EKF(H∞-EKF)在目标跟踪中的性能。通过MATLAB仿真实验,结果显示:在存在模型误差和测量噪声的情况下,H∞-EKF相比经典EKF在位置误差上降低

985工科博士毕业,专攻定位、导航和滤波等算法研究。从业10年,主要使用MATLAB 250

51NOD - 1677treecnt

题目链接:51NOD - 1677treecnt 显然可以考虑每条边的贡献。 假设这条边左边s个点,右边n-s个点。只有左右都有点,那么这条边就会贡献答案。然后容斥计算就是总方案减去全在左边或者全在右边。 AC代码: #pragma GCC optimize("-Ofast","-funroll-all-loops") #include<bits/stdc++.h> #define...

青烟绕指柔的博客 350

【Docker安装】OpenEuler系统下部署Docker环境

【Docker安装】OpenEuler系统下部署Docker环境

jks212454的博客 4199

自适应控制 (Adaptive Control) 算法详解及案例分析

自适应控制是一种能够在线调整控制器参数以适应系统动态变化的控制方法。其核心思想是通过实时估计系统参数或调整控制器参数,使系统在不确定性或动态变化的环境中仍能保持稳定性和性能。

闲人编程的博客 4090

51nod 2485 + 51nod 2486

51nod 2485 #include<bits/stdc++.h> using namespace std; int main() { char s[505]; scanf("%s",s); int a[30]; memset(a,0,sizeof(a)); int len=strlen(s); for(int i=0;i<l...

老铁,干了这碗algorithms的博客 721

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

51NOD - 1689逛街

题目链接:51NOD - 1689逛街 首先枚举最后一个选的位置。(答案一定在其中) 因为我们要至少选k个1,所以肯定在前面选k个最小的,其他的再单独选。 所以我们可以用堆来维护选的k个。然后其余的直接放到权值树当中,在权值树中二分即可。 注意特判k==0的情况。 AC代码: #pragma GCC optimize("-Ofast","-funroll-all-loops") #includ...

青烟绕指柔的博客 424

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

51NOD - 1406与查询

题目链接:51NOD - 1406 子集dp或者说高维前缀和。比较水。 不过卡IO很恶心。 AC代码: #pragma GCC optimize("-Ofast","-funroll-all-loops") #include<bits/stdc++.h> //#define int long long using namespace std; const int N=4e6+10;...

青烟绕指柔的博客 422

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

51nod1009 51nod1042(数位dp)

51nod1009数字1的数量 题目意思就是求1到n出现过几个1(例如n为12时,包含了5个1。1,10,12共包含3个1,11包含2个1,总共5个1) 题目思路就是数位dp,其实可以找规律,但本人太懒了,太麻烦了不想找。数位dp首先要知道dp记录的是什么,这里dp[i][j]指0到第i位数值为j的1的数量(比如dp[3][2](没有上限的情况下)则记录的是0到299有多少...

agyohust791263的博客 152

51Nod1353 树

51Nod1353 树 传送门 思路 我们定义\(dp[i][j]\)代表第i个点联通块大小为j的方案总数,也可以把它理解为等待分配(不确定归属)的联通块大小为j的方案总数。 那么每次转移我们就使用一个类似背包的东西来统计答案。 对于一个节点的每个儿子,我们只需要从大到小遍历所有可用的j_now(j_now上限就是所有遍历过的点子树大小的和)。然后再枚举这个儿子的j_son,那么显然我们...

weixin_30847939的博客 177

51NOD - 1427迈克步

题目链接:51NOD - 1427迈克步 把数字从大到小排序。然后依次放,用并查集维护连通性,每次更新sz的答案。 AC代码: #pragma GCC optimize("-Ofast","-funroll-all-loops") #include<bits/stdc++.h> //#define int long long using namespace std; const i...

青烟绕指柔的博客 341

51NOD - 2553双重祖先

题目链接:51NOD - 2553双重祖先 先对第一颗树处理,求出每个点的dfs序。 然后对于第二课树,我们就只需要在子树中找当前点在第一颗树的dfs的区间中的点的个数。 子树中直接线段树合并即可。 AC代码: #pragma GCC optimize("-Ofast","-funroll-all-loops") #include<bits/stdc++.h> //#define ...

青烟绕指柔的博客 569

51NOD - 1461稳定桌

题目链接:51NOD - 1461稳定桌 枚举每一个作为最长的价值。 所以我们从大往小枚举,然后我们就可以算出比当前值大的和,直接变量记录即可。 然后再权值树种找最小的k个值。 所有答案取min即可。 AC代码: #pragma GCC optimize("-Ofast","-funroll-all-loops") #include<bits/stdc++.h> #define i...

青烟绕指柔的博客 470

51nod 1925 + 51nod 1095

51nod 1095 注意排序的思想 #include<bits/stdc++.h> using namespace std; int main() { int n; scanf("%d",&n); map<string,int>str,solve; str.clear(); solve.clear(); getchar(); for(int i...

老铁,干了这碗algorithms的博客 491
上一篇: 各种读入方式速度比较
下一篇: Spring data mongodb @CreatedBy@LastModifiedBy@CreatedBy@LastModifiedBy SpringSecurityAuditorAware,只记...
weixin_30292843
博客等级 码龄11年 190粉丝 1365原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值