强行一波题解骗一个访问量好了...

http://blog.csdn.net/yanqval/article/details/51457302

http://absi2011.is-programmer.com/posts/200822.html

http://absi2011.is-programmer.com/posts/200920.html

orz js省队神犇↑

Day1

最佳团队(team)

给一个n+1个点的树,每个点有价值pi和费用si,求一个包含根的大小为m+1的联通块,使价值和/费用和最大。

为什么是n+1和m+1呢?根节点是0,节点从0开始编号。

1<=m<=n<=2500,0<si,pi<=10000。

首先这是一个01分数规划,我们二分答案x之后就变成了每个点权为p-s*x,判断是否存在一个包含根的正权联通块。

其实就是问联通块的最大权值。这个有两种做法。

第一种是暴力树形背包dp,每一次只循环到子树大小,由于这里物品重量都相当于是1,可以证明复杂度是O(n^2)的。

第二种之前写过,一类有依赖的属性背包dp方法

事实上第二种还更好写~

  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. using namespace std;
  5. #define SZ 2600
  6. int m,n,s[SZ],p[SZ],fa[SZ],fc[SZ],nc[SZ],siz[SZ],qs[SZ],qn=0;
  7. void dfs(int p)
  8. {
  9. qs[++qn]=p; siz[p]=1;
  10. for(int c=fc[p];c;c=nc[c]) dfs(c), siz[p]+=siz[c];
  11. }
  12. double f[SZ][SZ];
  13. bool ok(double x)
  14. {
  15. for(int j=1;j<=m;j++) f[n+2][j]=-20000000000000LL;
  16. for(int i=n+1;i>=1;i--)
  17. {
  18. f[i][0]=0;
  19. for(int j=1;j<=m;j++) f[i][j]=max(f[i+1][j-1]+p[qs[i]]-s[qs[i]]*x,f[i+siz[qs[i]]][j]);
  20. }
  21. return f[1][m]>1e-7;
  22. }
  23. #define FO(x) {freopen(#x".in","r",stdin);freopen(#x".out","w",stdout);}
  24. int main()
  25. {
  26. FO(team)
  27. scanf("%d%d",&m,&n); ++m;
  28. for(int i=1;i<=n;i++)
  29. {
  30. scanf("%d%d%d",s+i,p+i,fa+i);
  31. nc[i]=fc[fa[i]]; fc[fa[i]]=i;
  32. }
  33. dfs(0);
  34. double l=0,r=10007;
  35. double jd=(1e-5)*3;
  36. while(r-l>jd)
  37. {
  38. double m=(l+r)/2;
  39. if(ok(m)) l=m; else r=m;
  40. }
  41. printf("%.3lf",l);
  42. }

独特的树叶(leaf)

给定一个大小为n的树A和大小为n+1的树B,已知B是由A加上一个叶子并打乱节点编号构成的,求这个叶子。如果有多个输出编号最小的。

1<=n<=10^5

教你hash正确姿势系列

我们首先可以想到,假设我们有一种非常靠谱的hash姿势可以在比较靠谱的时间内求出一棵树以每个点为根的hash值。

这样我们只要枚举每一个叶子,把它的父向边以一种比较靠谱的方式删掉,在map里查一下hash值就行了。

那至于这个“比较靠谱的hash姿势”,其实还比较难想...

吴大爷告诉了我一个比较靠谱的hash方法,我们设dp(i,j)为(i,j)这条边的hash值(别问我为什么要以边为单位定义hash值)

我们让$dp(i,j)={(\sum_{p\not=i}{dp(j,p)}+233)}^{666623333}$模上一个大质数。可以发现这个做法是十分靠谱的。(而且666623333是一个质数)

至于以每个点x为根,只要再统计一下$(\sum_{p}{dp(x,p)}+233)^{666623333}$就行了。

那至于删掉父向边...我们发现我们并不需要真的删掉它,只需要计算这条边的hash值就行了。

//代码等测试过了再补

扭动的回文串(palindrome)

给两个长度为n的字符串A和B,求由A的[i,j]拼接上B的[j,k]组成的最长回文串。

