【B】后缀数组
【C】染色,DP(感觉可出)
【D】BFS搜索,有点麻烦
【G】概率DP+状态压缩
【I】矩阵快速幂(队友已出)
【J】树的分治
Post Robot
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
But there is such few comments of his posts that he feels depressed all the day. As his best friend and an excellent programmer, DT asked you to help make his blog look more popular. He is so warm that you have no idea how to refuse. But you are unwilling to read all of his boring posts word by word. So you decided to write a script to comment below his posts automatically.
After observation, you found words “Apple” appear everywhere in his posts. After your counting, you concluded that “Apple”, “iPhone”, “iPod”, “iPad” are the most high-frequency words in his blog. Once one of these words were read by your smart script, it will make a comment “MAI MAI MAI!”, and go on reading the post.
In order to make it more funny, you, as a fan of Sony, also want to make some comments about Sony. So you want to add a new rule to the script: make a comment “SONY DAFA IS GOOD!” when “Sony” appears.
The size of the article does not exceed 8KB.
Apple bananaiPad lemon ApplepiSony 233 Tim cook is doubi from Apple iPhoneipad iPhone30 is so biiiiiiig Microsoft makes good App.
【Sample Output】
MAI MAI MAI! MAI MAI MAI! MAI MAI MAI! SONY DAFA IS GOOD! MAI MAI MAI! MAI MAI MAI! MAI MAI MAI!
【分析】
Game
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)● In the beginning of the game, there are a lot of piles of beads.
● Players take turns to play. Each turn, player choose a pile i and remove some (at least one) beads from it. Then he could do nothing or split pile i into two piles with a beads and b beads.(a,b > 0 and a + b equals to the number of beads of pile i after removing)
● If after a player's turn, there is no beads left, the player is the winner.
Suppose that the two players are all very clever and they will use optimal game strategies. Your job is to tell whether the player who plays first can win the game.
For each test case, the first line contains a postive integer n(n < 105) means there are n piles of beads. The next line contains n postive integer, the i-th postive integer ai(ai < 231) means there are ai beads in the i-th pile.
1 1 2 1 1 3 1 2 3
【Sample Output】
Win
Lose
Lose
【题意】
Dice
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)At the beginning, the two dices may face different(which means there exist some i, ai ≠ bi). Ddy wants to make the two dices look the same from all directions(which means for all i, ai = bi) only by the following four rotation operations.(Please read the picture for more information)
For each case, the first line consists of six integers a1,a2,a3,a4,a5,a6, representing the numbers on dice A.
The second line consists of six integers b1,b2,b3,b4,b5,b6, representing the numbers on dice B.
1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 5 6 4 3 1 2 3 4 5 6 1 4 2 5 3 6
【Sample Output】
0 3 -1
【题意】
1 /* *********************************************** 2 MYID : Chen Fan 3 LANG : G++ 4 PROG : F1006 5 ************************************************ */ 6 7 #include <iostream> 8 #include <cstdio> 9 #include <cstring> 10 #include <algorithm> 11 12 using namespace std; 13 14 typedef struct nod{ 15 int a,b,c,d,e,f; 16 } node; 17 18 node move1(node a) 19 { 20 node ret; 21 ret.a=a.d;ret.b=a.c;ret.c=a.a; 22 ret.d=a.b;ret.e=a.e;ret.f=a.f; 23 return ret; 24 } 25 26 node move2(node a) 27 { 28 node ret; 29 ret.a=a.c;ret.b=a.d;ret.c=a.b; 30 ret.d=a.a;ret.e=a.e;ret.f=a.f; 31 return ret; 32 } 33 34 node move3(node a) 35 { 36 node ret; 37 ret.a=a.f;ret.b=a.e;ret.c=a.c; 38 ret.d=a.d;ret.e=a.a;ret.f=a.b; 39 return ret; 40 } 41 42 node move4(node a) 43 { 44 node ret; 45 ret.a=a.e;ret.b=a.f;ret.c=a.c; 46 ret.d=a.d;ret.e=a.b;ret.f=a.a; 47 return ret; 48 } 49 50 node q[50000]; 51 int num[50000]; 52 int head=1,tail=1; 53 54 bool check(node a) 55 { 56 for (int i=1;i<=tail;i++) 57 if (a.a==q[i].a&&a.b==q[i].b&&a.c==q[i].c&&a.d==q[i].d&&a.e==q[i].e&&a.f==q[i].f) 58 return true; 59 return false; 60 } 61 62 int main() 63 { 64 node a,b; 65 while (scanf("%d",&a.a)!=EOF) 66 { 67 scanf("%d%d%d%d%d",&a.b,&a.c,&a.d,&a.e,&a.f); 68 scanf("%d%d%d%d%d%d",&b.a,&b.b,&b.c,&b.d,&b.e,&b.f); 69 70 if (a.a==b.a&&a.b==b.b&&a.c==b.c&&a.d==b.d&&a.e==b.e&&a.f==b.f) 71 { 72 printf("0 "); 73 } else 74 { 75 q[1]=a; 76 num[1]=0; 77 bool done=false; 78 head=1;tail=1; 79 while (head<=tail) 80 { 81 node temp=move1(q[head]); 82 if (temp.a==b.a&&temp.b==b.b&&temp.c==b.c&&temp.d==b.d&&temp.e==b.e&&temp.f==b.f) 83 { 84 printf("%d ",num[head]+1); 85 done=true; 86 break; 87 } 88 if (!check(temp)) 89 { 90 tail++; 91 q[tail]=temp; 92 num[tail]=num[head]+1; 93 } 94 95 temp=move2(q[head]); 96 if (temp.a==b.a&&temp.b==b.b&&temp.c==b.c&&temp.d==b.d&&temp.e==b.e&&temp.f==b.f) 97 { 98 printf("%d ",num[head]+1); 99 done=true; 100 break; 101 } 102 if (!check(temp)) 103 { 104 tail++; 105 q[tail]=temp; 106 num[tail]=num[head]+1; 107 } 108 109 temp=move3(q[head]); 110 if (temp.a==b.a&&temp.b==b.b&&temp.c==b.c&&temp.d==b.d&&temp.e==b.e&&temp.f==b.f) 111 { 112 printf("%d ",num[head]+1); 113 done=true; 114 break; 115 } 116 if (!check(temp)) 117 { 118 tail++; 119 q[tail]=temp; 120 num[tail]=num[head]+1; 121 } 122 123 temp=move4(q[head]); 124 if (temp.a==b.a&&temp.b==b.b&&temp.c==b.c&&temp.d==b.d&&temp.e==b.e&&temp.f==b.f) 125 { 126 printf("%d ",num[head]+1); 127 done=true; 128 break; 129 } 130 if (!check(temp)) 131 { 132 tail++; 133 q[tail]=temp; 134 num[tail]=num[head]+1; 135 } 136 137 head++; 138 } 139 if (!done) printf("-1 "); 140 } 141 } 142 143 return 0; 144 }
代码写得也比较丑
【启发】
当时初看题目的时候瞟了几眼直接就跳过了,-_-///事后感觉没什么可以做了才回来看的,然后才发现其实很简单。还是应该认真看看题,比赛的时候至少每道题都要看过去,说不定能找到一些突破点。
Number Sequence
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Special Judge
● ai ∈ [0,n] ● ai ≠ aj( i ≠ j )
For sequence a and sequence b, the integrating degree t is defined as follows(“⊕” denotes exclusive or):
Now give you a number n and the sequence a. You should calculate the maximum integrating degree t and print the sequence b.
For each case, the first line contains an integer n(1 ≤ n ≤ 105), The second line contains a0,a1,a2,...,an.
4 2 0 1 4 3
【Sample Output】
20 1 0 2 3 4
【题意】
题目给出一组A序列,t的值是等于B序列中的每一个对应的数与A序列异或之后的和。现在的要求就是求出能够使得t最大的B序列。
【分析】
首先我真的不是特别明白这道题为什么会用到Special Judge?
......按照我的想法,能使得t最大的B序列应该是唯一的,把所有数都化为二进制的形式,则为了使得Ai异或Bi的值最大,Bi的二进制数位要尽可能多地与Ai错开。
所以这样出来的数其实是一组两个的数,A序列是从0到n,B序列也是从0到n,且每个序列不出现重复的数字。对于每一个Ai,用一个等长的全1的二进制数去与之异或,就能得到对应的Bi,反过来对数Bi操作可以得到数Ai。其实就是构造出来之后使对应的Ai+Bi能等于一个全是1的二进制数。
如此就能构造出B序列了,下面是几个序列的对应关系,T这行表示异或结果
n=7 ------------------- A 0 1 2 3 4 5 6 7 B 7 6 5 4 3 2 1 0 T 7 7 7 7 7 7 7 7 ------------------- n=8 ------------------- A 0 1 2 3 4 5 6 7 8 B 0 6 5 4 3 2 1 8 7 T 0 7 7 7 7 7 7 15 15 ------------------- n=9 ------------------- A 0 1 2 3 4 5 6 7 8 9 B 1 0 5 4 3 2 9 8 7 6 T 1 1 7 7 7 7 15 15 15 15 -------------------
当然这里有个顺序的问题,构造的时候从最大(二进制最长的数)的开始向小的数进行,保证使最终总的异或值最大,并且后来构造出来Bi的数比Ai小,避免了异或构造出来的数大于n的情况(真的可能会有啊)。
1 /* *********************************************** 2 MYID : Chen Fan 3 LANG : G++ 4 PROG : H1008 5 ************************************************ */ 6 7 #include <iostream> 8 #include <cstdio> 9 #include <cstring> 10 #include <algorithm> 11 12 using namespace std; 13 14 int pp[]={0,1,3,7,15,31,63,127,255,511,1023,2047,4095,8191,16383,32767,65535,131071}; 15 int a[100010],b[100010]; 16 17 int get(int s) 18 { 19 int ret=0; 20 while (s>0) 21 { 22 ret++; 23 s/=2; 24 } 25 return ret; 26 } 27 28 int main() 29 { 30 //freopen("H1008.txt","r",stdin); 31 //freopen("H1008out.txt","w",stdout); 32 33 int n; 34 while (scanf("%d",&n)!=EOF) 35 { 36 memset(a,0,sizeof(a)); 37 for (int i=n;i>=0;i--) 38 if (!a[i]) 39 { 40 for (int j=i;j>=pp[get(i)-1]+1;j--) 41 { 42 int yh=j^pp[get(i)]; 43 a[j]=yh; 44 a[yh]=j; 45 } 46 } 47 48 long long ans=0; 49 int x; 50 for (int i=0;i<=n;i++) 51 { 52 scanf("%d",&x); 53 //x=i; 54 ans+=(long long )x^a[x]; 55 b[i]=a[x]; 56 } 57 printf("%lld ",ans); 58 printf("%d",b[0]); 59 for (int i=1;i<=n;i++) printf(" %d",b[i]); 60 printf(" "); 61 } 62 63 return 0; 64 }
【启发】
虽然当时还没想明白Special Judge是为什么(现在还是不明白...),不过以后碰到有思路的就直接上吧,说不定就对了呢。
Ellipsoid
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Special Judge
For each testcase, one line contains 6 real number a,b,c(0 < a,b,c,< 1),d,e,f(0 ≤ d,e,f < 1), as described above. It is guaranteed that the input data forms a ellipsoid. All numbers are fit in double.
1 0.04 0.01 0 0 0
【Sample Output】
1.0000000
【题意】
题目给出一个椭圆面的空间方程,要求计算出原点与椭圆面之间的最短距离。
【分析】
1 /* *********************************************** 2 MYID : Chen Fan 3 LANG : G++ 4 PROG : HDU5017-Easy Simulated Annealing 5 ************************************************ */ 6 7 #include <iostream> 8 #include <cstdio> 9 #include <cstring> 10 #include <algorithm> 11 #include <cmath> 12 13 using namespace std; 14 15 double a,b,c,d,e,f; 16 double r=0.99; 17 double eps=1e-8; 18 int dx[]={-1,-1,-1,0,0,1,1,1}; 19 int dy[]={-1,0,1,-1,1,-1,0,1}; 20 21 double cal(double x,double y) 22 { 23 double A=c,B=e*x+d*y,C=a*x*x+b*y*y+f*x*y-1; 24 double delta=B*B-4*A*C; 25 if (delta<0) return 1e60; 26 double z1=(-B+sqrt(delta))/2/A,z2=(-B-sqrt(delta))/2/A; 27 if (z1*z1<z2*z2) return z1; 28 else return z2; 29 } 30 31 double dis(double x,double y,double z) 32 { 33 return sqrt(x*x+y*y+z*z); 34 } 35 36 double doit() 37 { 38 double step=1; 39 double x=0,y=0,z; 40 while (step>eps) 41 { 42 z=cal(x,y); 43 for (int i=0;i<8;i++) 44 { 45 double xx=x+dx[i]*step,yy=y+dy[i]*step; 46 double zz=cal(xx,yy); 47 if (zz>1e30) continue; 48 if (dis(xx,yy,zz)<dis(x,y,z)) 49 { 50 x=xx; 51 y=yy; 52 z=zz; 53 } 54 } 55 step*=r; 56 } 57 return dis(x,y,z); 58 } 59 60 int main() 61 { 62 while (scanf("%lf%lf%lf%lf%lf%lf",&a,&b,&c,&d,&e,&f)!=EOF) 63 printf("%.8f ",doit()); 64 return 0; 65 }
个人认为这种搜索策略与真正的模拟退火算法还是有一点区别的,首先这里面根本没有涉及到向非最优解方向移动的情况。
我的理解是,这里模仿了模拟退火算法对温度控制的特性。由于是求解原点与椭球面的距离,这个结果必然是一个凸函数,则不断修正前进的方向并逐步缩小搜索范围,最终到达最优解。
首先指定一个步长与步长衰减速度(类退火速度),每次搜索完成一层,步长衰减一次,直到衰减程度在精度控制范围内。
三分:
二分用来求解线性函数的最优值,三分则是用来求解凸函数的最优值。
对于本题来说,基本想法是以x,y,z中的其中两个变量进行两次三分,而后计算出第三个变量,并以最终的距离作为判断函数。由于这里都是实数,两层三分需要迭代的次数比较难以控制,参数稍微修正得不对,精度与时间上就会出现比较大的问题,这两个要求的参数是对立的,精度高了耗时就长,时间控制了,可能最终结果又错了。
最后提交的是在超时边缘......中间出现了许多重复运算,这种还是不推荐使用了。