BestCoder17(B,C)good

 
 
 

Select

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


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

树状数组+二分
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
const int maxn=100010;
typedef long long LL;
int N,K,cnt;
int X[maxn],a[1010][105];
int tree[maxn];
void add(int x,int val)
{
    while(x<=100000)
    {
        tree[x]+=val;
        x+=(x&(-x));
    }
}
LL getsum(int x)
{
    LL sum=0;
    while(x)
    {
        sum+=tree[x];
        x-=(x&(-x));
    }
    return sum;
}
void solve()
{
    memset(tree,0,sizeof(tree));
    for(int i=2;i<=N;i++)
    {
        for(int j=1;j<=a[i][0];j++)
        {
            int pos=lower_bound(X+1,X+cnt+1,a[i][j])-X;
            add(pos,1);
        }
    }
    LL ans=0;
    for(int i=1;i<N;i++)
    {
        for(int j=1;j<=a[i][0];j++)
        {
            int tmp=K-a[i][j]+1;
            int pos=lower_bound(X+1,X+cnt+1,tmp)-X;
            ans+=getsum(100000)-getsum(pos-1);
        }
        for(int j=1;j<=a[i+1][0];j++)
        {
            int pos=lower_bound(X+1,X+cnt+1,a[i+1][j])-X;
            add(pos,-1);
        }

    }
    printf("%I64d\n",ans);
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&N,&K);
        int m,x;
        cnt=1;
        for(int i=1;i<=N;i++)
        {
            scanf("%d",&a[i][0]);
            for(int j=1;j<=a[i][0];j++)
            {
                scanf("%d",&a[i][j]);
                X[cnt++]=a[i][j];
            }
        }
        sort(X+1,X+cnt);
        cnt=unique(X+1,X+cnt)-X-1;
        solve();
    }
    return 0;
}


The K-th Distance

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 302    Accepted Submission(s): 81


Problem Description
Given a tree, which has n node in total. Define the distance between two node u and v is the number of edge on their unique route. So we can have n(n-1)/2 numbers for all the distance, then sort the numbers in ascending order. The task is to output the sum of the first K numbers.
 

Input
There are several cases, first is the number of cases T. (There are most twenty cases).
For each case, the first line contain two integer n and K ( 2n100000,0Kmin(n(n1)/2,106)  ). In following there are n-1 lines. Each line has two integer u , v. indicate that there is an edge between node u and v.
 

Output
For each case output the answer.
 

Sample Input
       
       
2 3 3 1 2 2 3 5 7 1 2 1 3 2 4 2 5
 

Sample Output
       
       
4 10


把所有边(u,v) 以及(v,u)放入一个队列,队列每弹出一个元素(u,v),对于所有与u相邻的点w,如果w!=v,就把(w,u)入队。这样就能一个一个生成前K小的距离。
注意到每条边实际上会入队两次,只要把K翻倍且把ans除2即可,时间复杂度为O(n+K)
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn=100010;
int N,K;
vector<int> g[maxn];
struct node
{
    int u,v,w;
    node(){}
    node(int x,int y,int f)
    {
        u=x;
        v=y;
        w=f;
    }
};
void solve()
{
    queue<node> q;
    for(int i=0;i<=N;i++)g[i].clear();
    for(int i=0;i<N-1;i++)
    {
        int u,v;
        scanf("%d%d",&u,&v);
        g[u].push_back(v);
        g[v].push_back(u);
        q.push(node(u,v,1));
        q.push(node(v,u,1));
    }
    LL ans=0;
    int cnt=2*(N-1);
    while(!q.empty())
    {
        node tmp=q.front();q.pop();
        int u=tmp.u,v=tmp.v;
        ans+=tmp.w;
        if(cnt>=2*K)continue;
        int len=g[u].size();
        for(int i=0;i<len;i++)
        {
            int x=g[u][i];
            if(x==v)continue;
            if(cnt<K*2)
            {
                q.push(node(x,u,tmp.w+1));
                cnt++;
            }
        }
    }
    printf("%I64d\n",ans/2);
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&N,&K);
        solve();
    }
    return 0;
}





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值