Codeforces Round #536 (Div. 2)(A,B,C,D)题解

比赛链接

A. Lunar New Year and Cross Counting

Lunar New Year is approaching, and you bought a matrix with lots of "crosses".

This matrix MM of size n×nn×n contains only 'X' and '.' (without quotes). The element in the ii-th row and the jj-th column (i,j)(i,j) is defined as M(i,j)M(i,j), where 1≤i,j≤n1≤i,j≤n. We define a crossappearing in the ii-th row and the jj-th column (1<i,j<n1<i,j<n) if and only if M(i,j)=M(i−1,j−1)=M(i−1,j+1)=M(i+1,j−1)=M(i+1,j+1)=M(i,j)=M(i−1,j−1)=M(i−1,j+1)=M(i+1,j−1)=M(i+1,j+1)='X'.

The following figure illustrates a cross appearing at position (2,2)(2,2) in a 3×33×3 matrix.

X.X
.X.
X.X

Your task is to find out the number of crosses in the given matrix MM. Two crosses are different if and only if they appear in different rows or columns.

Input

The first line contains only one positive integer nn (1≤n≤5001≤n≤500), denoting the size of the matrix MM.

The following nn lines illustrate the matrix MM. Each line contains exactly nn characters, each of them is 'X' or '.'. The jj-th element in the ii-th line represents M(i,j)M(i,j), where 1≤i,j≤n1≤i,j≤n.

Output

Output a single line containing only one integer number kk — the number of crosses in the given matrix MM.

Examples

input

5
.....
.XXX.
.XXX.
.XXX.
.....

output

1

input

2
XX
XX

output

0

input

6
......
X.X.X.
.X.X.X
X.X.X.
.X.X.X
......

output

4

Note

In the first sample, a cross appears at (3,3)(3,3), so the answer is 11.

In the second sample, no crosses appear since n<3n<3, so the answer is 00.

In the third sample, crosses appear at (3,2)(3,2), (3,4)(3,4), (4,3)(4,3), (4,5)(4,5), so the answer is 44.

题意:就是给出一个图,找出图中所有满足这种位置摆设方式的点M(i,j)=M(i−1,j−1)=M(i−1,j+1)=M(i+1,j−1)=M(i+1,j+1)=M(i,j)=M(i−1,j−1)=M(i−1,j+1)=M(i+1,j−1)=M(i+1,j+1)='X。

题解:因为图最大只有500*500,所有暴力枚举每一个点就行。

#include <iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<map>
#include<queue>
#include<set>
#include<cmath>
#include<stack>
#include<string>
const int maxn=1e3+10;
const int mod=1e9+7;
const int inf=0x3f3f3f3f;
#define me(a,b) memset(a,b,sizeof(a))
#define lowbit(x) x&(-x)
#define mid (l+r)/2
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
int dir[4][2]= {0,1,0,-1,1,0,-1,0};
typedef long long ll;
using namespace std;
char maps[maxn][maxn];
int n;
int check(int x,int y)
{
    if(x<0||y<0||x>=n||y>=n||maps[x][y]=='.')
        return 0;
    return 1;
}
int main()
{

    scanf("%d",&n);
    for(int i=0;i<n;i++)
        scanf("%s",maps[i]);
    int sum=0;
    for(int i=0;i<n;i++)
        for(int j=0;j<n;j++)
        {
            if(check(i,j)&&check(i-1,j-1)&&check(i-1,j+1)&&check(i+1,j-1)&&check(i+1,j+1))
                sum++;
        }
    printf("%d\n",sum);
    return 0;
}

B. Lunar New Year and Food Ordering

Lunar New Year is approaching, and Bob is planning to go for a famous restaurant — "Alice's".

The restaurant "Alice's" serves nn kinds of food. The cost for the ii-th kind is always cici. Initially, the restaurant has enough ingredients for serving exactly aiai dishes of the ii-th kind. In the New Year's Eve, mm customers will visit Alice's one after another and the jj-th customer will order djdjdishes of the tjtj-th kind of food. The (i+1)(i+1)-st customer will only come after the ii-th customer is completely served.

