寒假第三周 1.25 --- 1.31

1.25

什么都没干

1.26

装jekyll

---------------------------------------

恩!不管怎么样,都要好好加油>_<

1.27

好久没有早起了的说

搭了一个挫挫的博客

 

1.28

cf 588e 588E - Duff in the Army

给出一棵n 个节点的树,有m个人住在这n个城市里面的一些城市里面

给出 q 个询问,每次询问 u v a

求u 到 v 路径上,输出 前 a 大的人的编号

lca,因为a的范围是 1到 10,所以再开一个vector  f[i][j] 表示 节点 i 到 它的 2^j级祖先的路径上前 10大的编号

然后就合并--

话说看了别人的代码,再用lca 的板改也写了好久的说啊

  1 #include<cstdio>
  2 #include<cstring>
  3 #include<iostream>
  4 #include<algorithm>
  5 #include<vector>
  6 using namespace std;
  7 
  8 typedef long long LL;
  9 const int maxn = 2e5+5;
 10 const int MAX_LOG = 32;
 11 
 12 int n,m,q;
 13 int first[maxn],ecnt;
 14 int dep[maxn];
 15 vector<int> f[MAX_LOG][maxn];
 16 vector<int> c[maxn];
 17 int fa[MAX_LOG][maxn];
 18 int ql[maxn],qr[maxn],qa[maxn];
 19 vector<int> res;
 20 
 21 struct Edge{
 22     int v,next,w,u;
 23 }e[maxn<<1];
 24 
 25 void init(){
 26     memset(first,-1,sizeof(first));
 27     memset(ql,0,sizeof(ql));
 28     memset(qr,0,sizeof(qr));
 29     ecnt = 0;
 30     for(int i = 0;i < maxn;i++) c[i].clear();
 31     for(int i = 0;i < MAX_LOG;i++){
 32         for(int j = 0;j < maxn;j++){
 33                 f[i][j].clear();
 34         }
 35     }
 36 }
 37 
 38 void Add_edge(int u,int v){
 39     e[++ecnt].next = first[u];
 40     e[ecnt].v = v;
 41     e[ecnt].u = u;
 42     first[u] = ecnt;
 43 }
 44 
 45 void Merge(vector<int>& v1,vector<int>& v2){
 46     for(int i = 0;i < v2.size();i++) v1.push_back(v2[i]);
 47     sort(v1.begin(),v1.end());
 48     int len = unique(v1.begin(),v1.end())-v1.begin();
 49     v1.resize(min(len,10));
 50 }
 51 
 52 void Dfs(int p,int pre,int d){
 53     fa[0][p] = pre;
 54     dep[p] = d;
 55     Merge(f[0][p],c[p]);
 56     Merge(f[0][p],c[pre]);
 57     for(int i = first[p];~i;i=e[i].next){
 58         int v = e[i].v;
 59         if(v == pre) continue;
 60         Dfs(v,p,d+1);
 61     }
 62 }
 63 
 64 void Pre(){
 65     Dfs(1,1,0);
 66     for(int k =0;k+1 < MAX_LOG;++k){
 67         for(int v=1;v<=n;++v){
 68                 fa[k+1][v] = fa[k][fa[k][v]];
 69                 f[k+1][v] = f[k][v];
 70                 Merge(f[k+1][v],f[k][fa[k][v]]);
 71         }
 72     }
 73 }
 74 
 75 int Lca(int u,int v){
 76     if(dep[u] > dep[v]) swap(u,v);
 77     for(int k= MAX_LOG-1;k>=0;--k){
 78         if((dep[v]-dep[u])&(1<<k)){
 79             Merge(res,f[k][v]);
 80             v=fa[k][v];
 81         }
 82     }
 83     if(u == v){
 84         Merge(res,c[u]);
 85         return u;
 86     }
 87     for(int k=MAX_LOG-1;k>=0;--k){
 88         if(fa[k][u] != fa[k][v]){
 89                 Merge(res,f[k][u]);
 90                 Merge(res,f[k][v]);
 91                 u=fa[k][u];
 92                 v=fa[k][v];
 93         }
 94     }
 95     Merge(res,f[0][u]);
 96     Merge(res,f[0][v]);
 97     return fa[0][u];
 98 }
 99 
