• HDU 5101 Select



    Select

    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 786    Accepted Submission(s): 232


    Problem Description
    One day, Dudu, the most clever boy, heard of ACM/ICPC, which is a very interesting game. He wants to take part in the game. But as we all know, you can't get good result without teammates.
    So, he needs to select two classmates as his teammates. 
    In this game, the IQ is very important, if you have low IQ you will WanTuo. Dudu's IQ is a given number k. We use an integer v[i] to represent the IQ of the ith classmate. 
    The sum of new two teammates' IQ must more than Dudu's IQ.
    For some reason, Dudu don't want the two teammates comes from the same class.
    Now, give you the status of classes, can you tell Dudu how many ways there are.
     

    Input
    There is a number T shows there are T test cases below. (T20)
    For each test case , the first line contains two integers, n and k, which means the number of class and the IQ of Dudu. n ( 0n1000 ), k( 0k<231 ).
    Then, there are n classes below, for each class, the first line contains an integer m, which means the number of the classmates in this class, and for next m lines, each line contains an integer v[i], which means there is a person whose iq is v[i] in this class. m( 0m100 ), v[i]( 0v[i]<231 )
     

    Output
    For each test case, output a single integer.
     

    Sample Input
    1 3 1 1 2 1 2 2 1 1
     

    Sample Output
    5
     

    Source
     

    题目大意:
    给定一些集合,选择两个来自不同集合的数,加和大于k。问有多少种选择方案。

    解题思路: 答案=从全部数中选择的两个加和大于k的数的方案数-在同一个集合中选择的两个加和大于k的数的方案数 而对于同一个集合中选择的两个加和大于k的方案数是能够直接排序然后利用单调性高速统计出来的。

    //546MS	1860K
    #include<stdio.h>
    #include<string.h>
    #include<algorithm>
    using namespace std;
    long long s[1007][107],num[1007],array[100007];
    int main()
    {
        int t;
        scanf("%d",&t);
        while(t--)
        {
            int n;
            long long k,a=0,ans=0;
            scanf("%d%I64d",&n,&k);
            for(int i=0; i<n; i++)
            {
                scanf("%I64d",&num[i]);
                for(int j=0; j<num[i]; j++)
                {
                    scanf("%I64d",&s[i][j]);
                    array[a++]=s[i][j];
                }
                sort(s[i],s[i]+num[i]);
            }
            sort(array,array+a);
            int x=a-1;
            for(int i=0; i<a; i++)//从全部数中选择的两个加和大于k的数的方案数
            {
                while(array[i]+array[x]>k&&x>i)x--;
                if(i>x)ans+=(a-i-1);
                else ans+=(a-x-1);
            }
            long long count=0;
            for(int i=0; i<n; i++)//在同一个集合中选择的两个加和大于k的数的方案数
            {
                int x=num[i]-1;
                for(int j=0; j<num[i]; j++)
                {
                    while(s[i][j]+s[i][x]>k&&x>j) x--;
                    if(j>=x)count+=(num[i]-j-1);
                    else count+=(num[i]-x-1);
                }
            }
            printf("%I64d
    ",ans-count);
        }
    }
    


  • 相关阅读:
    数据库
    Xpath表达式
    错误:向非托管代码传递委托时,托管应用程序必须让这些委托保持活动状态,直到确信不会再次调用它们
    xmlhttp status各类Http请求状态(status)及其含义
    配置ckeditor3.0 和ckfinder
    程序默认用管理员身份打开(vs2010)
    (转)CKFinder破解的方法,去掉提示
    无法嵌入互操作类型“SHDocVw.ShellWindowsClass”。请改用适用的接口
    ASP.NET页面执行顺序
    回调函数
  • 原文地址:https://www.cnblogs.com/slgkaifa/p/6871840.html
Copyright © 2020-2023  润新知