需要注意的是,也可以只由A或B中的一截组成回文串。

首先我们可以用hash或manacher求出A或B的最长回文串,以及以每个位置为中心的极大回文串(就是不能再扩展的那种)。

假设我们现在有一坨A或B中的极大回文串,我们可以发现一个事情,那就是如果经过了这样一个“扭动”的过程,一定是A的一段+A的一个极大回文串+B的一段 或者 A的一段+B的一个极大回文串+B的一段 这种形式。证明就算了。

然后我们hash乱搞一波就行了。

  1. //强行卡常系列
  2. #include <iostream>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <time.h>
  7. #include <memory.h>
  8. using namespace std;
  9. #define SZ 233333
  10. #define UP 200001
  11. typedef long long ll;
  12. int jz=31,cm[SZ];
  13. struct Hasher
  14. {
  15. char s[SZ+1]; int n;
  16. int hash[SZ+1],fhash[SZ+1];
  17. char* str() {return s+1;}
  18. void pre1()
  19. {
  20. n=strlen(s+1);
  21. for(int i=n;i>=1;i--) hash[i]=hash[i+1]+cm[n-i]*(s[i]-'A'+2);
  22. for(int i=n;i>=1;i--) fhash[i]=fhash[i+1]+cm[n-i]*(s[n+1-i]-'A'+2);
  23. }
  24. int ghash(int l,int r)
  25. {
  26. return (hash[l]-hash[r+1])*cm[r];
  27. }
  28. int gfhash(int l,int r)
  29. {
  30. l=n-l+1; r=n-r+1; l^=r; r^=l; l^=r;
  31. return (fhash[l]-fhash[r+1])*cm[r];
  32. }
  33. int hl[SZ],hr[SZ],hs;
  34. Hasher() {hs=0;}
  35. void pre2()
  36. {
  37. for(int i=1;i<=n;i++)
  38. {
  39. int l=0,r=min(n-i,i-1);
  40. while(l<r)
  41. {
  42. int m=l+r+1>>1;
  43. if(gfhash(i-m,i)!=ghash(i,i+m)) r=m-1; else l=m;
  44. }
  45. ++hs; hl[hs]=i-l; hr[hs]=i+r;
  46. }
  47. for(int i=1;i<n;i++)
  48. {
  49. if(s[i]!=s[i+1]) continue;
  50. int l=0,r=min(n-i+1,i-1);
  51. while(l<r)
  52. {
  53. int m=l+r+1>>1;
  54. if(gfhash(i-m,i)!=ghash(i+1,i+1+m)) r=m-1; else l=m;
  55. }
  56. ++hs; hl[hs]=i-l; hr[hs]=i+1+r;
  57. }
  58. }
  59. void pre12()
  60. {
  61. pre1(); pre2();
  62. }
  63. }ha,hb;
  64. #define FO(x) {freopen(#x".in","r",stdin);freopen(#x".out","w",stdout);}
  65. int main()
  66. {
  67. FO(palindrome)
  68. int n;
  69. cm[0]=1;
  70. for(int i=1;i<=UP;i++) cm[i]=cm[i-1]*jz;
  71. scanf("%d\n",&n);
  72. gets(ha.str());
  73. gets(hb.str());
  74. ha.pre12(); hb.pre12();
  75. int ans=0;
  76. for(int i=1;i<=ha.hs;i++)
  77. {
  78. int l=ha.hl[i],r=ha.hr[i];
  79. int el=0,er=min(l-1,n-r+1);
  80. if(r-l+1+2*er<=ans) continue;
  81. while(el<er)
  82. {
  83. int em=el+er+1>>1;
  84. if(ha.gfhash(l-em,l-1)!=hb.ghash(r,r+em-1)) er=em-1; else el=em;
  85. }
  86. ans=max(ans,r-l+1+2*el);
  87. }
  88. for(int i=1;i<=hb.hs;i++)
  89. {
  90. int l=hb.hl[i],r=hb.hr[i];
  91. int el=0,er=min(l,n-r);
  92. if(r-l+1+2*er<=ans) continue;
  93. while(el<er)
  94. {
  95. int em=el+er+1>>1;
  96. if(ha.gfhash(l-em+1,l)!=hb.ghash(r+1,r+em)) er=em-1; else el=em;
  97. }
  98. ans=max(ans,r-l+1+2*el);
  99. }
  100. printf("%d\n",ans);
  101. return 0;
  102. }