100 void solve(){
101     Pre();
102    /* for(int i = 1;i <= n;i++){
103         for(int k = 0;k < 5;k++){
104             printf("i = %d  k = %d  ",i,k);
105             for(int j = 0;j < f[k][i].size();j++){
106                 printf("%d ",f[k][i][j]);
107             }
108             printf("\n");
109         }
110     }*/
111 
112     for(int i = 1;i <= q;i++){
113         res.clear();
114         int lca = Lca(ql[i],qr[i]);
115       //  printf("i = %d lca = %d\n",i,lca);
116         int len = res.size();
117         printf("%d ",min(qa[i],len));
118         for(int j = 0;j < min(qa[i],len);j++){
119             printf("%d ",res[j]);
120         }
121         printf("\n");
122     }
123 }
124 
125 int main(){
126     while(scanf("%d %d %d",&n,&m,&q) != EOF){
127         init();
128         int u,v;
129         for(int i = 1;i <= n-1;i++){
130             scanf("%d %d",&u,&v);
131             Add_edge(u,v);
132             Add_edge(v,u);
133         }
134         int x;
135         for(int i = 1;i <= m;i++){
136             scanf("%d",&x);
137             c[x].push_back(i);
138         }
139         for(int i = 1;i <= n;i++){
140             sort(c[i].begin(),c[i].end());
141             if(c[i].size()>10) c[i].resize(10);
142         }
143 
144         for(int i = 1;i <= q;i++){
145             scanf("%d %d %d",&ql[i],&qr[i],&qa[i]);
146         }
147       /*  for(int i = 1;i <= n;i++){
148             printf("i = %d  ",i);
149             for(int j = 0;j < c[i].size();j++){
150                 printf("%d ",c[i][j]);
151             }
152             printf("\n");
153         }*/
154 
155         solve();
156     }
157     return 0;
158 }
View Code

 

cf 591c 591C - Median Smoothing

又重新瞅了下这题,先把要变的处理出来,然后就是尺取了

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<algorithm>
 5 #include<vector>
 6 using namespace std;
 7 
 8 const int maxn = 5e5+5;
 9 int n,a[maxn],b[maxn];
10 
11 void solve(){
12     memset(b,0,sizeof(b));
13     b[1]=b[n] = 0;
14     for(int i = 2;i <= n-1;i++){
15         if(a[i]==0 && a[i-1]==1 && a[i+1]== 1) b[i]=1;
16         if(a[i]==1 && a[i-1]==0 && a[i+1]== 0) b[i]=1;
17     }
18     int res = 0;
19     for(int i = 2;i <= n-1;){
20         int j = i;
21         if(b[i] == 0){
22             i++;
23             continue;
24         }
25         while(j <= n-1 && b[j] == 1) j++;
26         int c = (j-i+1)/2;
27         res = max(res,c);
28         int l = i,r = j;
29        // printf("i = %d  j = %d\n",i,j);
30         while(l <= r){
31             a[l] = a[i-1];
32             a[r] = a[j];
33             l++;r--;
34         }
35         i = j;
36     }
37     printf("%d\n",res);
38     for(int i = 1;i <= n;i++){
39         printf("%d ",a[i]);
40     }
41     printf("\n");
42 }
43 
44 int main(){
45     while(scanf("%d",&n) != EOF){
46         for(int i = 1;i <= n;i++){
47             scanf("%d",&a[i]);
48         }
49         solve();
50     }
51     return 0;
52 }
View Code

 

cf 592c The Big Race

浮点误差,一直 wa 58

http://www.cnblogs.com/oyking/p/3959905.html 这篇讲了好多浮点误差,先放这儿下

1 http://www.cnblogs.com/oyking/p/3959905.html
View Code

 

1.29

cf 617c C - Watering Flowers