Suppose there are riri dishes of the ii-th kind remaining (initially ri=airi=ai). When a customer orders 11 dish of the ii-th kind, the following principles will be processed.

  1. If ri>0ri>0, the customer will be served exactly 11 dish of the ii-th kind. The cost for the dish is cici. Meanwhile, riri will be reduced by 11.
  2. Otherwise, the customer will be served 11 dish of the cheapest available kind of food if there are any. If there are multiple cheapest kinds of food, the one with the smallest index among the cheapest will be served. The cost will be the cost for the dish served and the remain for the corresponding dish will be reduced by 11.
  3. If there are no more dishes at all, the customer will leave angrily. Therefore, no matter how many dishes are served previously, the cost for the customer is 00.

If the customer doesn't leave after the djdj dishes are served, the cost for the customer will be the sum of the cost for these djdj dishes.

Please determine the total cost for each of the mm customers.

Input

The first line contains two integers nn and mm (1≤n,m≤1051≤n,m≤105), representing the number of different kinds of food and the number of customers, respectively.

The second line contains nn positive integers a1,a2,…,ana1,a2,…,an (1≤ai≤1071≤ai≤107), where aiai denotes the initial remain of the ii-th kind of dishes.

The third line contains nn positive integers c1,c2,…,cnc1,c2,…,cn (1≤ci≤1061≤ci≤106), where cici denotes the cost of one dish of the ii-th kind.

The following mm lines describe the orders of the mm customers respectively. The jj-th line contains two positive integers tjtj and djdj (1≤tj≤n1≤tj≤n, 1≤dj≤1071≤dj≤107), representing the kind of food and the number of dishes the jj-th customer orders, respectively.

Output

Print mm lines. In the jj-th line print the cost for the jj-th customer.

Examples

input

8 5
8 6 2 1 4 5 7 5
6 3 3 2 6 2 3 2
2 8
1 4
4 7
3 4
6 10

output

22
24
14
10
39

input

6 6
6 6 6 6 6 6
6 66 666 6666 66666 666666
1 6
2 6
3 6
4 6
5 6
6 66

output

36
396
3996
39996
399996
0

input

6 6
6 6 6 6 6 6
6 66 666 6666 66666 666666
1 6
2 13
3 6
4 11
5 6
6 6

output

36
11058
99996
4333326
0
0

Note

In the first sample, 55 customers will be served as follows.

  1. Customer 11 will be served 66 dishes of the 22-nd kind, 11 dish of the 44-th kind, and 11 dish of the 66-th kind. The cost is 6⋅3+1⋅2+1⋅2=226⋅3+1⋅2+1⋅2=22. The remain of the 88 kinds of food will be {8,0,2,0,4,4,7,5}{8,0,2,0,4,4,7,5}.
  2. Customer 22 will be served 44 dishes of the 11-st kind. The cost is 4⋅6=244⋅6=24. The remain will be {4,0,2,0,4,4,7,5}{4,0,2,0,4,4,7,5}.
  3. Customer 33 will be served 44 dishes of the 66-th kind, 33 dishes of the 88-th kind. The cost is 4⋅2+3⋅2=144⋅2+3⋅2=14. The remain will be {4,0,2,0,4,0,7,2}{4,0,2,0,4,0,7,2}.
  4. Customer 44 will be served 22 dishes of the 33-rd kind, 22 dishes of the 88-th kind. The cost is 2⋅3+2⋅2=102⋅3+2⋅2=10. The remain will be {4,0,0,0,4,0,7,0}{4,0,0,0,4,0,7,0}.
  5. Customer 55 will be served 77 dishes of the 77-th kind, 33 dishes of the 11-st kind. The cost is 7⋅3+3⋅6=397⋅3+3⋅6=39. The remain will be {1,0,0,0,4,0,0,0}{1,0,0,0,4,0,0,0}.

In the second sample, each customer is served what they order except the last one, who leaves angrily without paying. For example, the second customer is served 66 dishes of the second kind, so the cost is 66⋅6=39666⋅6=396.

In the third sample, some customers may not be served what they order. For example, the second customer is served 66 dishes of the second kind, 66 of the third and 11 of the fourth, so the cost is 66⋅6+666⋅6+6666⋅1=1105866⋅6+666⋅6+6666⋅1=11058.

题意:有n种菜,每种菜的数量和价格都不一样,现在有m位客人点菜,给出每个客人要点的菜的序号和数量,要是客人点的菜的剩了没那么多,就拿店里的最便宜的菜往上补,直到补够该客人需要菜的数量。要是最后菜的数量都补不够,该客人的之前点的所有菜都不用付钱。