Day2

病毒感染(virus)

原题目简直鬼畜...以下仅作参考,不保证正确性

有n个点排成一排,每个点有一个权值ai。初始时在1号点,在i号点时有三种操作:令ai=0,左移一步,右移一步。每次操作后要付出sigma(ai)的代价。如果经过一个点,没有令这个点的ai=0,并且之后某一步选择往左走,那么必须令这个点的ai=0后才能远离这个点。求令所有ai=0的最小代价。

1<=n<=3000。

因为我还不会做所以还是不要瞎bb比较好...不过听说是个沙茶dp?

反质数序列(prime)

给一个长度为n的序列,要求找出一个长度最长的满足任意两个数加起来均不为质数的最长子序列,输出长度。

1<=n<=3000,1<=ai<=10^5。

可以发现,如果我们把“不为质数”的关系列出来,似乎是叫我们找最大团。

虽然最大团是NP的(虽然可以用一些分支定界搜索之类的奇技淫巧卡过去)但是我们可以发现最大团=补图的最大独立集。

补图?任意两个数加起来均为质数?似乎必然是一奇一偶?

哦好像还有1+1=2,但是我们如果开头把所有的1去成只剩一个1,那么补图必然边都是一奇连一偶。

那么这就是一个二分图拉,最大独立集=n-最大匹配。

  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <memory.h>
  5. using namespace std;
  6. int n,a[233333];
  7. bool ip[233333];
  8. namespace Matcher
  9. {
  10. #define SZ 666666
  11. #define ES 36000005
  12. int M=1;typedef long long ll;
  13. int fst[SZ],nxt[ES],vb[ES],n1,n2;
  14. bool cap[ES];
  15. void _ad_dl(int a,int b,int c) {++M;nxt[M]=fst[a];fst[a]=M;vb[M]=b;cap[M]=c;}
  16. void ad_dl(int a,int b,int c) {_ad_dl(a,b,c); _ad_dl(b,a,0); /*cout<<a<<"->"<<b<<";\n";*/}
  17. int S,T,q[SZ],d[SZ];
  18. bool bfs()
  19. {
  20. memset(d,-1,sizeof(d));
  21. d[S]=0; q[1]=S; int h=1,t=2;
  22. while(h!=t)
  23. {
  24. int cur=q[h++];
  25. for(int e=fst[cur];e;e=nxt[e])
  26. {
  27. int b=vb[e];
  28. if(d[b]==-1&&cap[e]) d[b]=d[cur]+1, q[t++]=b;
  29. }
  30. }
  31. return d[T]!=-1;
  32. }
  33. int dfs(int x,int f)
  34. {
  35. if(f<=0) return 0;
  36. if(x==T) return f;
  37. int ca=0;
  38. for(int e=fst[x];e;e=nxt[e])
  39. {
  40. int b=vb[e];
  41. if(d[b]!=d[x]+1) continue;
  42. int w=dfs(b,min((int)cap[e],f-ca));
  43. cap[e]-=w; cap[e^1]+=w; ca+=w;
  44. if(ca==f) break;
  45. }
  46. if(!ca) d[x]=-1;
  47. return ca;
  48. }
  49. #define inf 1000000000
  50. int dinic()
  51. {
  52. int ans=0;
  53. while(bfs()) ans+=dfs(S,inf);
  54. return ans;
  55. }
  56. }
  57. #define FO(x) {freopen(#x".in","r",stdin);freopen(#x".out","w",stdout);}
  58. int main()
  59. {
  60. FO(prime)
  61. scanf("%d",&n);
  62. bool yi=0;
  63. for(int i=1;i<=n;i++)
  64. {
  65. scanf("%d",a+i);
  66. if(a[i]==1)
  67. {
  68. if(!yi) yi=1;
  69. else {--i; --n;}
  70. }
  71. }
  72. for(int i=2;i<=200000;i++)
  73. {
  74. bool np=0;
  75. for(int j=2;j*j<=i;j++)
  76. {
  77. if(i%j==0) {np=1; break;}
  78. }
  79. ip[i]=!np;
  80. }
  81. for(int i=1;i<=n;i++)
  82. {
  83. for(int j=i+1;j<=n;j++)
  84. {
  85. if(ip[a[i]+a[j]])
  86. {
  87. if(a[i]%2!=0) Matcher::ad_dl(j,i,1);
  88. else Matcher::ad_dl(i,j,1);
  89. }
  90. }
  91. }
  92. Matcher::S=n+1; Matcher::T=n+2;
  93. for(int i=1;i<=n;i++)
  94. {
  95. if(a[i]%2==0) Matcher::ad_dl(n+1,i,1);
  96. else Matcher::ad_dl(i,n+2,1);
  97. }
  98. printf("%d\n",n-Matcher::dinic());
  99. }