看题解说的nlogn 的做法,于是我 sb 地用线段树去查询的最大值了

  1 #include<cstdio>
  2 #include<cstring>
  3 #include<iostream>
  4 #include<algorithm>
  5 using namespace std;
  6 
  7 typedef long long LL;
  8 const int maxn = 5e5+5;
  9 int n;
 10 LL x1,x2,y1,y2;
 11 int ql,qr;
 12 LL nmax;
 13 
 14 struct node{
 15     LL r,R;
 16 }p[maxn];
 17 
 18 struct Node{
 19     int l,r;
 20     LL maxx;
 21 }t[maxn*4];
 22 
 23 int cmp1(node n1,node n2){
 24     return n1.r < n2.r;
 25 }
 26 
 27 int cmp2(node n1,node n2){
 28     return n1.R < n2.R;
 29 }
 30 
 31 void Pushup(int p){
 32     t[p].maxx = max(t[p<<1].maxx,t[p<<1|1].maxx);
 33 }
 34 
 35 void Build_tree(int o,int l,int r,int key){
 36     t[o].l = l;
 37     t[o].r = r;
 38     if(l == r){
 39         if(key == 0)t[o].maxx = p[l].R;
 40         else t[o].maxx = p[l].r;
 41         return;
 42     }
 43     int mid = (l+r)/2;
 44     Build_tree(o<<1,l,mid,key);
 45     Build_tree(o<<1|1,mid+1,r,key);
 46     Pushup(o);
 47 }
 48 
 49 void query(int p,int l,int r){
 50     if(t[p].maxx < nmax){
 51         return;
 52     }
 53     if(ql <= t[p].l && t[p].r <= qr){
 54         nmax = max(nmax,t[p].maxx);
 55         return;
 56     }
 57     int mid = (t[p].l+t[p].r)/2;
 58     if(ql > mid) query(p<<1|1,mid+1,r);
 59     else if(qr <= mid) query(p<<1,l,mid);
 60     else{
 61         query(p<<1,l,mid);
 62         query(p<<1|1,mid+1,r);
 63     }
 64 }
 65 
 66 void solve(){
 67     LL ans = 1e18+7;
 68     sort(p+1,p+n+1,cmp1);
 69 
 70    /* for(int i = 1;i <= n;i++){
 71         printf("p[%d].r = %I64d R = %I64d\n",i,p[i].r,p[i].R);
 72     }*/
 73 
 74     Build_tree(1,1,n,0);
 75     for(int i = 0;i <= n-1;i++){
 76         nmax = 0;
 77         ql = i+1;qr = n;
 78         query(1,i+1,n);
 79         ans = min(ans,nmax+p[i].r);
 80        // printf("p[%d].r = %I64d  nmax = %I64d\n",i,p[i].r,nmax);
 81     }
 82     sort(p+1,p+n+1,cmp2);
 83     Build_tree(1,1,n,1);
 84     for(int i = 0;i <= n-1;i++){
 85         nmax = 0;
 86         ql = i+1;qr = n;
 87         query(1,i+1,n);
 88         ans = min(ans,nmax+p[i].R);
 89     }
 90     printf("%I64d\n",ans);
 91 }
 92 
 93 int main(){
 94     while(scanf("%d %I64d %I64d %I64d %I64d",&n,&x1,&y1,&x2,&y2)!=EOF){
 95         LL x,y;
 96         for(int i = 1;i <= n;i++){
 97             scanf("%I64d %I64d",&x,&y);
 98             p[i].r = (x-x1)*(x-x1)+(y-y1)*(y-y1);
 99             p[i].R = (x-x2)*(x-x2)+(y-y2)*(y-y2);
100         }
101         solve();
102     }
103     return 0;
104 }
View Code

 

我终于知道比赛的时候为什么错了---呜呜呜呜

看别人的代码,他们都只扫了一边就可以了,但是我要扫两边才能过,,是因为自己写的时候只扫了一边,但初始化只取一边的时候,我取了两边的max

所以如果改的话,改成扫两边就能过了

-------

我又sb的用线段树了,,像那天的每日一样,维护一个MAX数组就好----诶---

 

