AtCoder Beginner Contest 051 ABCD题

A - Haiku


Time limit : 2sec / Memory limit : 256MB

Score : 100 points

Problem Statement

As a New Year's gift, Dolphin received a string s of length 19.
The string s has the following format: [five lowercase English letters],[seven lowercase English letters],[five lowercase English letters].
Dolphin wants to convert the comma-separated string s into a space-separated string.
Write a program to perform the conversion for him.

Constraints

  • The length of s is 19.
  • The sixth and fourteenth characters in s are ,.
  • The other characters in s are lowercase English letters.

Input

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

s

Output

Print the string after the conversion.


Sample Input 1

Copy
happy,newyear,enjoy

Sample Output 1

Copy
happy newyear enjoy

Replace all the commas in happy,newyear,enjoy with spaces to obtain happy newyear enjoy.


Sample Input 2

Copy
haiku,atcoder,tasks

Sample Output 2

Copy
haiku atcoder tasks

Sample Input 3

Copy
abcde,fghihgf,edcba

Sample Output 3

Copy
abcde fghihgf edcba

题意:根据样列猜题意
解法:模拟
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int main()
 4 {
 5     string s;
 6     cin>>s;
 7     for(int i=0;i<s.length();i++)
 8     {
 9         if(s[i]!=',')
10         {
11             cout<<s[i];
12         }
13         else
14         {
15             cout<<" ";
16         }
17     }
18 }

B - Sum of Three Integers


Time limit : 2sec / Memory limit : 256MB

Score : 200 points

Problem Statement

You are given two integers K and S.
Three variable X,Y and Z takes integer values satisfying 0≤X,Y,ZK.
How many different assignments of values to X,Y and Z are there such that X+Y+Z=S?

Constraints

  • 2≤K≤2500
  • 0≤S≤3K
  • K and S are integers.

Input

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

K S

Output

Print the number of the triples of X,Y and Z that satisfy the condition.


Sample Input 1

Copy
2 2

Sample Output 1

Copy
6

There are six triples of X,Y and Z that satisfy the condition:

  • X=0,Y=0,Z=2
  • X=0,Y=2,Z=0
  • X=2,Y=0,Z=0
  • X=0,Y=1,Z=1
  • X=1,Y=0,Z=1
  • X=1,Y=1,Z=0

Sample Input 2

Copy
5 15

Sample Output 2

Copy
1

The maximum value of X+Y+Z is 15, achieved by one triple of X,Y and Z.

题意:看样列猜题意

解法:乍一看感觉是暴力,但不怎么妥当,我这里采用数组保存的方式 500+x+y==s-z记录符合要求的个数

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int a[1000000];
 4 int main()
 5 {
 6     int k,s;
 7     cin>>k>>s;
 8     for(int i=0;i<=k;i++)
 9     {
10         for(int j=0;j<=k;j++)
11         {
12             a[5000+i+j]++;
13         }
14     }
15     int sum=0;
16     for(int i=0;i<=k;i++)
17     {
18         int pos=s-i;
19         if(pos<0)
20         {
21             continue;
22         }
23         sum+=a[5000+pos];
24     }
25     cout<<sum<<endl;
26     return 0;
27 }

C - Back and Forth


Time limit : 2sec / Memory limit : 256MB

Score : 300 points

Problem Statement

Dolphin resides in two-dimensional Cartesian plane, with the positive x-axis pointing right and the positive y-axis pointing up.
Currently, he is located at the point (sx,sy). In each second, he can move up, down, left or right by a distance of 1.
Here, both the x- and y-coordinates before and after each movement must be integers.
He will first visit the point (tx,ty) where sx<tx and sy<ty, then go back to the point (sx,sy), then visit the point (tx,ty) again, and lastly go back to the point(sx,sy).
Here, during the whole travel, he is not allowed to pass through the same point more than once, except the points (sx,sy) and (tx,ty).
Under this condition, find a shortest path for him.

Constraints

  • −1000≤sx<tx≤1000
  • −1000≤sy<ty≤1000
  • sx,sy,tx and ty are integers.

Input

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

sx sy tx ty

Output

Print a string S that represents a shortest path for Dolphin.
The i-th character in S should correspond to his i-th movement.
The directions of the movements should be indicated by the following characters:

  • U: Up
  • D: Down
  • L: Left
  • R: Right

If there exist multiple shortest paths under the condition, print any of them.


Sample Input 1

Copy
0 0 1 2

Sample Output 1

Copy
UURDDLLUUURRDRDDDLLU

One possible shortest path is:

  • Going from (sx,sy) to (tx,ty) for the first time: (0,0) → (0,1) → (0,2) → (1,2)
  • Going from (tx,ty) to (sx,sy) for the first time: (1,2) → (1,1) → (1,0) → (0,0)
  • Going from (sx,sy) to (tx,ty) for the second time: (0,0) → (−1,0) → (−1,1) → (−1,2) → (−1,3) → (0,3) → (1,3) → (1,2)
  • Going from (tx,ty) to (sx,sy) for the second time: (1,2) → (2,2) → (2,1) → (2,0) → (2,−1) → (1,−1) → (0,−1) → (0,0)

Sample Input 2

Copy
-2 -2 1 1

