您的位置:首页 > 其它

Codeforces Round #332 (Div. 2)

2016-08-17 20:07 513 查看
B.Spongebob and Joke

time limit per test
2 seconds

memory limit per test
256 megabytes

input
standard input

output
standard output

While Patrick was gone shopping, Spongebob decided to play a little trick on his friend. The naughty Sponge browsed through Patrick's personal stuff and found a sequence a1, a2, ..., am of
length m, consisting of integers from 1 to n,
not necessarily distinct. Then he picked some sequence f1, f2, ..., fn of
length n and for each number ai got
number bi = fai.
To finish the prank he erased the initial sequence ai.

It's hard to express how sad Patrick was when he returned home from shopping! We will just say that Spongebob immediately got really sorry about what he has done and he is now trying to restore the original sequence. Help him do this or determine that this
is impossible.

Input

The first line of the input contains two integers n and m (1 ≤ n, m ≤ 100 000) —
the lengths of sequences fi and bi respectively.

The second line contains n integers, determining sequence f1, f2, ..., fn (1 ≤ fi ≤ n).

The last line contains m integers, determining sequence b1, b2, ..., bm (1 ≤ bi ≤ n).

Output

Print "Possible" if there is exactly one sequence ai,
such that bi = fai for
all i from 1 to m.
Then print m integers a1, a2, ..., am.

If there are multiple suitable sequences ai,
print "Ambiguity".

If Spongebob has made a mistake in his calculations and no suitable sequence ai exists,
print "Impossible".

Examples

input
3 3
3 2 1
1 2 3


output
Possible
3 2 1


input
3 3
1 1 1
1 1 1


output
Ambiguity


input
3 3
1 2 1
3 3 3


output
Impossible


Note

In the first sample 3 is replaced by 1 and
vice versa, while 2 never changes. The answer exists and is unique.

In the second sample all numbers are replaced by 1, so it is impossible to unambiguously restore the original sequence.

In the third sample fi ≠ 3 for
all i, so no sequence ai transforms
into such bi and
we can say for sure that Spongebob has made a mistake.

#include<stdio.h>

#include<string.h>

int main()

{
int n,m,i,ans,sum;
int f[100010],b[100010],a[100010],visit[100010];
while(~scanf("%d%d",&n,&m))
{
memset(visit,0,sizeof(visit));
memset(a,0,sizeof(a));
for(i=1;i<=n;i++)
scanf("%d",&f[i]);
for(i=1;i<=m;i++)
scanf("%d",&b[i]);
for(i=1;i<=n;i++)
{
a[f[i]]=i;
visit[f[i]]++;
}
ans=0;
sum=0;
for(i=1;i<=m;i++)
{
 if(visit[b[i]]>1)
 ans=1;
  if(!a[b[i]])
  {
   sum=1;
   break;
  }
}
   if(sum)
   printf("Impossible\n");
   else if(ans)
   printf("Ambiguity\n");
   else
   {
   printf("Possible\n");
    printf("%d",a[b[1]]);
    for(i=2;i<=m;i++)
    printf(" %d",a[b[i]]);
    printf("\n");
}
}
return 0;

}

C. Day at the Beach

time limit per test
2 seconds

memory limit per test
256 megabytes

input
standard input

output
standard output

One day Squidward, Spongebob and Patrick decided to go to the beach. Unfortunately, the weather was bad, so the friends were unable to ride waves. However, they decided to spent their time building sand castles.

At the end of the day there were n castles built by friends. Castles are numbered from 1 to n,
and the height of the i-th castle is equal tohi.
When friends were about to leave, Squidward noticed, that castles are not ordered by their height, and this looks ugly. Now friends are going to reorder the castles in a way to obtain that condition hi ≤ hi + 1 holds
for all i from 1 to n - 1.

Squidward suggested the following process of sorting castles:

Castles are split into blocks — groups of consecutive castles. Therefore the block from i to j will
include castles i, i + 1, ..., j. A block may consist of a single castle.

The partitioning is chosen in such a way that every castle is a part of exactly one block.

Each block is sorted independently from other blocks, that is the sequence hi, hi + 1, ..., hj becomes
sorted.

The partitioning should satisfy the condition that after each block is sorted, the sequence hi becomes
sorted too. This may always be achieved by saying that the whole sequence is a single block.

