Codeforces Round #269 (Div. 2)(hash)

来源:互联网 发布:ubuntu桌面主题 编辑:程序博客网 时间:2024/05/09 06:18

B. MUH and Important Things

It's time polar bears Menshykov and Uslada from the zoo of St. Petersburg and elephant Horace from the zoo of Kiev got down to business. In total, there aren tasks for the day and each animal should do each of these tasks. For each task, they have evaluated its difficulty. Also animals decided to do the tasks in order of their difficulty. Unfortunately, some tasks can have the same difficulty, so the order in which one can perform the tasks may vary.

Menshykov, Uslada and Horace ask you to deal with this nuisance and come up with individual plans for each of them. The plan is a sequence describing the order in which an animal should do all then tasks. Besides, each of them wants to have its own unique plan. Therefore three plans must form three different sequences. You are to find the required plans, or otherwise deliver the sad news to them by stating that it is impossible to come up with three distinct plans for the given tasks.

Input

The first line contains integer n (1 ≤ n ≤ 2000) — the number of tasks. The second line containsn integers h1, h2, ..., hn (1 ≤ hi ≤ 2000), where hi is the difficulty of thei-th task. The larger number hi is, the more difficult thei-th task is.

Output

In the first line print "YES" (without the quotes), if it is possible to come up with three distinct plans of doing the tasks. Otherwise print in the first line "NO" (without the quotes). If three desired plans do exist, print in the second line n distinct integers that represent the numbers of the tasks in the order they are done according to the first plan. In the third and fourth line print two remaining plans in the same form.

If there are multiple possible answers, you can print any of them.

Sample test(s)
Input
41 3 3 1
Output
YES1 4 2 3 4 1 2 3 4 1 3 2 
Input
52 4 1 4 8
Output
NO
Note

In the first sample the difficulty of the tasks sets one limit: tasks 1 and 4 must be done before tasks 2 and 3. That gives the total of four possible sequences of doing tasks : [1, 4, 2, 3], [4, 1, 2, 3], [1, 4, 3, 2], [4, 1, 3, 2]. You can print any three of them in the answer.

In the second sample there are only two sequences of tasks that meet the conditions — [3, 1, 2, 4, 5] and [3, 1, 4, 2, 5]. Consequently, it is impossible to make three distinct sequences of tasks.


#include<iostream>#include<cstdio>#include<string>#include<cstring>#include<vector>#include<cmath>#include<queue>#include<stack>#include<map>#include<set>#include<algorithm>using namespace std;const int maxn=2010;int N,cnt;int vis[maxn],path[maxn];struct node{    int x,id;    bool operator<(const node &a)const    {        return x<a.x;    }}a[maxn];void solve(){    int ans=1;    cnt=0;    for(int i=2;i<=N;i++)    {        if(a[i].x==a[i-1].x)        {            path[cnt++]=i-1;            if(cnt>1)break;        }    }    if(cnt<=1){printf("NO\n");return;}    printf("YES\n");    for(int i=1;i<N;i++)printf("%d ",a[i].id);    printf("%d\n",a[N].id);    swap(a[path[0]],a[path[0]+1]);    for(int i=1;i<N;i++)printf("%d ",a[i].id);    printf("%d\n",a[N].id);    swap(a[path[0]],a[path[0]+1]);    swap(a[path[1]],a[path[1]+1]);    for(int i=1;i<N;i++)printf("%d ",a[i].id);    printf("%d\n",a[N].id);}int main(){    scanf("%d",&N);    for(int i=1;i<=N;i++)    {        scanf("%d",&a[i].x);        a[i].id=i;        vis[a[i].x]++;    }    sort(a+1,a+1+N);    solve();    return 0;}

C. MUH and House of Cards

Polar bears Menshykov and Uslada from the zoo of St. Petersburg and elephant Horace from the zoo of Kiev decided to build a house of cards. For that they've already found a hefty deck ofn playing cards. Let's describe the house they want to make:

  1. The house consists of some non-zero number of floors.
  2. Each floor consists of a non-zero number of rooms and the ceiling. A room is two cards that are leaned towards each other. The rooms are made in a row, each two adjoining rooms share a ceiling made by another card.
  3. Each floor besides for the lowest one should contain less rooms than the floor below.

Please note that the house may end by the floor with more than one room, and in this case they also must be covered by the ceiling. Also, the number of rooms on the adjoining floors doesn't have to differ by one, the difference may be more.

While bears are practicing to put cards, Horace tries to figure out how many floors their house should consist of. The height of the house is the number of floors in it. It is possible that you can make a lot of different houses of different heights out of n cards. It seems that the elephant cannot solve this problem and he asks you to count the number of the distinct heights of the houses that they can make usingexactly n cards.