炸弹攻击2(attack2)

X轴上方有A个a点,X轴下方有B个b点和C个c点。求四元组(ai,bj,ck,cl)的对数,其中k<l且aibj与ckcl相交。保证不存在重点和三点共线。

1<=A,B,C<=800,0<=|x|,|y|<=10^9

 样例图(为了观察方便,有压缩,实际上A点的y坐标没这么小)

一个比较直观的想法是,对于一个b点,我们可以枚举所有的c点对,然后首先连线要在b的上方,那么计入答案的就会是极角序上的一段区间。我们把a和c的极角序排一下,前缀和啥的搞搞,就可以得到一个...O(n^3)的优秀算法了!这样就可以得到50分(虽然性价比不是很高啊,这个东西实际上并不是很好写)

那么我们发现我们对于某一个c点,假设以它作为连线的一端,然后极角序sort完之后,可以发现“连线在b的上方”是从这个c点开始逆时针的一串,所以这个玩意儿是满足二分性质的。

那么我们可以发现对于每一个c点作为一端点另外一头能到哪里了,接下来我们发现这个东西对于中间极角分成的每一段a的贡献大概是像1 2 3 4 5这样的一个等差数列。

我们只要想办法维护一个等差数列啥的就行。

注意到兹磁离线,那么比如要在[2,4]这样弄一个等差数列,那就把[2,4]这个区间+1,[5]这个位置-3,然后求一个前缀和就完事了。

注意到区间加法这种东西离线也是可以兹磁O(1)的,例如[a,b]+1,只要[a]+1,[b+1]-1,前缀和一发就行。

当然这题的等差数列加法更为蛋疼,因为它是环状的...但是认真想想还是能写的。

所以这样我们就可以得到一个非常靠谱的O(n^2logn)的做法啦。

