zoukankan      html  css  js  c++  java
  • The Great Team(好题)

     The Great Team
    Time Limit:500MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

    Description

    When a few students of the Ural State University finished their sport career, the university encountered a lot of problems in team composition. Veterans of sports programming decided to play their role and create the most successful team in the history of the Ural SU.
    Veterans assumed that success of a team strongly depends on the number of friends in the ACM community the members of this team have. After more discussions they developed the criterion of success: all three members of the team should have the same number of friends.
    Unfortunately, the veterans failed to compose a team, as it turned out that there were no three programmers in the Ural SU that together satisfied this criterion.
    You should use this information to determine which students are friends of each other.

    Input

    The first line contains a single integer n (3 ≤ n ≤ 200), which is the number of students in the Ural SU participating in programming contests.

    Output

    If the veterans' calculations are correct, the first line should contain an integer k, which is the number of pairs of students that are friends of each other. The following k lines should contain these pairs. Students should be numbered 1 through n. If a problem has multiple correct answers, output any of them.
    If the veterans are wrong and the problem has no solution, output a single line containing a number −1.

    Sample Input

    inputoutput
    4
    
    2
    1 3
    3 4
     

    题意:有n个人,若可以使其中至多只有两个人朋友数同等,输出可能的朋友配对;否则,输出-1.

    实际上,对任意n,总可能使得至多有2人朋友数相等。采取的策略如下:

    (1)n为偶数

    设编号为i的人的朋友集合为S(i),如题中所给例子,S(1) = {3}, S(4) = {3},S(2) = ∅。对于编号由1到n/2的人,设置其朋友集合为S(i) = {n/2+i, n/2+i+1,… ,n},(1 <= i <= n/2)。这样就能使|S(i)| = |S(n - i + 1)| !=其它集合的势,此时每一个人的朋友数均与唯一的另一个人的朋友数相等,故满足至多有两个人朋友数相等的要求。

    (2)n为奇数

    设置第n个人没有朋友,而剩下的n - 1个人照上面的情况处理即可。

    AC CODE

     1 //Memory: 112 KB        Time: 15 MS
     2 //Language: C++        Result: Accepted
     3 
     4 #include <iostream>
     5 #include <cstdio>
     6 using namespace std;
     7 
     8 int main() 
     9 {
    10   int n;
    11   while(scanf("%d", &n) != EOF)
    12   {
    13     if(n & 1)  n--;
    14     printf("%d\n", (n + 2) * n / 8);
    15     for(int i = 1; i <= n / 2; i++)
    16     {
    17       for(int j = n / 2 + i; j <= n; j++)
    18       {
    19         printf("%d %d\n", i, j);        
    20       }
    21     }               
    22   }    
    23 }
  • 相关阅读:
    优化代码及其他注意事项---好好做人吧
    el-table里面的列需要对比两个返回参数
    怎么样使element ui 的table某列变色
    怎么在app上添加图标和文字
    app内嵌H5的上传图片的功能
    将本地的链接在手机上查看
    《概率统计》2.离散型随机变量:分布与数字特征
    《概率统计》1.理论基石:条件概率、独立性与贝叶斯
    pygame(1):基本使用(更新中~~~)
    详解DataFrame、Series的replace方法
  • 原文地址:https://www.cnblogs.com/cszlg/p/2910480.html
Copyright © 2011-2022 走看看