Input

The single line contains integer n (1 ≤ n ≤ 1012) — the number of cards.

Output

Print the number of distinct heights that the houses made of exactly n cards can have.

Sample test(s)
Input
13
Output
1
Input
6
Output
0
Note

In the first sample you can build only these two houses (remember, you must use all the cards):

Thus, 13 cards are enough only for two floor houses, so the answer is 1.

The six cards in the second sample are not enough to build any house.

思路:很容易发现每层的卡片数n都满足(n+1)%3==0;然后数学推导出如果n个卡片存在k层则有

3*a1-1+3*a2-1+3*a3-1+  +3*ak-1=n

===>3*(a1+a2+a3+  +ak)=n+k

===>a1+a2+a3+  +ak=(n+k)/3

所以有n+k必须被3整除

(n+k)/3必须大于等于f(k)=1+2+3+  +k

#include<iostream>#include<cstdio>#include<string>#include<cstring>#include<vector>#include<cmath>#include<queue>#include<stack>#include<map>#include<set>#include<algorithm>using namespace std;typedef long long LL;LL N;int main(){    cin>>N;    LL k=1;    int ans=0;    while(true)    {        if((N+k)%3==0)        {            if((N+k)/3<k*(k+1)/2)break;            ans++;        }        k++;    }    cout<<ans<<endl;    return 0;}

D. MUH and Cube Walls

Polar bears Menshykov and Uslada from the zoo of St. Petersburg and elephant Horace from the zoo of Kiev got hold of lots of wooden cubes somewhere. They started making cube towers by placing the cubes one on top of the other. They defined multiple towers standing in a line as a wall. A wall can consist of towers of different heights.

Horace was the first to finish making his wall. He called his wall an elephant. The wall consists ofw towers. The bears also finished making their wall but they didn't give it a name. Their wall consists ofn towers. Horace looked at the bears' tower and wondered: in how many parts of the wall can he "see an elephant"? He can "see an elephant" on a segment ofw contiguous towers if the heights of the towers on the segment match as a sequence the heights of the towers in Horace's wall. In order to see as many elephants as possible, Horace can raise and lower his wall. He even can lower the wall below the ground level (see the pictures to the samples for clarification).

Your task is to count the number of segments where Horace can "see an elephant".

Input

The first line contains two integers n andw (1 ≤ n, w ≤ 2·105) — the number of towers in the bears' and the elephant's walls correspondingly. The second line containsn integers ai (1 ≤ ai ≤ 109) — the heights of the towers in the bears' wall. The third line contains w integers bi (1 ≤ bi ≤ 109) — the heights of the towers in the elephant's wall.

Output

Print the number of segments in the bears' wall where Horace can "see an elephant".

Sample test(s)
Input
13 52 4 5 5 4 3 2 2 2 3 3 2 13 4 4 3 2
Output
2
Note

The picture to the left shows Horace's wall from the sample, the picture to the right shows the bears' wall. The segments where Horace can "see an elephant" are in gray.



hash要对MOD取模

#include<iostream>#include<cstdio>#include<string>#include<cstring>#include<vector>#include<cmath>#include<queue>#include<stack>#include<map>#include<set>#include<algorithm>using namespace std;typedef long long LL;const int maxn=200010;const int B=123;const int MOD=1000000007;int N,W;LL H[maxn],xp[maxn],bhash,sum;int a[maxn],b[maxn];void init(){    xp[0]=1;    for(int i=1;i<=W;i++)xp[i]=(xp[i-1]*B)%MOD;}void gethash(){    H[N]=0;    for(int i=N-1;i>=0;i--)        H[i]=((H[i+1]*B)%MOD+a[i])%MOD;}void solve(){    int ans=0;    for(int i=0;i+W-1<N;i++)    {        LL tmp=((H[i]-H[i+W]*xp[W]%MOD)%MOD+MOD)%MOD;        LL cha=sum*(a[i]-b[0])%MOD;        if(((tmp-cha)%MOD+MOD)%MOD==bhash)ans++;    }    printf("%d\n",ans);}int main(){    scanf("%d%d",&N,&W);    init();    for(int i=0;i<N;i++)scanf("%d",&a[i]);    for(int i=0;i<W;i++)scanf("%d",&b[i]);    if(W==1){printf("%d\n",N);return 0;}    gethash();    bhash=0,sum=0;    for(int i=W-1;i>=0;i--)    {        bhash=((bhash*B)%MOD+b[i])%MOD;        sum=(sum+xp[i])%MOD;    }    solve();}


0 0
原创粉丝点击