Even Patrick understands that increasing the number of blocks in partitioning will ease the sorting process. Now friends ask you to count the maximum possible number of blocks in a partitioning that satisfies all the above requirements.

Input

The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) —
the number of castles Spongebob, Patrick and Squidward made from sand during the day.

The next line contains n integers hi (1 ≤ hi ≤ 109).
The i-th of these integers corresponds to the height of the i-th
castle.

Output

Print the maximum possible number of blocks in a valid partitioning.

Examples

input
3
1 2 3


output
3


input
4
2 1 3 2


output
2


Note

In the first sample the partitioning looks like that: [1][2][3].



In the second sample the partitioning is: [2, 1][3, 2]



题意:把一组数据尽可能的分组,对每一组进行升序排序,排完后整个序列也是升序,问最多可以分为几组。分组后每一组的最大值都要小于等于后一组的最小值,所以[1,i]最大值小于等于[i,n]最小值的话,可以分。所以把[1,i]的最小值求出来,[n,i]的最大值求出来
#include<stdio.h>

int main()

{
int n,i,j;
int a[100010],max[100010],min[100010];
while(~scanf("%d",&n))
{
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
max[0]=0;
min[n+1]=1e9+10;
for(i=1;i<n;i++)
{
if(a[i]>=max[i-1])
max[i]=a[i];
else
max[i]=max[i-1];
}
for(j=n;j>=1;j--)
{
if(min[j+1]>=a[j])
min[j]=a[j];
else 
min[j]=min[j+1];
}
int ans=0;
for(i=1;i<n;i++)
{
if(max[i]<=min[i+1])
ans++;
}
printf("%d\n",ans+1);
}
return 0;

}

D. Spongebob and Squares

time limit per test
2 seconds

memory limit per test
256 megabytes

input
standard input

output
standard output

Spongebob is already tired trying to reason his weird actions and calculations, so he simply asked you to find all pairs of n and m, such that there are exactly x distinct
squares in the table consisting of n rows and m columns.
For example, in a 3 × 5 table there are 15squares
with side one, 8 squares with side two and 3 squares
with side three. The total number of distinct squares in a 3 × 5 table is15 + 8 + 3 = 26.

Input

The first line of the input contains a single integer x (1 ≤ x ≤ 1018) —
the number of squares inside the tables Spongebob is interested in.

Output

First print a single integer k — the number of tables with exactly x distinct
squares inside.

Then print k pairs of integers describing the tables. Print the pairs in the order of increasing n,
and in case of equality — in the order of increasing m.

Examples

input
26


output
6
1 262 9
3 5
5 39 226 1


input
2


output
21 22 1


input
8


output
4
1 82 33 28 1


Note

In a 1 × 2 table there are 2 1 × 1 squares.
So, 2 distinct squares in total.



In a 2 × 3 table there are 6 1 × 1 squares
and 2 2 × 2 squares. That
is equal to 8 squares in total.



对于n*m,假设n<=m,可以分成的正方形个数为x=sigma(n-i)(m-i) (0<=i<n),用到公式1^2+2^2+3^2+...+n^2=n*(n+1)*(2*n+1)/6
化简得6*x=-n*n*n+n+3*n*n*m+3*n*m(n<=m,x<=1e18,故n<6*1e6)
m=(6*x+n*n*n-n)/(3*n*n+3*n)
此外写这道题又学到了vectotr容器的用法
#include<stdio.h>

#include<vector>

using namespace std;

#define INF 6*1e6

#define LL __int64

struct node{
LL x,y;

};

vector<node>ans;

void fas(LL x)

{
for(LL i=1;i<=INF;i++)
{
LL a=6*x+i*i*i-i;
LL b=3*i*i+3*i;
if(a%b)
continue;
LL m=a/b;
if(i>m)
break;
node now;
now.x=i;
now.y=m;
ans.push_back(now);
}

}

int main()

{
LL x;
while(~scanf("%I64d",&x))
{
ans.clear();
fas(x);
int top=ans.size()-1;
if(ans[top].x==ans[top].y)
top=2*ans.size()-1;
else
top=2*ans.size();
printf("%d\n",top);
top=ans.size()-1;
for(int i=0;i<=top;i++)
printf("%I64d %I64d\n",ans[i].x,ans[i].y);
if(ans[top].x==ans[top].y)
top--;
for(int i=top;i>=0;i--)
printf("%I64d %I64d\n",ans[i].y,ans[i].x);

}
return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: