• The 2014 ACMICPC Asia Regional Xian Online


    【A】签到题

    【B】后缀数组

    【C】染色,DP(感觉可出)

    【D】BFS搜索,有点麻烦

    【E】博弈论,Nim博弈

    【F】BFS状态搜索

    【G】概率DP+状态压缩

    【H】异或+构造

    【I】矩阵快速幂(队友已出)

    【J】树的分治

    【K】类模拟退火的方向修正搜索、三分

    【A】

    Post Robot

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

    【Problem Description】
       DT is a big fan of digital products. He writes posts about technological products almost everyday in his blog.
       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.
     
    【Input】
       A blog article described above, which contains only printable characters(whose ASCII code is between 32 and 127), CR(ASCII code 13, ‘ ’ in C/C++), LF(ASCII code 10, ‘ ’ in C/C++), please process input until EOF. Note all characters are case sensitive.
       The size of the article does not exceed 8KB.
     
    【Output】
       Output should contains comments generated by your script, one per line.
     
    【Sample Input】
    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!

     【分析】

    买买买!!!
    直接扫描判断即可。
     
     
    【E】

    Game

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
    【Problem Description】
       Here is a game for two players. The rule of the game is described below:
       ● 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.
     
    【Input】
       There are multiple test cases. Please process till EOF.
       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.
     
    【Output】
       For each test case, if the first player can win the game, ouput "Win" and if he can't, ouput "Lose"
     
    【Sample Input】
    1
    1
    2
    1 1
    3
    1 2 3

    【Sample Output】

    Win
    Lose
    Lose

    【题意】

    取石子的游戏:初始时有n堆,每人轮流进行取的过程,每次可以选择某一堆取任意个,然后选择把剩下的部分分成任意数量的两堆。最后一个取完所有东西的人获胜。
     
    【分析】
    妈蛋!!!比赛前我根本没听说过Nim博弈好么!!!果然还是题目做的太少T_T,东西了解得太少T_T////////////////////////
    看着别人刷刷刷都过了,死活看不明白是怎么回事......哭晕在厕所啊
     
    以下是两个博弈论知识的总结帖:
     
    【启发】
    ......还是多做题吧,没见过只能怪自己没见过。
     
     
    【F】

    Dice

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
    Problem Description】
       There are 2 special dices on the table. On each face of the dice, a distinct number was written. Consider a1.a2,a3,a4,a5,a6 to be numbers written on top face, bottom face, left face, right face, front face and back face of dice A. Similarly, consider b1.b2,b3,b4,b5,b6 to be numbers on specific faces of dice B. It’s guaranteed that all numbers written on dices are integers no smaller than 1 and no more than 6 while ai ≠ aj and bi ≠ bj for all i ≠ j. Specially, sum of numbers on opposite faces may not be 7.
       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)
       Now Ddy wants to calculate the minimal steps that he has to take to achieve his goal.
     
    【Input】
       There are multiple test cases. Please process till EOF.
       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.
     
    【Output】
       For each test case, print a line with a number representing the answer. If there’s no way to make two dices exactly the same, output -1.
     
    【Sample Input】
    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(不可能)。
     
    【分析】
    看到题目的第一感觉就是这种题肯定没办法直接通过数学上的关系解决,每种状态是6个数字,最多也就是6^6=46k多一点种,而实际上筛子有严格的对应关系,所以实际的状态量要比这个还少一点。
    于是采用BFS扩展状态即可,中间最重要的就是注意判重就好。
     
      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 }
    View Code

    代码写得也比较丑

    【启发】

    当时初看题目的时候瞟了几眼直接就跳过了,-_-///事后感觉没什么可以做了才回来看的,然后才发现其实很简单。还是应该认真看看题,比赛的时候至少每道题都要看过去,说不定能找到一些突破点。

    【H】

    Number Sequence

    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

    Special Judge

    【Problem Description】
       There is a special number sequence which has n+1 integers. For each number in sequence, we have two rules:
       ● ai ∈ [0,n]    ● ai ≠ aj( i ≠ j )
       For sequence a and sequence b, the integrating degree t is defined as follows(“⊕” denotes exclusive or):
    t = (a0 ⊕ b0) + (a1 ⊕ b1) +···+ (an ⊕ bn)
       (sequence B should also satisfy the rules described above)
       Now give you a number n and the sequence a. You should calculate the maximum integrating degree t and print the sequence b.
     
    【Input】
       There are multiple test cases. Please process till EOF.
       For each case, the first line contains an integer n(1 ≤ n ≤ 105), The second line contains a0,a1,a2,...,an.
     
    【Output】
       For each case, output two lines.The first line contains the maximum integrating degree t. The second line contains n+1 integers b0,b1,b2,...,bn. There is exactly one space between bi and bi+1(0 ≤ i ≤ n - 1). Don’t ouput any spaces after bn.
     
    【Sample Input】
    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 }
    View Code

    【启发】

    虽然当时还没想明白Special Judge是为什么(现在还是不明白...),不过以后碰到有思路的就直接上吧,说不定就对了呢。

    【K】

    Ellipsoid

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

    Special Judge

    【Problem Description】
       Given a 3-dimension ellipsoid(椭球面)
    your task is to find the minimal distance between the original point (0,0,0) and points on the ellipsoid. The distance between two points (x1,y1,z1) and (x2,y2,z2) is defined as
     
    【Input】
       There are multiple test cases. Please process till EOF.
       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.
     
    【Output】
       For each test contains one line. Describes the minimal distance. Answer will be considered as correct if their absolute error is less than 10-5.
     
    【Sample Input】
    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 }
    View Code

    个人认为这种搜索策略与真正的模拟退火算法还是有一点区别的,首先这里面根本没有涉及到向非最优解方向移动的情况。

    我的理解是,这里模仿了模拟退火算法对温度控制的特性。由于是求解原点与椭球面的距离,这个结果必然是一个凸函数,则不断修正前进的方向并逐步缩小搜索范围,最终到达最优解。

    首先指定一个步长与步长衰减速度(类退火速度),每次搜索完成一层,步长衰减一次,直到衰减程度在精度控制范围内。

    三分:

    二分用来求解线性函数的最优值,三分则是用来求解凸函数的最优值。

    对于本题来说,基本想法是以x,y,z中的其中两个变量进行两次三分,而后计算出第三个变量,并以最终的距离作为判断函数。由于这里都是实数,两层三分需要迭代的次数比较难以控制,参数稍微修正得不对,精度与时间上就会出现比较大的问题,这两个要求的参数是对立的,精度高了耗时就长,时间控制了,可能最终结果又错了。

    最后提交的是在超时边缘......中间出现了许多重复运算,这种还是不推荐使用了。

    Do Cool Things That Matter!
  • 相关阅读:
    C#呓语
    引起超时的原因及表解锁的方法<转>
    如何使用数据库引擎优化顾问优化数据库 <转>
    缩短IIS应用池回收时间,减少IIS假死<转>
    Microsoft Silverlight 4 Tools for Visual Studio 2010中文版本
    系统统一验证(IHttpHandlerFactory)<转>
    解决CSS BUG的顺口溜<转>
    重建索引提高SQL Server性能<转>
    .NET调用osql.exe执行sql脚本创建表和存储过程<转>
    SQL SERVER性能优化综述<转>
  • 原文地址:https://www.cnblogs.com/jcf94/p/3973796.html
Copyright © 2020-2023  润新知