题解:我们首先开个数组记录每种菜品的数量,然后再开一个优先队列来维护当前剩余的价格最便宜的菜的编号和价格,每当该菜品的数量为0后就将该种菜品从优先队列中弹出,每次维护就好了,细节看代码。

#include <iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<map>
#include<queue>
#include<set>
#include<cmath>
#include<stack>
#include<string>
const int maxn=1e5+10;
const int mod=1e9+7;
const int inf=0x3f3f3f3f;
#define me(a,b) memset(a,b,sizeof(a))
#define lowbit(x) x&(-x)
#define mid (l+r)/2
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
int dir[4][2]= {0,1,0,-1,1,0,-1,0};
typedef long long ll;
using namespace std;
int n,m;
struct node
{
    ll c,i;
    node(ll c0=0,ll i0=0):c(c0),i(i0){}
    bool friend operator<(node a,node b)
    {
        return a.c>b.c;
    }
}a[maxn];
ll s[maxn];
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
        scanf("%lld",&s[i]);
    for(int i=1;i<=n;i++)
        scanf("%lld",&a[i].c);
    priority_queue<node>q;
    for(int i=1;i<=n;i++)
        q.push(node(a[i].c,i));
    while(m--)
    {
        ll t,d;
        scanf("%lld%lld",&t,&d);
        if(s[t]>=d)///选的菜的剩余数量大于客人要的数量。
        {
            printf("%lld\n",a[t].c*d);
            s[t]-=d;
        }
        else
        {
            ll temp=d-s[t];
            ll sum=0;
            sum+=s[t]*a[t].c,s[t]=0;
            while(!q.empty()&&temp)
            {
                node now=q.top();
                if(s[now.i]>temp)///当前最便宜菜的剩余数量大于客人要的数量。
                {
                    sum+=temp*a[now.i].c;
                    s[now.i]-=temp,temp=0;
                }
                else///当前最便宜菜的剩余数量小于等于客人要的数量,该种菜品被选完,从队列中弹出。
                {
                    sum+=s[now.i]*a[now.i].c;
                    temp-=s[now.i],s[now.i]=0;
                    q.pop();
                }
            }
            if(temp==0)
                printf("%lld\n",sum);
            else
                printf("0\n");
        }
    }
    return 0;
}

C. Lunar New Year and Number Division

Lunar New Year is approaching, and Bob is struggling with his homework – a number division problem.

There are nn positive integers a1,a2,…,ana1,a2,…,an on Bob's homework paper, where nn is always an even number. Bob is asked to divide those numbers into groups, where each group must contain at least 22 numbers. Suppose the numbers are divided into mm groups, and the sum of the numbers in the jj-th group is sjsj. Bob's aim is to minimize the sum of the square of sjsj, that is

∑j=1ms2j.∑j=1msj2.

Bob is puzzled by this hard problem. Could you please help him solve it?

Input

The first line contains an even integer nn (2≤n≤3⋅1052≤n≤3⋅105), denoting that there are nn integers on Bob's homework paper.

The second line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1041≤ai≤104), describing the numbers you need to deal with.

Output

A single line containing one integer, denoting the minimum of the sum of the square of sjsj, which is

∑i=jms2j,∑i=jmsj2,

where mm is the number of groups.

Examples

input

4
8 5 2 3

output

164

input

6
1 1 1 2 2 2

output

27

Note

In the first sample, one of the optimal solutions is to divide those 44 numbers into 22 groups {2,8},{5,3}{2,8},{5,3}. Thus the answer is (2+8)2+(5+3)2=164(2+8)2+(5+3)2=164.

In the second sample, one of the optimal solutions is to divide those 66 numbers into 33 groups {1,2},{1,2},{1,2}{1,2},{1,2},{1,2}. Thus the answer is (1+2)2+(1+2)2+(1+2)2=27(1+2)2+(1+2)2+(1+2)2=27.

题意:题中给出偶数个数字,现在让你给他们分组,要求最少都要两个数组一组,然后使每一组的平方和的和最小。

题解:因为后面要进行平方,所以要使分组后每组的和尽量小,所以直接两个一组。要使每组的和尽量小,只有最大的和最小的分到一组这样子,最后平方下来才会整体最小。(这个拿几个数组验算一下就能明白)

