【比赛重练】◆AtCoder◆ Regular Contest 093

◆AtCoder◆ Regular Contest 093


□微目录□

C- Traveling Plan
D- Grid Components
Tip:后续会继续补充剩余的E、F题  =)   = )


□题外话□

自发的打开了AtCoder,翻了一翻之前的比赛。突然发现以前做的比赛都是Beginner,受不了自己,所以一气之下就把Regular刷了一发。


□题目□

C- Traveling Plan -(AtCoder Traveling Plan)

■原题■

Time limit : 2sec / Memory limit : 256MB
Score : 300 points

Problem Statement
There are N sightseeing spots on the x-axis, numbered 1,2,…,N. Spot i is at the point with coordinate Ai. It costs |a−b| yen (the currency of Japan) to travel from a point with coordinate a to another point with coordinate b along the axis.

You planned a trip along the axis. In this plan, you first depart from the point with coordinate 0, then visit the N spots in the order they are numbered, and finally return to the point with coordinate 0.

However, something came up just before the trip, and you no longer have enough time to visit all the N spots, so you decided to choose some i and cancel the visit to Spot i. You will visit the remaining spots as planned in the order they are numbered. You will also depart from and return to the point with coordinate 0 at the beginning and the end, as planned.

For each i=1,2,…,N, find the total cost of travel during the trip when the visit to Spot i is canceled.

Constraints
2≤N≤105
−5000≤Ai≤5000 (1≤i≤N)
All input values are integers.
Input
Input is given from Standard Input in the following format:

N
A1 A2 … AN

Output
Print N lines. In the i-th line, print the total cost of travel during the trip when the visit to Spot i is canceled.

Samples

Sample 1 Sample 2 Sample 3
Input 33 5 1 3 3   5   − 1 51 1 1 2 0 5 1   1   1   2   0 6679 2409 3258 3095 3291 4462 6 − 679   − 2409   − 3258   3095   − 3291   − 4462
Output 12810 12 8 10 44424 4 4 4 2 4 21630216301993289242163019288 21630 21630 19932 8924 21630 19288

Sample 1 Hint:
Spot 1, 2 and 3 are at the points with coordinates 3, 5 and −1, respectively. For each i, the course of the trip and the total cost of travel when the visit to Spot i is canceled, are as follows:
For i=1, the course of the trip is 0→5→−1→0 and the total cost of travel is 5+6+1=12 yen.
For i=2, the course of the trip is 0→3→−1→0 and the total cost of travel is 3+4+1=8 yen.
For i=3, the course of the trip is 0→3→5→0 and the total cost of travel is 3+2+5=10 yen.

■翻译■

在x轴有n个景点,给出每个景点的坐标 posi p o s i ,从两点i到j的花费为 |ij| | i − j | 。一开始你在坐标0,你将按1~n的顺序浏览每一个景点,最后回到0。但是现在时间不够了,你必须放弃参观一个景点,输出n行,第i行的一个数字表示放弃参观i景点所需要的花费(Tab:仍然保持1~n的顺序参观)。

可能不太准确,请原谅ヽ( ̄▽ ̄)ノ

■解析■

这道题n最大是105,暂且看作100。显然直接模拟的话 O(n4) O ( n 4 ) 是承受不起的,所以我们需要一些简单的结论。
我们人为地设 pos0=posn+1=0 p o s 0 = p o s n + 1 = 0 ,也就是起点、终点。显然每种情况的花费和游览所有景点的总花费是有联系的,所以我们先求出总花费 tot t o t 。如果把“不去景点k”看作“删去节点k”,则这样的操作一定只影响景点k前后两个景点,下面来看一看这些结论,设当前不去景点k( 1kn 1 ≤ k ≤ n ):
①当 posk1poskposk+1 p o s k − 1 ≤ p o s k ≤ p o s k + 1 ,如图
【比赛重练】◆AtCoder◆ Regular Contest 093_第1张图片
如果我们将它用删点连边表示出来,就是—— tot=tot|poskposk1||poskposk+1|+|posk1posk+1| t o t ′ = t o t − | p o s k − p o s k − 1 | − | p o s k − p o s k + 1 | + | p o s k − 1 − p o s k + 1 | ,也就是删去 k-1~k 、 k~k+1;然后连上 k-1~k+1。
②当 posk1>poskposk+1>posk p o s k − 1 > p o s k ⊙ p o s k + 1 > p o s k ,如图:
【比赛重练】◆AtCoder◆ Regular Contest 093_第2张图片
我们就没有必要再向景点k的方向走然后再返回景点k+1了,而是在景点k-1直接掉头。按照之前的方法,我们再来计算一下—— tot=tot|poskposk1||poskposk+1|+|posk1posk+1| t o t ′ = t o t − | p o s k − p o s k − 1 | − | p o s k − p o s k + 1 | + | p o s k − 1 − p o s k + 1 | 。没有一点区别!
所以……直接算不就行啦(〃’▽’〃)

■源代码■

/*Lucky_Glass*/
#include
#include
#include
using namespace std;
#define MAXN int(1e5)
int n,tot;
int poi[MAXN+5];
int main()
{
    //freopen("in.txt","r",stdin);
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%d",&poi[i]),tot+=abs(poi[i-1]-poi[i]);
    tot+=abs(poi[n]);
    for(int i=1;i<=n;i++)
    {
        int ans=tot;
        if(i==n)
            ans=ans-abs(poi[n])-abs(poi[n-1]-poi[n])+abs(poi[n-1]);
        else
            ans=ans-abs(poi[i-1]-poi[i])-abs(poi[i]-poi[i+1])+abs(poi[i-1]-poi[i+1]);
        printf("%d\n",ans);
    }
    return 0;
}