(还好这题没有共线,不然这题写起来简直**)

  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <algorithm>
  5. #include <time.h>
  6. #include <math.h>
  7. using namespace std;
  8. typedef double ld;
  9. ld pi=acos(-1),eps=1e-6;
  10. int pp(ld x)
  11. {
  12. if(x>eps) return 1;
  13. if(x<-eps) return -1;
  14. return 0;
  15. }
  16. ld cl(ld x) //return [0,2pi)
  17. {
  18. while(pp(x)<0) x+=pi*2;
  19. while(pp(x-pi*2)>=0) x-=pi*2;
  20. return x;
  21. }
  22. typedef pair<ld,ld> pnt;
  23. #define _1 first
  24. #define _x first
  25. #define _2 second
  26. #define _y second
  27. ld ang(pnt x) {return cl(atan2(x._y,x._x));}
  28. pnt operator + (pnt a,pnt b) {return pnt(a._x+b._x,a._y+b._y);}
  29. pnt operator - (pnt a,pnt b) {return pnt(a._x-b._x,a._y-b._y);}
  30. #define SZ 666666
  31. int an,bn,cn;
  32. pnt as[SZ],bs[SZ],cs[SZ],mb;
  33. int aid[SZ],cid[SZ];
  34. ld dgci[SZ],dga[SZ],dgc[SZ];
  35. int cnt[SZ],qzh[SZ];
  36. bool cmpa(int a,int b) {return dga[a]<dga[b];}
  37. bool cmpc(int a,int b) {return dgc[a]<dgc[b];}
  38. void add(int l,int r,int x)
  39. {
  40. if(r<0||l>r) return;
  41. qzh[l]+=x; qzh[r+1]-=x;
  42. }
  43. void dj_add(int l,int r) //递减add [l,r)
  44. {
  45. if(l==r) return;
  46. if(l<=r)
  47. {
  48. add(l,r-1,1);
  49. add(l-1,l-1,l-r);
  50. }
  51. else
  52. {
  53. int tot=cn-l+r;
  54. add(0,r-1,1);
  55. add(l,cn-1,1);
  56. add(l-1,l-1,-tot);
  57. add(cn-1,cn-1,r);
  58. }
  59. }
  60. #define FO(x) {freopen(#x".in","r",stdin);freopen(#x".out","w",stdout);}
  61. int main()
  62. {
  63. FO(attack2)
  64. scanf("%d",&an);
  65. for(int i=0;i<an;i++) scanf("%lf%lf",&as[i]._x,&as[i]._y), aid[i]=i;
  66. scanf("%d",&bn);
  67. for(int i=0;i<bn;i++) scanf("%lf%lf",&bs[i]._x,&bs[i]._y);
  68. scanf("%d",&cn);
  69. for(int i=0;i<cn;i++) scanf("%lf%lf",&cs[i]._x,&cs[i]._y), cid[i]=i;
  70. long long ans=0;
  71. for(int i=0;i<bn;i++)
  72. {
  73. mb=bs[i];
  74. for(int j=0;j<an;j++) dga[j]=ang(as[j]-mb);
  75. cnt[cn]=qzh[cn]=0;
  76. for(int j=0;j<cn;j++) dgc[j]=ang(cs[j]-mb), cnt[j]=qzh[j]=0;
  77. sort(aid,aid+an,cmpa);
  78. sort(cid,cid+cn,cmpc);
  79. for(int j=0;j<cn;j++) dgci[j]=dgc[cid[j]];
  80. for(int j=0;j<an;j++)
  81. {
  82. ld x=dga[aid[j]];
  83. int bl=0;
  84. if(x<dgci[0]||x>dgci[cn-1]) bl=cn-1;
  85. else bl=upper_bound(dgci,dgci+cn,x)-dgci-1;
  86. ++cnt[bl];
  87. }
  88. for(int j=0;j<cn;j++)
  89. {
  90. ld tg=cl(dgci[j]+pi);
  91. int tp=upper_bound(dgci,dgci+cn,tg)-dgci-1;
  92. tp=(tp%cn+cn)%cn;
  93. dj_add(j,tp);
  94. }
  95. for(int j=1;j<=cn;j++) qzh[j]+=qzh[j-1];
  96. for(int j=cn-1;j>=0;j--) qzh[j]+=qzh[j+1];
  97. for(int j=0;j<cn;j++) ans+=(long long)(qzh[j])*cnt[j];
  98. }
  99. cout<<ans<<"\n";
  100. }

