CF Round #423 Div. 2 题解 总结

B - Black Square

  CodeForces - 828B

Polycarp has a checkered sheet of paper of size n × m. Polycarp painted some of cells with black, the others remained white. Inspired by Malevich's "Black Square", Polycarp wants to paint minimum possible number of white cells with black so that all black cells form a square.

You are to determine the minimum possible number of cells needed to be painted black so that the black cells form a black square with sides parallel to the painting's sides. All the cells that do not belong to the square should be white. The square's side should have positive length.

Input

The first line contains two integers n and m (1 ≤ n, m ≤ 100) — the sizes of the sheet.

The next n lines contain m letters 'B' or 'W' each — the description of initial cells' colors. If a letter is 'B', then the corresponding cell is painted black, otherwise it is painted white.

Output

Print the minimum number of cells needed to be painted black so that the black cells form a black square with sides parallel to the painting's sides. All the cells that do not belong to the square should be white. If it is impossible, print -1.

Examples
Input
5 4
WWWW
WWWB
WWWB
WWBB
WWWW
Output
5
Input
1 2
BB
Output
-1
Input
3 3
WWW
WWW
WWW
Output
1
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
char a[105][105];
int main()
{
    int n, m;
    while (cin >> n >> m)
    {
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                cin>>a[i][j];
            }
        }
        int minx = 99999, miny = 99999, maxx = -99999, maxy = -99999;
        int flag = 0;
        for(int i=0;i<n;i++)
        {
            for (int j = 0; j < m; j++)
            {
                if (a[i][j] == 'B')
                {
                    flag++;
                    if (i < minx) minx = i;
                    if (i > maxx) maxx = i;
                    if (j < miny) miny = j;
                    if (j > maxy) maxy = j;
                }
            }
        }//找到在所有黑色的点里xy坐标的最小最大值,比较,得到可以包含所有黑色点的最小正方形
        int xx = maxx - minx+1;
        int xy = maxy - miny+1;

        int s = max(xx, xy);//取大才能全包含进去。。
        int sum = 0;
        if (flag==0)
        {
            sum = 1;//没有黑色的点,随便涂一个就ok
        }
        else if (s > n || s > m)
        {
            sum = -1;
        }
        else
        {
            sum = s*s-flag;
        }
        cout << sum << endl;;
    }
    return 0;
}



C - String Reconstruction

  CodeForces - 828C 

Ivan had string s consisting of small English letters. However, his friend Julia decided to make fun of him and hid the string s. Ivan preferred making a new string to finding the old one. 

Ivan knows some information about the string s. Namely, he remembers, that string ti occurs in string s at least ki times or more, he also remembers exactly ki positions where the string ti occurs in string s: these positions are xi, 1, xi, 2, ..., xi, ki. He remembers n such strings ti.

You are to reconstruct lexicographically minimal string s such that it fits all the information Ivan remembers. Strings ti and string s consist of small English letters only.

Input

The first line contains single integer n (1 ≤ n ≤ 105) — the number of strings Ivan remembers.

The next n lines contain information about the strings. The i-th of these lines contains non-empty string ti, then positive integer ki, which equal to the number of times the string ti occurs in string s, and then ki distinct positive integers xi, 1, xi, 2, ..., xi, ki in increasing order — positions, in which occurrences of the string ti in the string s start. It is guaranteed that the sum of lengths of strings ti doesn't exceed 1061 ≤ xi, j ≤ 1061 ≤ ki ≤ 106, and the sum of all ki doesn't exceed 106. The strings tican coincide.

It is guaranteed that the input data is not self-contradictory, and thus at least one answer always exists.

Output

Print lexicographically minimal string that fits all the information Ivan remembers. 

Examples
Input
3
a 4 1 3 5 7
ab 2 1 5
ca 1 4
Output
abacaba
Input
1
a 1 3
Output
aaa
Input
3
ab 1 1
aba 1 3
ab 2 3 5
Output
ababab



数据太大,暴力做会有很多重复,妥妥超时。因为题目讲填充的过程中不会前后矛盾,也就是说一个位置只要填充过一次,就一定ok,不需要再重复填充第二次

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=1000005;
string res="";
string st;
struct node
{
    int pos;
    int id;
}a[maxn];
string str[100005];

bool cmp(node a,node b)
{
    return a.pos<b.pos;
}

int main()
{
    ios::sync_with_stdio(false);//不加这句话的结果就是超时。。  加了就800ms  输入的数据大的时候记得顺手加上
    int p;
    int cnt=0;
    int num;
    int n;
    cin>>n;

    for(int i=0;i<n;i++)
    {
        cin>>str[i] >>num;
        while(num--)
        {
            cin>>p;
            a[cnt].id=i;//输入的第i个字符串
            a[cnt].pos=p;
            cnt++;
        }
    }

    sort(a,a+cnt,cmp);
    int i=0;
    int now=1;

    for(;i<cnt;i++)
    {
        while(now<a[i].pos)
        {
            res+='a';
            now++;
        }
        int len=str[a[i].id].size();
        //cout<<a[i].id<<' ';
        //cout<<len<<endl;
        if(now>=a[i].pos&&now<=a[i].pos+len-1)//now在a[i].str的(pos,pos+len-1)的区间内
        {
            int time=a[i].pos+len-now;//需要填充多少次
            //cout<<time<<endl;
            int temp=now-a[i].pos;//now在str数组中的相对位置
            while(time--)
            {
                res+=str[a[i].id][temp++];
                now++;
            }
        }
    }
    cout<<res<<endl;



    return 0;
}


D - High Load

  CodeForces - 828D

Arkady needs your help again! This time he decided to build his own high-speed Internet exchange point. It should consist of n nodes connected with minimum possible number of wires into one network (a wire directly connects two nodes). Exactly k of the nodes should be exit-nodes, that means that each of them should be connected to exactly one other node of the network, while all other nodes should be connected to at least two nodes in order to increase the system stability.

Arkady wants to make the system as fast as possible, so he wants to minimize the maximum distance between two exit-nodes. The distance between two nodes is the number of wires a package needs to go through between those two nodes.

Help Arkady to find such a way to build the network that the distance between the two most distant exit-nodes is as small as possible.

Input

The first line contains two integers n and k (3 ≤ n ≤ 2·1052 ≤ k ≤ n - 1) — the total number of nodes and the number of exit-nodes.

Note that it is always possible to build at least one network with n nodes and k exit-nodes within the given constraints.

Output

In the first line print the minimum possible distance between the two most distant exit-nodes. In each of the next n - 1 lines print two integers: the ids of the nodes connected by a wire. The description of each wire should be printed exactly once. You can print wires and wires' ends in arbitrary order. The nodes should be numbered from 1 to n. Exit-nodes can have any ids.

If there are multiple answers, print any of them.

Examples
Input
3 2
Output
2
1 2
2 3
Input
5 3
Output
3
1 2
2 3
3 4
3 5
Note

In the first example the only network is shown on the left picture.

In the second example one of optimal networks is shown on the right picture.

Exit-nodes are highlighted.


思维题。。一共有n个点,n-1条连线,其中有k个点只有一个点与它连接,这样的点称为exit点,求两个exit点最长距离的最小值。

思路:构造一个点为图形的中心(1号点),以这个点为心,向外辐射出k条支路,这k条支路的最远点就是exit点,这样构造出的exit点的最大距离是最小的,因为这个图形各条支路是对称的。

依照这个思路,分为3种情况:

第一种,各个支路一样,这样任取两个exit点的距离是相同的。

 第二种,有一条支路冒尖,比其他的支路长1,取这条支路上的exit点和其他任取一个。

第三种,有两个及以上的支路比其他支路长1,任取2个冒尖的支路上的exit点。

最后是打印路径。。螺旋向外标记打印,先把第一圈打印出来,后面的顺序就是t连接t+k这样的顺序了。

#include <iostream>
using namespace std;
int main()
{
    int tot;
    int len;
    int n,k;//k也为支路的条数
    cin>>n>>k;
    if((n-1)%k==0)//每条分支一样长
    {
        len=2*(n-1)/k;
    }
    else if((n-1)%k==1)//有一条分支比其他的分支长1
    {
        len=2*((n-1)/k)+1;
    }
    else//最长的距离一定是两条最长的支路之和
    {
        len=2*((n-1)/k)+2;
    }
    cout<<len<<endl;

    //打印路径
    tot=n-1;
    for(int i=2;i<=k+1;i++)
    {
        cout<<1<<' '<<i<<endl;
    }//第一圈

    for(int i=k+2;i<=tot+1;i++)
    {
        cout<<i-k<<' '<<i<<endl;//螺旋标记,每条支路的前一个和后一个相差支路的条数即k
    }

    return 0;
}







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值