D- Grid Components -(AtCoder Grid Components)

■原题■

Time limit : 2sec / Memory limit : 256MB
Score : 500 points

Problem Statement
You are given two integers A and B.
Print a grid where each square is painted white or black that satisfies the following conditions, in the format specified in Output section:

  • Let the size of the grid be h×w (h vertical, w horizontal). Both h and w are at most 100.
  • The set of the squares painted white is divided into exactly A connected components.
  • The set of the squares painted black is divided into exactly B connected components.

It can be proved that there always exist one or more solutions under the conditions specified in Constraints section. If there are multiple solutions, any of them may be printed.

Notes
Two squares painted white, c1 and c2, are called connected when the square c2 can be reached from the square c1 passing only white squares by repeatedly moving up, down, left or right to an adjacent square.
A set of squares painted white, S, forms a connected component when the following conditions are met:

  • Any two squares in S are connected.
  • No pair of a square painted white that is not included in S and a square included in S is connected.

A connected component of squares painted black is defined similarly.
Constraints
1A5001B500 1 ≤ A ≤ 500 1 ≤ B ≤ 500

Input
Input is given from Standard Input in the following format:

A B

Output
Output should be in the following format:

  • In the first line, print integers h and w representing the size of the grid you constructed, with a space in between.
  • Then, print h more lines. The i-th (1≤i≤h) of these lines should contain a string si as follows:
    • If the square at the i-th row and j-th column (1≤j≤w) in the grid is painted white, the j-th character in si should be ..
    • If the square at the i-th row and j-th column (1≤j≤w) in the grid is painted black, the j-th character in si should be #.

Sample

Sample 1 Sample 2 Sample 3 Sample 4
Input 2 3 2   3 7 8 7   8 1 1 1   1 3 14 3   14
Output 3 3##...##.# 3   3 # # . . . # # . # 3 5#.#.#.#.#.#.#.# 3   5 # . # . # . # . # . # . # . # 4 2..#.#### 4   2 . . # . # # # # 8 18........................................##.......####.....#.#.....#........#...#....#.......#.###.#...#......#.......#..#.....#.........#..####. 8   18 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . # # . . . . . . . # # # # . . . . . # . # . . . . . # . . . . . . . . # . . . # . . . . # . . . . . . . # . # # # . # . . . # . . . . . . # . . . . . . . # . . # . . . . . # . . . . . . . . . # . . # # # # .

(Tab:最后那个图是一个”AC”,大家可以复制下来看一看:) )
This Sample output 1 corresponds to the grid below:

这个图是作者配的QwQ
【比赛重练】◆AtCoder◆ Regular Contest 093_第3张图片

■翻译■

求一个边长均小于等于100的矩阵,涂成黑白两色,使得白色连通块和黑色连通块的数量分别是A、B(四联通)。输出这个矩阵的长宽,并输出这个矩阵的具体涂色方案(’.’是白色,’#’是黑色)。

■解析■

这道题其实不算太难……我们要理解一点,我们输出的图案一定是有规律的——也就是说不能太,像样例4那样输出“AC” :) 。其实这些样例里面最正常的就是样例2了——以一个单独的色块为一个连通块是最简单也是最容易想到的。
其次我们需要知道:如果有 mn<pq m n < p q ,则用m*n能够完成的解一定可以用p*q的矩形完成(eg:3*3的矩形能够完成问题”2 3”,则3*4的矩形一定也可以)。所以100*100的矩形一定可以完成所有的数据。
看到样例2,我们可以把它看成一开始上下分为黑白两块,然后在上面的黑色块中挖空填成白色,在下面的白色块中挖空填成黑色:
【比赛重练】◆AtCoder◆ Regular Contest 093_第4张图片
所以我们先定答案为一个100*100的正方形,并把上50列和下50列分别初始化为白色、黑色(反过来也可以)。然后现在就有一个黑色连通块和一个白色连通块了。剩下我们就要挖(A-1)个白色空和(B-1)个黑色空了。
但是样例2不一样的是它挖白色空的时候同时也增加了黑色连通块的数量,这样并不便于我们计数,所以我们需要保证我们不会在一个八连通的范围内挖两个空,所以最后挖出来应该是这样:
【比赛重练】◆AtCoder◆ Regular Contest 093_第5张图片
所以我们就可以愉快地模拟了!

■源代码■

/*Lucky_Glass*/
#include
#include
#include
using namespace std;
bool sqr[105][105];
int main()
{
    //freopen("out.txt","w",stdout);
    int A,B;scanf("%d%d",&A,&B);
    A--;B--;
    for(int i=0;i<100;i++)
        for(int j=0;j<100;j++)
            sqr[i][j]=i<50;
    int x,y;
    x=0,y=0;
    //white false/black true
    while(A--)
    {
        sqr[x][y]=false;
        y+=2;
        if(y>=100) x+=2,y=0;
    }
    x=99;y=0;
    while(B--)
    {
        sqr[x][y]=true;
        y+=2;
        if(y>=100) x-=2,y=0;
    }
    printf("100 100\n");
    for(int i=0;i<100;i++)
    {
        if(i) puts("");
        for(int j=0;j<100;j++)
            printf("%c",sqr[i][j]? '#':'.');
    }
    return 0;
}

你可能感兴趣的:(#模拟算法,-,水题的浪潮#,#数学推理,-,拿出你数学学霸的风度#,#枚举算法,-,可能要优化QWQ#)