1. 程式人生 > 其它 >2018-2019 ICPC Southwestern European Regional Programming Contest (SWERC 2018)

2018-2019 ICPC Southwestern European Regional Programming Contest (SWERC 2018)

A - City of Lights

Gym - 102465A

Paris has been called "ville lumière" (city of lights) since the 17th century. It earned this nickname in part because of the many city lights illuminating famous sites such as monuments, statues, churches, or fountains.

Those public lights in Paris are numbered from11toNNand are all on by default. A group of hackers has gained the capability to toggle groups of lights. Every time the hackers use their program, they cause a numberii(that they cannot control) to be sent to the system controlling the city lights. The lights numberedii,2i2i,3i3i, and so on (up toNN) then change state instantly: lights that were on go off, and lights that were off go on.

During the night, the hackers use their programskktimes. What is the greatest number of lights that are simultaneously off at the same time?

Input

The input comprises several lines, each consisting of a single integer:

  • The first line contains the numberNNof lights.1≤N≤10000001≤N≤1000000.
  • The second line contains the numberkkof uses hackers's program.1≤k≤1001≤k≤100.
  • The nextkklines contain a numberiisent to the system controlling the lights.1≤i≤N1≤i≤N.

Output

The output should consist of a single line, whose content is an integer, the greatest number of lights that are simultaneously off at the same time.

Example

Input

10
4
6
2
1
3

Output

6

Note

Sample Explanation:

We start with a group of 10 lights which are all on.

The hackers send the number 6: light 6 is toggled.

They then send the number 2: lights 2, 4, 6, 8, and 10 are toggled.

The number 1 is then sent: all lights are toggled.

They end up sending the number 3: lights 3, 6, and 9 are toggled.

The maximum number of lights off at the same time was 6.

題解:

#include <bits/stdc++.h>
using namespace std;

int a[1011010];
int main()
{
    int n,k,b;
    scanf("%d",&n);
    scanf("%d",&k);
    int sum=0;
    int max1=0;
    for(int i=1; i<=k; i++)
    {
        scanf("%d",&b);
        for(int j=1; j*b<=n; j++)
        {
            int x=j*b;
            if(a[x]==0)
            {
                a[x]=1;
                sum++;
            }
            else
            {
                a[x]=0;
                sum--;
            }

        }
        max1=max(max1,sum);
    }
    printf("%d\n",max1);
    return 0;
}

B - Blurred Pictures

Gym - 102465B

Damon loves to take photos of the places he visits during his travels, to put them into frames. All of his photos are in a square format ofN×NN×Npixels. He brought back beautiful pictures of the many monuments in Paris, such as the Eiffel Tower or the Louvre, but unfortunately, when he got back home, he realized that all his pictures were blurred on the edges. Looking closely, Damon realizes that he can easily distinguish the blurred pixels from the "good" (i.e., non-blurred) ones and that, luckily, all the non-blurred pixels are connected in such a way that any horizontal or vertical line drawn between two non-blurred pixels goes only through non-blurred pixels. In order to get the best from his failed pictures, he decides to cut out the biggest possible picture without any blurred pixel from each of his photos. And since his frames are all squares, for aesthetic reasons, the cut-out pictures have to be squares too. Damon does not want his pictures to be tilted so he wants the sides of the cut-outs to be parallel to the sides of the original picture.

Input

The input comprises several lines, each consisting of integers separated with single spaces:

  • The first line contains the lengthNN, in pixels, of the input photo;
  • Each of the nextNNlines contains two integersaiaiandbibi, the indices of the first(ai)(ai)and the last(bi)(bi)non-blurred pixel on theii-th line.
  • 0<N≤1000000<N≤100000,
  • 0≤ai≤bi<N0≤ai≤bi<N.

Output

The output should consist of a single line, whose content is an integer, the length of the largest square composed of non-blurred pixels inside the picture.

Examples

Input

3
1 1
0 2
1 1

Output

1

Input

8
2 4
2 4
1 4
0 7
0 3
1 2
1 2
1 1

Output

3

Note

  • In the input picture, each row and each column has at least one non-blurred pixel.
  • In any two consecutive lines, there are at least two non-blurred pixels in the same column.

