zoukankan      html  css  js  c++  java
  • 2018年第四阶段组队训练赛第五场

    A: Smooth Sequences

    题目描述

    A sequence of n integers is called smooth, if the absolute values of the differences between successive numbers are at most d. We call a non-smooth sequence semi-smooth, if we can  change exactly one number in the sequence to make it smooth. For example, let n = 4 and d = 5. The sequence 21, 20, 18, 20 is smooth, since each pair of adjacent numbers has a gap smaller than 5. The sequence 21, -8, 20, 20 is semi-smooth, since we can replace -8 with 20 to make it smooth. Write a program to test if a sequence of integers is smooth or semi-smooth.

    输入

    An instance of the problem consists of the size n of the sequence, and the gap d in a line.
    They are positive integers in the range [1, 1000]. Then n integers, separated with space(s),follow in the next line.
    Note that the test data file may contain more than one instance. The last instance is followed by a line containing a single 0.

    输出

    For each instance, output “Y”, if the sequence is smooth or semi-smooth in a separate line.Otherwise, output “N”.

    样例输入

    3 2
    2 4 6
    3 1
    3 5 7
    0
    

    样例输出

    Y
    N
    

    提示

    1.All integers in the sequence have absolute values bounded by 220.
    2.1≤n,d≤1000.

    #include<bits/stdc++.h>
     
    using namespace std;
    typedef long long ll;
    const int maxn = 1e5+5;
    int main()
    {
        int n,d;
        while(1)
        {
            scanf("%d",&n);
            if(n==0)
                break;
            scanf("%d",&d);
            int a[maxn];
            int cnt = 0;
            scanf("%d",&a[0]);
            int b[maxn]={0};
            for(int i=1;i<n;i++)
            {
                scanf("%d",&a[i]);
                if(abs(a[i]-a[i-1])>d)
                {
                    b[cnt++] = i;
                }
            }
     
            if(cnt>=3)
            {
                printf("N
    ");
                continue;
            }
            else if(cnt==2)
            {
                if(b[0]+1==b[1])
                {
                    int t = b[1];
                    if(a[t]-a[t-2]<=2*d)
                    {
                        printf("Y
    ");
                        continue;
                    }
                    else
                    {
                        printf("N
    ");
                        continue;
                    }
                }
                else
                {
                    printf("N
    ");
                }
            }
            else if(cnt==1)
            {
                if(b[0]==0||b[0]==n-1)
                {
                    printf("Y
    ");
                    continue;
                }
                else
                {
                    int t = b[0];
                    if(abs(a[t]-a[t-2])<=2*d||(a[t+2]-a[t])<=2*d)
                    {
                        cout<<"Y
    ";
                        continue;
                    }
                    else
                    {
                        printf("N
    ");
                        continue;
                    }
                }
            }
            else
            {
                printf("Y
    ");
                continue;
            }
        }
        return 0;
    }

    思维题

    ——————————————分割线————————————————

     B: Threesome

    题目描述

    There is a student association S formed by n college students and every student has a unique ID that is a positive integer. For convenience, we use {1, 2, . . . , n} to represent the n students. Any two students i and j have a social connectivity, denoted by c(i, j), between i and j, where 1 ≤ c(i, j) ≤ 10000. Three students i, j, k are best threesome if the sum of their mutual social connectivities c(i, j) + c(j, k) + c(i, k) is maximum among all the threesomes,i.e., .
    Your task is to write a computer program to compute the sum of their mutual social connectivities of a best threesome.

    输入

    The first line of the input contains an integer, denoting the number of test cases to follow.
    For each test case, the association S = {a1 , a2 ,..., an } is given with the following format:
    The first line contains a positive integer n. In the following n(n−1)/2 lines, each line contains three integers representing i, j, and c(i, j) such that any two integers are separated by a space.

    输出

    For each test case, output the sum of their mutual social connectivities of best threesome.

    样例输入

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

    样例输出

    3
    6
    

    提示

    • 3 ≤ n ≤ 300
    • Each ID is at least 1 and at most n.
    • For any two students i and j, 1 ≤ c(i, j) ≤ 10000.

     
    #include<bits/stdc++.h>
     
    using namespace std;
    typedef long long ll;
    int ans[320][320];
    int n;
    void init()
    {
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=n; j++)
            {
                ans[i][j]=0;
            }
        }
    }
    int main()
    {
        int t,a,b,c;
        scanf("%d",&t);
        while(t--)
        {
            scanf("%d",&n);
            for(int i=1; i<=n*(n-1)/2; i++)
            {
                scanf("%d %d %d",&a,&b,&c);
                ans[a][b]=c;
                ans[b][a]=c;
            }
            int maxx=0;
            for(int i=1; i<=n; i++)
            {
                for(int j=1; j<=n; j++)
                {
                    for(int k=1; k<=n; k++)
                    {
                        if(i!=j&&i!=k&&j!=k)
                            maxx=max(maxx,ans[i][j]+ans[j][k]+ans[i][k]);
                    }
                }
            }
            printf("%d
    ",maxx);
            init();
        }
        return 0;
    }
     

     ——————————————分割线————————————————

    C: Coefficient Computation

    题目描述

    In mathematics, any of the positive integers that occurs as a coefficient in the binomial theorem is a binomial coefficient. A binomial coefficient is commonly indexed by a pair of integers n ≥ k ≥ 0 and is denoted by C(n, k). It is the coefficient of the xk term in the polynomial expansion of the binomial power (1+x)n , which is equal to
    n!/(k!(n−k)!). Furthermore,in combinatorics, it is the number of ways to choose a subset of k items from a set of n items given that the order of selection does not matter. There are several ways to compute C(n, k). 
    The above formula is factorial formula. And the following formula is the recursive formula,
    Now, give three integers n, k and d with n ≥ k ≥ 0 and 1 < d < 10. Please write a program to compute C(n, k) and output the result in base d. That is, the result value should be converted from base 10 to base d.

    输入

    The first line of the input contains an integer indicating the number of test cases to follow. Each test case contains three integers n, k and d, separated by space on a single line.

    输出

    For each test case, output C(n, k) in base d in a line.

    样例输入

    3
    20 10 3
    150 50 8
    10 2 7
    

    样例输出

    100101102211
    354470761204215171415303750634427342737334454
    63
    

    提示

    1.The number of test cases is no larger than 10.
    2.n is an integer and 0 ≤ n ≤ 300.
    3.k is an integer and 0 ≤ k ≤ n.
    4.d is an integer and 1 < d < 10.

     
    import java.util.*;
    import java.math.BigInteger;
    public class Main {
     
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Scanner cin = new Scanner(System.in);
            int t = cin.nextInt();
            BigInteger ans,tmp,p;
            int n,k,d;
            String str;
            while (t-- != 0) {
                tmp=BigInteger.valueOf(1);
                ans=BigInteger.valueOf(1);
                n=cin.nextInt();
                k=cin.nextInt();
                d=cin.nextInt();
                if(k==0||k==n)
                {
                    ans=BigInteger.valueOf(1);
                }
                else if(k==1)
                {
                    ans=BigInteger.valueOf(n);
                }
                else
                {
                    for(int i=n;i>=n-k+1;i--)
                    {
                        p=BigInteger.valueOf(i);
                        ans=ans.multiply(p);
                    }
                    for(int i=1;i<=k;i++) {
                        p=BigInteger.valueOf(i);
                        tmp=tmp.multiply(p);
                         
                    }
                    ans=ans.divide(tmp);
                     
                }
                str=ans.toString(d);
                System.out.println(str);
                 
            }
        }
     
    }

     队友写的java 我并不会

    ——————————————分割线————————————————

    D: Network Report

    For time efficiency, data transmission is often done along the shortest path (with minimum number of hops) between network nodes. In the network graph (undirected and unweighted) below, the length of the shortest path from node 0 to node 2 is 2, and the length of the shortest path from node 0 to node 4 is 3. For a network graph with n nodes, we want to find all-pairs shortest paths, and provide a summary report on how many shortest paths are there for each length (from 1 to the maximal length). That is, this summary report should contain the total number of shortest paths of length 1, the total number of shortest paths of length 2, and so on and so forth.
    Note that if multiple shortest paths from node i to node j (i ≠ j) exist, they should collectively count as one. For example, though there are two shortest paths from node 0 to node 4, only one path is counted. Please write a program to output the above summary report from an input connected graph.

    输入

    The input contains multiple instances of an undirected and unweighted connected graph (not multigraph). For each instance, the first line contains a single integer n. The second line contains a single integer e, denoting the number of edges in the graph. For the next e lines, each line contains two integers i, j, denoting that there is an edge connecting node i and node j. There is a 0 on a single line following the last test instance. There are at most 10 test instances.

    输出

    For each instance, there are several lines of output pending on the input. Each line of output consists of two integer numbers separated by a single space, where the first number is the path length and the second number is the total number of shortest paths of the corresponding length. The output should display the results from length 1 to the maximal length in the graph incrementally.

    样例输入

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

    样例输出

    1 6
    1 10
    2 8
    3 2
    

    提示

    1. 2 ≤ n ≤ 200, nodes are numbered as 0, 1, 2, . . . , n − 1.
    2. Any counting number is within the range of integer.

    floyd 最短路

    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    const int INF=1e7;
    int dis[220][220];
    int ans[220];
    void init(int n)
    {
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<n; j++)
            {
                dis[i][j]=INF;
            }
            dis[i][i]=0;
        }
        memset(ans,0,sizeof(ans));
    }
    int main()
    {
        int n,m;
        while(1)
        {
            scanf("%d",&n);
            if(n==0)
            {
                break;
            }
            init(n);
            scanf("%d",&m);
            int a,b;
            for(int i=0; i<m; i++)
            {
                scanf("%d %d",&a,&b);
                dis[a][b]=dis[b][a]=1;
            }
            for(int k=0; k<n; k++)
            {
                for(int i=0; i<n; i++)
                {
                    for(int j=0; j<n; j++)
                    {
                        dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]);
                    }
                }
            }
            for(int i=0; i<n; i++)
            {
                for(int j=0; j<n; j++)
                {
                    if(dis[i][j]!=INF)
                    {
                        ans[dis[i][j]]++;
                    }
                }
            }
            for(int i=1;i<=n;i++)
            {
                if(ans[i])
                {
                    printf("%d %d
    ",i,ans[i]);
                }
            }
        }
        return 0;
    }

      ——————————————分割线————————————————

    H: A Partial Order Relation

    题目描述

    In mathematics, a partial order set formalizes and generalizes the intuitive concept of an ordering, sequencing, or arrangement of elements of a set. It is a binary relation indicating that, for certain pairs of elements in a set, one of the elements precedes the other in the ordering but not every pair is comparable. For example, the divisors of 120 form a partial order set. Let the binary relation ≺ be divisibility relation. So, 120 ≺ 24 and 120 ≺ 40 but there is no such relation between 24 and 40. ≺ is a transitive relation such that 120 ≺ 40 and 40 ≺ 8 imply 120 ≺ 8.
    Let’s define a more restrictive binary relation called greatest divisibility relation . Given a number a. Let div(a) be a set of all divisors of a minus a. Then we define a  b if b is a divisor of a but b cannot divide any other numbers in div(a). For example, div(30) = {1, 2, 3, 5, 6, 10, 15}. 30  6 because 6 cannot be used to divide other elements in the set.
    Given a number, you can construct  relation among the its divisors as in Fig. 1 for 120. The depth of the graph from root node (120) to the terminal node (always 1) is 5 and
    the number of edges are 28. Given an integer, please compute the number of edges in its  relation graph.

    输入

    The input data begins with a number n (n <= 100), which is the number of test cases. Each test case contains only a positive integer r, where 1 < r < 240 . r is the number to build  relation.

    输出

    For each test case, please print the number of edges in its divisibility relation.

    样例输入

    2
    6
    120
    

    样例输出

    4
    28

    分析:(分解质因数问题)
    有规律:n = 120时 有3个2的质数子数 1个3 1个5
    若一定有2 有 3*2*2种 (3、5可以选也可以不选)
    若一定有3 1*4*2
    若一定有5 1*4*2
    共28种 即有28条边
    代码:
    #include <bits/stdc++.h>
     
    using namespace std;
     
    typedef long long ll;
     
    ll n;
    int cnt;
    const int maxn = 1e5+5;
    int c[maxn];
    void f()
    {
        cnt = 0;
        for(ll i=2;i<=sqrt(n);i++)
        {
            if(n%i==0)
            {
                cnt++;
                while(n%i==0)
                {
                    n/=i;
                    c[cnt]++;
                }
            }
        }
        if(n>1)
        {
            cnt++;
            c[cnt] = 1;
        }
     
    }
    int main()
    {
        std::ios::sync_with_stdio(false);
        std::cin.tie(0);
        int T;
        cin>>T;
        while(T--)
        {
            memset(c,0,sizeof(c));
            cin>>n;
            if(n==1)
            {
                cout<<0<<endl;
                continue;
            }
            f();
     
    //        for(int i=1;i<=cnt;i++)
    //        {
    //            cout<<c[i]<<" ";
    //        }
    //        cout<<endl;
            ll ans = 0;
            for(int i=1;i<=cnt;i++)
            {
     
                ll tmp = c[i];
                for(int j=1;j<=cnt;j++)
                {
                    if(i!=j)
                    {
                        tmp*=c[j]+1;
                    }
                }
                ans+=tmp;
            }
            cout<<ans<<endl;
        }
    }

     ——————————————分割线————————————————

    I: Tracer Deployment

    题目描述

    Distributed denial of service (DDoS) is a rapidly growing network security problem.DDoS attacks always paralyze the network nodes such as servers and occupy the network bandwidth. How to defense DDoS attacks is a challenging issue for network security problem. The key to defense DDoS attacks is to find the attack origins. The log of attack origins can be used as evidence for post-attack investigations. Furthermore, if the attack origins can be located during the attack, the anomaly attack packets can be blocked by cooperative filter nodes distributed over the Internet. Therefore the network bandwidth among the attackers and targets can be prevented from being occupied by the attack packets. The IP traceback technology can be applied to identify the attack origins. It usually relays on enhanced routers and provides assistance in tracing the path traversed by attack traffic and then identify the machines that directly generate attack packets. Here, we refer to the enhance routers which provide tracing service as tracers. Evidently, the location of traces will affect the performance of locating the attack origins.
    Next, we consider the following tracer deployment problem: How many and where should the tracers be deployed in the network to be effective for locating the attack origins? Notice that the attacker can generate attack packets that appear to have originated from anywhere. It seems impossible to predict the location of attack origins. If the attack packets do not pass through any tracer in the network, the attack origins cannot be located using the IP traceback method.
    For example, if tracers are deployed at node A and node C, when an attack initiates at node a, passing through node B, and finally attacks node c, no tracer will detect the above attack. On the other hand, if three tracers are deployed at nodes A, B, and b, then any attack will always pass through at least one of the three tracer nodes, and the attack will be detected.

    Please write a program to determine the minimum number of tracers need to be deployed to ensure any attck will pass through (and be detected) at least one tracer. Note that the given network is a bipartite network G = (S, T, E) whose nodes can be partitioned into two subsets S and T so that each communication link (in E) has one end in S and one end in T. Also note that the network may be disconnected or even have isolated nodes.

    输入

    There are at most 10 test cases. The first line of each instance consists of an integer m(1 ≤ m ≤ 20), where m is the number of nodes (in set S of G = (S, T, E)) labeled with {0, 1, 2, . . . , m − 1} in the network. The second line of each instance consists of an integer n (1 ≤ n ≤ 20), where n is the number of nodes (in set T of G = (S, T, E)) labeled with {0, 1, . . . , n − 1} in the network. The third line of each instance consists of an integer e(1 ≤ e ≤ 100), where e is the number of communication links in the network. The next e lines, each contains two integers i, j, indicating a communication link exists between nodes i and node j. The last test case will be followed by a line containing a single 0.

    输出

    The output for each instance should contain an integer denoting the minimum number tracers needed for the given network.

    样例输入

    4
    3
    6
    0 0
    0 1
    1 0
    1 1
    1 2
    2 1
    4
    4
    7
    0 0
    0 2
    1 1
    1 3
    2 0
    2 2
    3 3
    0
    

    样例输出

    3
    4
    二分图的最小定点覆盖 套的板子。。。
    #include<bits/stdc++.h>
     
    using namespace std;
    struct node
    {
        int to,next;
    }edge[150];
    int head[150],tot,match[150];
    bool visit[150];
    void init()
    {
        memset(head,-1,sizeof(head));
        memset(match,-1,sizeof(match));
        tot=0;
    }
    void addedge(int u,int v)
    {
        edge[tot].to=v;
        edge[tot].next=head[u];
        head[u]=tot++;
    }
    bool dfs(int u)
    {
        for(int i=head[u];i!=-1;i=edge[i].next)
        {
            int v=edge[i].to;
            if(!visit[v])
            {
                visit[v]=1;
                if(match[v]==-1||dfs(match[v]))
                {
                    match[v]=u;
                    return 1;
                }
            }
        }
        return 0;
    }
    int main()
    {
        //freopen("in.txt","r",stdin);
        int n,m,e;
        while(1)
        {
            init();
            int ans=0;
            scanf("%d",&n);
            if(n==0)
            {
                break;
            }
            scanf("%d",&m);
            scanf("%d",&e);
            int a,b;
            for(int i=1;i<=e;i++)
            {
                scanf("%d %d",&a,&b);
                addedge(a,b);
            }
            for(int i=0;i<n;i++)
            {
                memset(visit,0,sizeof(visit));
                if(dfs(i))
                {
                    ans++;
                }
            }
            printf("%d
    ",ans);
        }
        return 0;
    }
     ——————————————分割线————————————————

    K: Assigning Frequencies

    题目描述

    Bob wants to assign frequencies to n satellites, numbered from 0 to n − 1. Two distinct satellites are said to be adjacent if, when assigned the same frequency, they interfere with each other. Sensibly, Bob’s assignment of frequencies must avoid interference altogether.
    However, only three frequencies are available to him.
    Please determine whether Bob can assign frequencies to all satellites to avoid any interference.

    输入

    The first line of the input contains an integer indicating the number of input cases. For each test case, the first line contains an integer n, denoting the number of satellites. The second line contains an integer p, denoting the number of adjacent pairs of satellites. The next p lines each contains two integers i, j, indicating the satellite i may intefer with satelite j when both are assigned the same frequency.

    输出

    For each test case, output “Y” if Bob can assign frequences so there is no integerence. Output “N” otherwise.

    样例输入

    4
    6
    6
    0 3
    1 5
    3 2
    2 5
    0 4
    1 0
    7
    12
    6 5
    0 3
    2 6
    3 5
    5 0
    0 4
    4 5
    6 3
    1 4
    1 2
    3 4
    2 3
    7
    8
    6 5
    0 3
    2 6
    3 5
    1 4
    1 2
    3 4
    2 3
    6
    9
    0 1
    1 2
    2 3
    5 2
    5 3
    3 4
    2 4
    1 4
    4 5
    

    样例输出

    Y
    N
    Y
    N

    染色问题 dfs 看别人代码 一下就会 但是自己就是写不出 自己太菜了
    #include <bits/stdc++.h>
     
    using namespace std;
     
    int n,m;
    int Map[55][55];
    int c[55];
    int dfs(int now)
    {
        for(int i=0;i<now;i++)
        {
            if(Map[now][i]&&now!=i&&c[now]==c[i])
            {
                return false;
            }
        }
        if(now==n-1)
            return true;
        for(int i=0;i<3;i++)
        {
            c[now+1] = i;
            if(dfs(now+1)) return true;
        }
    }
     
    int main()
    {
       int T;
       cin>>T;
       while(T--)
       {
           memset(Map,0,sizeof(Map));
           cin>>n>>m;
           for(int i=0;i<m;i++)
           {
               int x,y;
               cin>>x>>y;
               Map[x][y] = Map[y][x] = 1;
           }
     
           int flag = dfs(0);
           if(flag)
            cout<<"Y"<<endl;
           else
            cout<<"N"<<endl;
       }
       return 0;
    }
     
  • 相关阅读:
    md5加密排序
    md5加密
    PHP获取文件后缀名
    PHP中使用CURL实现GET、POST、PUT、DELETE请求
    PHP常用正则表达式精选
    19 个让 MySQL 效率提高 3 倍的 SQL 优化技巧
    git clone、 remote、fetch、pull、push、remote
    git 命令常用笔记
    十个推荐使用的 Laravel 的辅助函数
    PHP常用函数大全500+
  • 原文地址:https://www.cnblogs.com/hao-tian/p/9544243.html
Copyright © 2011-2022 走看看