BestCoder Round #43

链接:http://bestcoder.hdu.edu.cn/contests/contest_show.php?cid=599

A、

pog loves szh I

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 393    Accepted Submission(s): 271


Problem Description
Pog has lots of strings. And he always mixes two equal-length strings. For example, there are two strings: "abcd" and "efgh". After mixing, a new string "aebfcgdh" is coming.

However, szh thinks it is boring, so he reverses the second string, like changing "efgh" to "hgfe". Then mix them as usual, resulting in "ahbgcfde".

Now, here comes a problem. Pog is given a string after mixing by szh, but he wants to find the original two strings. 

Hint : In this question, it needs a linefeed at the end of line at hack time.
 
Input
The first line has an integer,  T(1T100), indicating the number of cases.

Then follows T lines. Every lines has a string S, whose length is even and not more than 100, and only consists of lower-case characters('a'~'z').
 
Output
For each cases, please output two lines, indicating two original strings.
 
Sample Input
1 aabbca
 
Sample Output
abc aba For example, there are two strings: "abcd" and "efgh". After mixing, a new string "aebfcgdh" is coming.
 
Source
BestCoder Round #43
 
没什么可以说、- -
#include <iostream>

#include <cstring>

#include <algorithm>

#include <cstdio>

#include <string>

#include <map>

using namespace std;

#define ll long long

#define INF 0x3f3f3f3f

#define N 2010



int len;

char s[N];

string s1,s2;

int main()

{

    int T;

    scanf("%d",&T);

    while(T--)

    {

        s1=s2="";

        scanf("%s",s+1);

        len=strlen(s+1);

        for(int i=1;i<=len;i+=2)

        {

            s1+=s[i];

            s2+=s[i+1];

        }

        reverse(s2.begin(),s2.end());

        cout<<s1<<endl<<s2<<endl;

    }

    return 0;

}

 

B、

pog loves szh II

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1737    Accepted Submission(s): 509


Problem Description
Pog and Szh are playing games.There is a sequence with  n numbers, Pog will choose a number A from the sequence. Szh will choose an another number named B from the rest in the sequence. Then the score will be (A+B) mod p.They hope to get the largest score.And what is the largest score?
 
Input
Several groups of data (no more than  5 groups,n1000).

For each case: 

The following line contains two integers,n(2n100000)p(1p2311)

The following line contains n integers ai(0ai2311)
 
Output
For each case,output an integer means the largest score.
 
Sample Input
4 4 1 2 3 0 4 4 0 0 2 2
 
Sample Output
3 2
 
Source
BestCoder Round #43
 
题意:在n个数中选出两个数,使得两数和模P最大,输出这个最大值
分析:首先我们可以先把每个数模P,这样任意两个数的和一定在0到2P-2之间、
     然后将数列从小到大排序,一次枚举每个数,一个数+另一个数要想最大,
     要么相加的和<P,要么>P再模P,对于前者我们可以二分查找使得加上的
     在<P范围内最大,对于后者,显然加上最后的那个最大的数再模P最大,
     取两者最大值即可。
#include <iostream>

#include <cstring>

#include <algorithm>

#include <cstdio>

#include <string>

#include <map>

using namespace std;

#define ll long long

#define INF 0x3f3f3f3f

#define N 100010



int n,p;

int a[N];



template <class T>

inline bool input(T &ret)

{

    char c; int sgn;

    if(c=getchar(),c==EOF) return 0;

    while(c!='-'&&(c<'0'||c>'9')) c=getchar();

    sgn=(c=='-')?-1:1;

    ret=(c=='-')?0:(c-'0');

    while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0');

    ret*=sgn;

    return 1;

}



int main()

{

    while(input(n))

    {

        input(p);

        for(int i=1;i<=n;i++)

        {

            input(a[i]);

            if(a[i]>=p) a[i]%=p;

        }

        sort(a+1,a+n+1);



        int ans=-1;

        for(int i=1;i<=n;i++)

        {

            int UP=p-1-a[i];

            int x=upper_bound(a+1,a+n+1,UP)-a;

            if(x-1>i)

                ans=max(ans,a[i]+a[x-1]);

            if(i!=n)

                ans=max(ans,a[i]+a[n]-p);

        }

        printf("%d\n",ans);

    }

    return 0;

}

 

C、

pog loves szh III

Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 781    Accepted Submission(s): 164


Problem Description
Pog and Szh are playing games. Firstly Pog draw a tree on the paper. Here we define 1 as the root of the tree.Then Szh choose some nodes from the tree. He wants Pog helps to find the least common ancestor (LCA) of these node.The question is too difficult for Pog.So he decided to simplify the problems.The nodes picked are consecutive numbers from  li to ri ([li,ri]).

Hint : You should be careful about stack overflow !
 
Input
Several groups of data (no more than 3 groups, n10000 or Q10000).

The following line contains ans integers,n(2n300000).

AT The following n1 line, two integers are bi and ci at every line, it shows an edge connecting bi and ci

The following line contains ans integers,Q(Q300000).

AT The following Q line contains two integers li and ri(1lirin).
 
Output
For each case,output  Q integers means the LCA of [li,ri].
 
Sample Input
5 1 2 1 3 3 4 4 5 5 1 2 2 3 3 4 3 5 1 5
 
Sample Output
1 1 3 3 1
Hint
Be careful about stack overflow.
 
Source
BestCoder Round #43
 
通过在简单的证明可知L~R的LCA为L~R中dfs序较小的那个位置与dfs序较大的那个位置的LCA
那么利用LCA的在线算法即可,相关算法 Click here
#include <iostream>

#include <cstdio>

#include <cstring>

#include <cmath>

using namespace std;

#pragma comment(linker, "/STACK:1024000000,1024000000")  //防爆栈,也可以使用bfs代替dfs

#define min(a,b) ((a)<(b)?(a):(b))

#define N 300010

#define M N*2



struct edge

{

    int u,v,next;

}edge[M];

int tot,head[N];



int n,m;

int idx;

bool vis[N];

int ver[2*N];

int dep[2*N];

int first[N];

int dp[2*N][20];



template <class T>

inline bool input(T &ret)

{

    char c; int sgn;

    if(c=getchar(),c==EOF) return 0;

    while(c!='-'&&(c<'0'||c>'9')) c=getchar();

    sgn=(c=='-')?-1:1;

    ret=(c=='-')?0:(c-'0');

    while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0');

    ret*=sgn;

    return 1;

}

void init()

{

    tot=0;

    memset(head,-1,sizeof(head));

}

void add(int u,int v)

{

    edge[tot].u=u;

    edge[tot].v=v;

    edge[tot].next=head[u];

    head[u]=tot++;

}

void init2()

{

    idx=0;

    memset(vis,0,sizeof(vis));

}

void dfs(int u,int d)

{

    vis[u]=1;

    ver[++idx]=u;

    first[u]=idx;

    dep[idx]=d;

    for(int i=head[u];i!=-1;i=edge[i].next){

        int v=edge[i].v;

        if(!vis[v]){

            dfs(v,d+1);

            ver[++idx]=u;

            dep[idx]=d;

        }

    }

}

void ST(int n)

{

    int i,j;

    for(i=1;i<=n;i++) dp[i][0]=i;

    for(j=1;(1<<j)<=n;j++){

        for(i=1;i<=n;i++){

            if(i+(1<<j)-1<=n){

                int a=dp[i][j-1];

                int b=dp[i+(1<<(j-1))][j-1];

                dp[i][j]=dep[a]<dep[b]?a:b;

            }

        }

    }

}

int RMQ(int i,int j)

{

    int k=(int)(log((double)(j-i+1))/log(2.0));

    int a=dp[i][k],b=dp[j-(1<<k)+1][k];

    return dep[a]<dep[b]?a:b;

}

int LCA(int u,int v)

{

    int x=first[u];

    int y=first[v];

    if(x>y) swap(x,y);

    int res=RMQ(x,y);

    return ver[res];

}

int main()

{

    while(input(n))

    {

        init();

        for(int i=1;i<n;i++)

        {

            int u,v,w;

            input(u);

            input(v);

            add(u,v);

            add(v,u);

        }

        init2();

        dfs(1,0);

        ST(2*n-1);

        input(m);

        while(m--)

        {

            int u,v;

            input(u);

            input(v);

            printf("%d\n",LCA(u,v));

        }

    }

    return 0;

}

 

D、

pog loves szh IV

Time Limit: 24000/12000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 78    Accepted Submission(s): 13


Problem Description
Pog plays a game with szh and this time he finds another tree, this tree is different from the previous one, all nodes of which have a weight! And the weight of the  ith node is ai

Pog and Szh choose two different nodes on the tree, then pog walks to szh along the paths of the tree and records the weight. Because of too many nodes, pog only records the XOR of all weight of the nodes he passes for convenient.Due to szh's love for pog,szh wanders the sum of which pog records for any n(n1)different combinations.

Because of tired of szh's corny pursuit, pog changes aA into B at every moment, of course, szh must output the answer of all moment.
 
Input
Several groups of data (no more than  3 groups,n1000).

In every group, two integers at first line are n(2n10000) and Q(1Q10000).

At the following line, n integers are ai(0ai<32768).

At the following n1 line, two integers are bi and ci at every line, it shows an edge connecting bi and ci.

At the following Q line, two integers are A(1An) and B(0B<32768) at every line, it shows that changing aA into B.
 
Output
For each inquire to each group of data, output a line , which shows all the sum of which pog records in different combinations.
 
Sample Input
5 5 1 2 3 4 5 1 2 1 3 2 4 2 5 1 3 2 4 4 4 5 3 2 1
 
Sample Output
82 46 46 70 60
 
Source
BestCoder Round #43
 
从没看过D题、- -

你可能感兴趣的:(round)