#include <bits/stdc++.h>
using namespace std;
map<string,int>p;
int n,a[100010],b[100010];
bool init(int x,int k)
{
    if(b[x]-a[x]<k)
    {
        return false;
    }
    if(x+k>n)
    {
        return false;
    }
    int sum=max(a[x],a[x+k]);
    if(b[x]-sum>=k&&b[x+k]-sum>=k)
    {
        return true;
    }
    else
    {
        return false;
    }
}
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d %d",&a[i],&b[i]);
    }
    int k=1;
    for(int i=1;i<=n;i++)
    {
        while(init(i,k))
            k++;
    }
    printf("%d\n",k);
    return 0;
}

D - Monument Tour

Gym - 102465D

A tour operator proposes itineraries consisting of visits ofNNmonuments and museums in Paris, modeled as a grid. The way the tour operates is the following: The bus enters the city from the west (on any street) and traverses the city, taking a left or a right turn to visit monuments when needed, returning to the same eastbound road it used to enter the city, and so on until it exits the city via the east.

A tour in a6×56×5grid city might look like the figure above. On the figure, the bus enters the city at coordinate(0,2)(0,2)(we consider(0,0)(0,0)to be the northwest corner of the city), first visits the monument at(1,2)(1,2)(already on the main road), takes a left turn and visits the monument at(1,0)(1,0), returns to the main road and moves east, takes a right turn and visits the monument at(2,4)(2,4), returns to the main road, visits the monument at(4,2)(4,2)(again on the main road), and then exits the city at coordinate(5,2)(5,2).

The bus operator counts that the traversal of one block costs 1 unit of gas. For the example above, the cost is thus5+2×2+2×2=135+2×2+2×2=13units of gas.

Your task is to help the tour operator choose which eastbound road the bus should travel through, so that the cost of the tour is minimized while visiting allNNmonuments.

Input

The input comprises several lines, each consisting of integers separated with single spaces:

  • The first line contains the numberXXof northbound streets and the numberYYof eastbound streets.
  • The second line contains the numberNNof monuments the tour needs to visit.
  • The next N lines each contain the coordinatesxixiandyiyiof each monument.
  • 1≤X,Y≤1000001≤X,Y≤100000;
  • 1≤N≤1000001≤N≤100000;
  • 0≤xi<X,0≤yi<Y0≤xi<X,0≤yi<Y.

Output

The output should consist of a single line, whose content is an integer, representing the minimal cost of a tour.

Examples

Input

6 5
4
1 0
1 2
2 4
4 2

Output

13

Input

5 7
9
0 0
0 2
0 3
2 2
2 3
3 2
4 3
4 4
4 6

Output

20

Note

  • The bus operator is not allowed to return to a different, parallel, eastbound road; they have to use the same road as the one by which they entered the city.
  • More than one monument can be present at the same coordinate.
#include <bits/stdc++.h>
using namespace std;
map<string,int>p;
const int inf=0x3f3f3f3f;
struct node
{
    int min1,max1;
} f[101010],s[101010];
int a[101010];
int main()
{
    int maxx,maxy,xx,yy;
    maxx=maxy=xx=yy=0;
    int n,m;
    scanf("%d %d",&n,&m);
    int t;
    scanf("%d",&t);
    int top=1;
    for(int i=0; i<=n; i++)
    {
        f[i].min1=inf;
        f[i].max1=-1;
    }
    while(t--)
    {
        int x,y;
        scanf("%d %d",&x,&y);
        if(y<f[x].min1)
        {
            f[x].min1=y;
        }
        if(y>f[x].max1)
        {
            f[x].max1=y;
        }
        if(x<s[y].min1)
        {
            s[y].min1=x;
        }
        if(x>s[y].max1)
        {
            s[y].max1=x;
        }
    }
     for(int i=0; i<n; i++)
    {
        if(f[i].min1!=inf&&f[i].max1!=-1)
        {

           a[top++]=f[i].min1;
           a[top++]=f[i].max1;
        }
    }
    sort(a,a+top);
    top=top-1;
    yy=(a[top/2]);
    long long int sum=n-1;
    for(int i=0; i<n; i++)
    {
        if(f[i].min1!=inf&&f[i].max1!=-1)
            sum+=f[i].max1-f[i].min1+abs(yy-f[i].max1)+abs(yy-f[i].min1);
    }
    printf("%lld\n",sum);
    return 0;
}