#include <iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<map>
#include<queue>
#include<set>
#include<cmath>
#include<stack>
#include<string>
const int maxn=3e5+10;
const int mod=1e9+7;
const int inf=0x3f3f3f3f;
#define me(a,b) memset(a,b,sizeof(a))
#define lowbit(x) x&(-x)
#define mid (l+r)/2
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
int dir[4][2]= {0,1,0,-1,1,0,-1,0};
typedef long long ll;
using namespace std;
int n,a[maxn];
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%d",&a[i]);
    sort(a+1,a+n+1);
    ll sum=0;
    for(int i=n/2,j=n/2+1;i>=0;i--,j++)
        sum+=(a[i]+a[j])*(a[i]+a[j]);
    printf("%lld\n",sum);
    return 0;
}

D. Lunar New Year and a Wander

Lunar New Year is approaching, and Bob decides to take a wander in a nearby park.

The park can be represented as a connected graph with nn nodes and mm bidirectional edges. Initially Bob is at the node 11 and he records 11 on his notebook. He can wander from one node to another through those bidirectional edges. Whenever he visits a node not recorded on his notebook, he records it. After he visits all nodes at least once, he stops wandering, thus finally a permutation of nodes a1,a2,…,ana1,a2,…,an is recorded.

Wandering is a boring thing, but solving problems is fascinating. Bob wants to know the lexicographically smallest sequence of nodes he can record while wandering. Bob thinks this problem is trivial, and he wants you to solve it.

A sequence xx is lexicographically smaller than a sequence yy if and only if one of the following holds:

  • xx is a prefix of yy, but x≠yx≠y (this is impossible in this problem as all considered sequences have the same length);
  • in the first position where xx and yy differ, the sequence xx has a smaller element than the corresponding element in yy.

Input

The first line contains two positive integers nn and mm (1≤n,m≤1051≤n,m≤105), denoting the number of nodes and edges, respectively.

The following mm lines describe the bidirectional edges in the graph. The ii-th of these lines contains two integers uiui and vivi (1≤ui,vi≤n1≤ui,vi≤n), representing the nodes the ii-th edge connects.

Note that the graph can have multiple edges connecting the same two nodes and self-loops. It is guaranteed that the graph is connected.

Output

Output a line containing the lexicographically smallest sequence a1,a2,…,ana1,a2,…,an Bob can record.

Examples

input

3 2
1 2
1 3

output

1 2 3 

input

5 5
1 4
3 4
5 4
3 2
1 5

output

1 4 3 2 5 

input

10 10
1 4
6 8
2 5
3 7
9 4
5 6
3 4
8 10
8 9
1 10

output

1 4 3 7 9 8 6 5 2 10 

Note

In the first sample, Bob's optimal wandering path could be 1→2→1→3. Therefore, Bob will obtain the sequence {1,2,3}{1,2,3}, which is the lexicographically smallest one.

In the second sample, Bob's optimal wandering path could be 1→4→3→2→3→4→1→5. Therefore, Bob will obtain the sequence {1,4,3,2,5}{1,4,3,2,5}, which is the lexicographically smallest one.

题意:现在给你一个图,让你从1节点开始访问,后面每次访问都只能访问之前未访问过且当前能访问到的编号最小的点。

题解:这个题用BFS+优先队列就能很好的解决,细节看代码。(DFS我不知道为什么不行,希望有大佬提示)。

#include <iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<map>
#include<queue>
#include<set>
#include<cmath>
#include<stack>
#include<string>
const int maxn=1e5+10;
const int mod=1e9+7;
const int inf=0x3f3f3f3f;
#define me(a,b) memset(a,b,sizeof(a))
#define lowbit(x) x&(-x)
#define mid (l+r)/2
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
int dir[4][2]= {0,1,0,-1,1,0,-1,0};
typedef long long ll;
using namespace std;
int n,m;
vector<int>a[maxn];
bool vis[maxn];
void solve()
{
    priority_queue<int,vector<int>,greater<int> >q;
    q.push(1);
    vis[1]=1;
    while(!q.empty())
    {
        int now=q.top();
        q.pop();
        printf("%d ",now);
        for(int i=0;i<a[now].size();i++)
            if(!vis[a[now][i]])
            {
                vis[a[now][i]]=1;
                q.push(a[now][i]);
            }
    }

}
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=0; i<m; i++)
    {
        int u,v;
        scanf("%d%d",&u,&v);
        a[u].push_back(v);
        a[v].push_back(u);
    }
    me(vis,0);
    solve();
    printf("\n");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41292370/article/details/86887661