Sample Output 2

Copy
UURRURRDDDLLDLLULUUURRURRDDDLLDL
题意:看样列猜题意
解法:根据提示进行模拟
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int a[100000];
 4 int main()
 5 {
 6     int sx, sy, tx, ty;
 7     cin >> sx >> sy >> tx >> ty;
 8     for(int i=1;i<=ty-sy;i++)
 9     {
10         cout<<"U";
11     }
12     for(int i=1;i<=tx-sx;i++)
13     {
14         cout<<"R";
15     }
16     for(int i=1;i<=ty-sy;i++)
17     {
18         cout<<"D";
19     }
20     for(int i=1;i<=tx-sx;i++)
21     {
22         cout<<"L";
23     }
24     cout<<"L";
25     for(int i=0;i<=ty-sy;i++)
26     {
27         cout<<"U";
28     }
29     cout<<"R";
30     for(int i=1;i<=tx-sx;i++)
31     {
32         cout<<"R";
33     }
34     cout<<"D";
35     cout<<"R";
36     for(int i=0;i<=ty-sy;i++)
37     {
38         cout<<"D";
39     }
40     cout<<"L";
41     for(int i=1;i<=tx-sx;i++)
42     {
43         cout<<"L";
44     }
45     cout<<"U";
46     return 0;
47 }

D - Candidates of No Shortest Paths


Time limit : 2sec / Memory limit : 256MB

Score : 400 points

Problem Statement

You are given an undirected connected weighted graph with N vertices and M edges that contains neither self-loops nor double edges.
The i-th (1≤iM) edge connects vertex ai and vertex bi with a distance of ci.
Here, a self-loop is an edge where ai=bi(1≤iM), and double edges are two edges where (ai,bi)=(aj,bj) or (ai,bi)=(bj,aj)(1≤i<jM).
connected graph is a graph where there is a path between every pair of different vertices.
Find the number of the edges that are not contained in any shortest path between any pair of different vertices.

Constraints

  • 2≤N≤100
  • N−1≤Mmin(N(N−1)⁄2,1000)
  • 1≤ai,biN
  • 1≤ci≤1000
  • ci is an integer.
  • The given graph contains neither self-loops nor double edges.
  • The given graph is connected.

Input

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

N M  
a1 b1 c1  
a2 b2 c2
:  
aM bM cM  

Output

Print the number of the edges in the graph that are not contained in any shortest path between any pair of different vertices.


Sample Input 1

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

Sample Output 1

Copy
1

In the given graph, the shortest paths between all pairs of different vertices are as follows:

  • The shortest path from vertex 1 to vertex 2 is: vertex 1 → vertex 2, with the length of 1.
  • The shortest path from vertex 1 to vertex 3 is: vertex 1 → vertex 3, with the length of 1.
  • The shortest path from vertex 2 to vertex 1 is: vertex 2 → vertex 1, with the length of 1.
  • The shortest path from vertex 2 to vertex 3 is: vertex 2 → vertex 1 → vertex 3, with the length of 2.
  • The shortest path from vertex 3 to vertex 1 is: vertex 3 → vertex 1, with the length of 1.
  • The shortest path from vertex 3 to vertex 2 is: vertex 3 → vertex 1 → vertex 2, with the length of 2.

Thus, the only edge that is not contained in any shortest path, is the edge of length 3 connecting vertex 2 and vertex 3, hence the output should be 1.


Sample Input 2

Copy
3 2
1 2 1
2 3 1

Sample Output 2

Copy
0

Every edge is contained in some shortest path between some pair of different vertices.

题意:让我们找出不含最短路径的道路有多少条

解法:先Floyd跑一次,然后比较原始道路和现在的道路,距离不同就说明最短路径也不经过这条道路,然后加一

#include<bits/stdc++.h>
using namespace std;
long long a[10000][10000];
long long b[10000][10000];
int inf=(1<<31)-1;
int main()
{
    int n,m;
    cin>>n>>m;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            a[i][j]=inf;
            b[i][j]=inf;
        }
        b[i][i]=0;
        a[i][i]=0;
    }
    for(int i=1;i<=m;i++)
    {
        long long x,y,z;
        cin>>x>>y>>z;
        a[x][y]=min(z,a[x][y]);
        a[y][x]=min(z,a[y][x]);
        b[x][y]=min(z,b[x][y]);
        b[y][x]=min(z,b[y][x]);
    }
    for(int k=1;k<=n;k++)
    {
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                a[i][j]=min(a[i][k]+a[k][j],a[i][j]);
              //  cout<<a[i][j]<<" "<<i<<" "<<j<<" "<<k<<endl;
            }
        }
    }
    int sum=0;
    for(int i=1;i<=n;i++)
    {
        for(int j=1+i;j<=n;j++)
        {
            if(b[i][j]!=inf&&a[i][j]!=b[i][j])
            {
                sum++;
                //cout<<i<<" "<<j<<endl;
               // cout<<a[i][j]<<" "<<b[i][j]<<endl;
            }
        }
    }
    cout<<sum<<endl;
   // cout<<b[1][2]<<endl;
    return 0;
}


原文地址:https://www.cnblogs.com/yinghualuowu/p/6263061.html