JSOI Round 2题解的更多相关文章

  1. Codeforces Round #556 题解

    Codeforces Round #556 题解 Div.2 A Stock Arbitraging 傻逼题 Div.2 B Tiling Challenge 傻逼题 Div.1 A Prefix S ...

  2. LibreOJ β Round #2 题解

    LibreOJ β Round #2 题解 模拟只会猜题意 题目: 给定一个长为 \(n\) 的序列,有 \(m\) 次询问,每次问所有长度大于 \(x\) 的区间的元素和的最大值. \(1 \leq ...

  3. Codeforces Round #569 题解

    Codeforces Round #569 题解 CF1179A Valeriy and Deque 有一个双端队列,每次取队首两个值,将较小值移动到队尾,较大值位置不变.多组询问求第\(m\)次操作 ...

  4. Codeforces Round #557 题解【更完了】

    Codeforces Round #557 题解 掉分快乐 CF1161A Hide and Seek Alice和Bob在玩捉♂迷♂藏,有\(n\)个格子,Bob会检查\(k\)次,第\(i\)次检 ...

  5. CFEducational Codeforces Round 66题解报告

    CFEducational Codeforces Round 66题解报告 感觉丧失了唯一一次能在CF上超过wqy的机会QAQ A 不管 B 不能直接累计乘法打\(tag\),要直接跳 C 考虑二分第 ...

  6. Google kickstart 2022 Round A题解

    Speed Typing 题意概述 给出两个字符串I和P,问能否通过删除P中若干个字符得到I?如果能的话,需要删除字符的个数是多少? 数据规模 \[1≤|I|,|P|≤10^5 \] 双指针 设置两个 ...

  7. Codeforces Beta Round #62 题解【ABCD】

    Codeforces Beta Round #62 A Irrational problem 题意 f(x) = x mod p1 mod p2 mod p3 mod p4 问你[a,b]中有多少个数 ...

  8. “玲珑杯”ACM比赛 Round #12题解&源码

    我能说我比较傻么!就只能做一道签到题,没办法,我就先写下A题的题解&源码吧,日后补上剩余题的题解&源码吧!                                     A ...

  9. “玲珑杯”ACM比赛 Round #19题解&源码【A,规律,B,二分,C,牛顿迭代法,D,平衡树,E,概率dp】

    A -- simple math problem Time Limit:2s Memory Limit:128MByte Submissions:1599Solved:270 SAMPLE INPUT ...

随机推荐

  1. App Today Extension开发注意事项

    从iOS 8起,就有了App Extension.Extension的种类至今也扩充到了19种,应用也很广泛,值得重点关注起来. Extension几乎可以看做一个内嵌的独立App,拥有独立的Bund ...

  2. Android 的系统架构

    Android 的系统架构 Android其本质就是在标准的Linux系统上增加了Java虚拟机Dalvik,并在Dalvik虚拟机上搭建了一个JAVA的application framework,所 ...

  3. 【代码笔记】iOS-看图听声音

    一,效果图. 二,工程图. 三,代码. RootViewController.h #import <UIKit/UIKit.h> #import <AVFoundation/AVFo ...

  4. db2+python+sqlchemy环境的搭建

    记录了通过sqlalchemy 管理db2数据库的环境搭建 1.db2数据库安装配置 利用winscp复制iso文件到/mnt/IBM_db2 目录下 IBM_db2为自己创建 重命名 mv IBM\ ...

  5. Github入门(一)

    之前早就听说过Git的大名,但由于合作项目时的团体都非常小,所以一直没有开始系统的学习和使用(其实就是懒!),最近终于有动力开始进行入门的学习. 首先介绍一下自学用书:https://git-scm. ...

  6. mapreduce流程中的几个关键点

    MapReduce中数据流动    (1)最简单的过程:  map - reduce    (2)定制了partitioner以将map的结果送往指定reducer的过程: map - partiti ...

  7. 模块module

    python中的Module相当于C++中头文件和命名空间的组合体,便于代码的组织,任何一个python代码的文件都是一个Module,都可以被其他模块import import,from...imp ...

  8. centos7系统下安装nodejs开发环境

    1)安装基础工具(if not exists) yum install -y net telnet tools vim wget ntp 2)同步系统时间(if necessary) ntpdate ...

  9. C# 中的占位符本质

    占位符本质 1.占位符是相对于String字符串类型而言的. 2.占位符其实就是调用String.Format()方法.把指定的变量拼接到定义好的字符串模板中组成新的字符串.

  10. mapred-site.xml 配置在线更新

    环境:ibm jdk , cdh2.35.0.2 需求:更新mapred-site.xml 中的mapreduce.map.java.opts 和 mapreduce.reduce.java.opts ...