AtCoder Regular Contest 090

C - Candies


Time limit : 2sec / Memory limit : 256MB

Score : 300 points

Problem Statement

We have a 2×N grid. We will denote the square at the i-th row and j-th column (1≤i≤2, 1≤jN) as (i,j).

You are initially in the top-left square, (1,1). You will travel to the bottom-right square, (2,N), by repeatedly moving right or down.

The square (i,j) contains Ai,j candies. You will collect all the candies you visit during the travel. The top-left and bottom-right squares also contain candies, and you will also collect them.

At most how many candies can you collect when you choose the best way to travel?

Constraints

  • 1≤N≤100
  • 1≤Ai,j≤100 (1≤i≤2, 1≤jN)

Input

Input is given from Standard Input in the following format:

N
A1,1 A1,2 … A1,N
A2,1 A2,2 … A2,N

Output

Print the maximum number of candies that can be collected.


Sample Input 1

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

Sample Output 1

Copy
14

The number of collected candies will be maximized when you:

  • move right three times, then move down once, then move right once.

Sample Input 2

Copy
4
1 1 1 1
1 1 1 1

Sample Output 2

Copy
5

You will always collect the same number of candies, regardless of how you travel.


Sample Input 3

Copy
7
3 3 4 5 4 5 3
5 3 4 4 2 3 2

Sample Output 3

Copy
29

Sample Input 4

Copy
1
2
3

Sample Output 4

Copy
5

从左上角到右下角,只能选择向右或者向下,直接dp一下就好了,加上他左边或者上边最大的数即可

#include
using namespace std;
int n,f[3][105];
int main()
{
    scanf("%d",&n);
    for (int i=1; i<=2; i++)
        for (int j=1; j<=n; j++)
            scanf("%d",&f[i][j]),
                  f[i][j]+=max(f[i-1][j],f[i][j-1]);
    printf("%d",f[2][n]);
    return 0;
}

 

D - People on a Line


Time limit : 2sec / Memory limit : 256MB

Score : 400 points

Problem Statement

There are N people standing on the x-axis. Let the coordinate of Person i be xi. For every ixi is an integer between 0 and 109(inclusive). It is possible that more than one person is standing at the same coordinate.

You will given M pieces of information regarding the positions of these people. The i-th piece of information has the form (Li,Ri,Di). This means that Person Ri is to the right of Person Li by Di units of distance, that is, xRixLi=Di holds.

It turns out that some of these M pieces of information may be incorrect. Determine if there exists a set of values (x1,x2,…,xN) that is consistent with the given pieces of information.

Constraints

  • 1≤N≤100 000
  • 0≤M≤200 000
  • 1≤Li,RiN (1≤iM)
  • 0≤Di≤10 000 (1≤iM)
  • LiRi (1≤iM)
  • If ij, then (Li,Ri)≠(Lj,Rj) and (Li,Ri)≠(Rj,Lj).
  • Di are integers.

Input

Input is given from Standard Input in the following format:

N M
L1 R1 D1
L2 R2 D2
:
LM RM DM

Output

If there exists a set of values (x1,x2,…,xN) that is consistent with all given pieces of information, print Yes; if it does not exist, print No.


Sample Input 1

Copy
3 3
1 2 1
2 3 1
1 3 2

Sample Output 1

Copy
Yes

Some possible sets of values (x1,x2,x3) are (0,1,2) and (101,102,103).


Sample Input 2

Copy
3 3
1 2 1
2 3 1
1 3 5

Sample Output 2

Copy
No

If the first two pieces of information are correct, x3−x1=2 holds, which is contradictory to the last piece of information.


Sample Input 3

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

Sample Output 3

Copy
Yes

Sample Input 4

Copy
10 3
8 7 100
7 9 100
9 8 100

Sample Output 4

Copy
No

Sample Input 5

Copy
100 0

Sample Output 5

Copy
Yes

有n个人,给了你m个人的位置关系和距离,问你能不能在一条直线上

距离都是一个方向的,左边

利用并查集的思想,如果两个点的祖先一样,那么他们的距离的差为z,否则就去维护这个距离

因为每次都去维护,所以这个祖先你每次都在检查,所以并查集的思想是可行的

cnt存的是到祖先的距离,fa存的是他的祖先

#include 
using namespace std;
const int N=1e5+5;
int n,m,fa[N],cnt[N];
void find(int x)
{
    if (fa[x]==x) return;
    find(fa[x]);
    cnt[x]+=cnt[fa[x]],fa[x]=fa[fa[x]];
}
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1; i<=n; ++i)
        fa[i]=i;
    for(int i=1,x,y,z; i<=m; ++i)
    {
        scanf("%d%d%d",&x,&y,&z);
        find(x),find(y);
        if(fa[x]==fa[y])
        {
            if(cnt[x]+z!=cnt[y])
            {
                puts("No");
                return 0;
            }
        }
        else
        {
            cnt[fa[y]]=cnt[x]+z-cnt[y];
            fa[fa[y]]=fa[x];
        }
    }
    puts("Yes");
    return 0;
}

 

转载于:https://www.cnblogs.com/BobHuang/p/8376762.html

你可能感兴趣的:(AtCoder Regular Contest 090)