1.30

什么都没干

1.31

补个bc,全都被hack了,不过后来rejudge第一题过了,想知道用的什么数据hack的

然后第二题,想都没想就打dfs了,虽说是错的,可是就是这种错的dfs都打了好久阿,好弱啊

然后dp的做法,看题解说倒着背包一遍不是很理解,然后偷瞄一眼别人的代码

和那天 cf 上用了下鸽巢原理的那题dp是一样的吧,好弱啊

hdu 5616 Jam's balance

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<algorithm>
 5 using namespace std;
 6 
 7 const int maxn = 5e5+5;
 8 int n,a[maxn],dp[2][maxn];
 9 int m,q[maxn];
10 
11 void solve(){
12     memset(dp,0,sizeof(dp));
13     dp[0][0] = 1;
14     int key = 0;
15     for(int i = 1;i <= n;i++){
16         dp[key][a[i]] = 1;
17 
18         for(int j = 0;j <= 2000;j++){
19             if(dp[1-key][j]){
20                 dp[key][j+a[i]] = 1;
21                 if(j-a[i] >= 0){
22                     dp[key][j-a[i]] = 1;
23                 }
24             }
25         }
26         for(int j = 0;j <= 2000;j++) dp[1-key][j] = dp[key][j];
27         key = !key;
28     }
29     for(int i = 1;i <= m;i++){
30         if(dp[key][q[i]]) puts("YES");
31         else puts("NO");
32     }
33 }
34 
35 int main(){
36     int T;
37     scanf("%d",&T);
38     while(T--){
39         scanf("%d",&n);
40         for(int i = 1;i <= n;i++) scanf("%d",&a[i]);
41         scanf("%d",&m);
42         for(int i = 1;i <= m;i++) scanf("%d",&q[i]);
43         solve();
44     }
45     return 0;
46 }
View Code

 

hdu 5617 Jam's maze

不会做

看了题解还是写不出代码--

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<algorithm>
 5 using namespace std;
 6 
 7 const int mod = 5201314;
 8 const int maxn = 505;
 9 char g[maxn][maxn];
10 int n,dp[2][maxn][maxn];
11 
12 void solve(){
13     memset(dp,0,sizeof(dp));
14     int key = 0;
15     if(g[1][1] == g[n][n]){
16         dp[0][1][n] = 1;
17     }
18 
19     int y1,y2;
20     for(int i = 1;i <= n-1;i++){
21         memset(dp[!key],0,sizeof(dp[!key]));
22         for(int x1 = 1;x1 <= i+1;x1++){
23             for(int x2 = n;x2 >= n-i;x2--){
24                 y1 = i+2-x1;
25                 y2 = 2*n-i-x2;
26                 if(g[x1][y1] == g[x2][y2]){
27                     dp[1-key][x1][x2] = (dp[1-key][x1][x2]+dp[key][x1][x2])%mod;
28                     dp[1-key][x1][x2] = (dp[1-key][x1][x2]+dp[key][x1][x2+1])%mod;
29                     dp[1-key][x1][x2] = (dp[1-key][x1][x2]+dp[key][x1-1][x2+1])%mod;
30                     dp[1-key][x1][x2] = (dp[1-key][x1][x2]+dp[key][x1-1][x2])%mod;
31                    // printf("dp[%d][%d][%d] = %d\n",1-key,x1,x2,dp[1-key][x1][x2]);
32 
33                 }
34             }
35         }
36         key = !key;
37     }
38     int ans = 0;
39     for(int i = 1;i <= n;i++){
40         ans = (ans+dp[key][i][i])%mod;
41     }
42     printf("%d\n",ans);
43 }
44 
45 int main(){
46     int T;
47     scanf("%d",&T);
48     while(T--){
49         scanf("%d",&n);
50         for(int i = 1;i <= n;i++) scanf("%s",g[i]+1);
51         solve();
52     }
53     return 0;
54 }
View Code

 

你可能感兴趣的:(寒假第三周 1.25 --- 1.31)