• hihocoder 挑战赛9 A.好配对(思维题目 防止超时)


    #1123 : 好配对

    时间限制:1000ms
    单点时限:1000ms
    内存限制:256MB

    描述

    给定两个序列a和b,每个序列中可能含有重复的数字。

    一个配对(i,j)是一个好配对当从第一个序列中选出一个数ai,再从第二个序列中选出一个数bj且满足ai>bj

    给出两个序列,问存在多少个好配对。

    输入

    输入包含多组数据,数据第一行一个整数T,表示数据组数。(T<=5)

    每组数据第一行包含两个整数n和m。(0<n,m<=105)

    接下来n行,每行两个整数x和y,表示在第一个序列中有y个x。

    接下来m行,每行两个整数x和y,表示在第二个序列中有y个x。(0<x<=109,0<y<=104)

    输出

    对于每组数据,输出一行一个整数,表示好配对的数量

    样例输入
    1
    2 2
    3 2
    4 1
    3 1
    2 3
    样例输出
    10

    算法:O(n)的复杂度
    代码:
     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <stdlib.h>
     4 #include <math.h>
     5 #include <iostream>
     6 #include <string>
     7 #include <iomanip>
     8 #include <vector>
     9 #include <queue>
    10 #include <algorithm>
    11 #define N 100000+10
    12 
    13 using namespace std;
    14 int n, m;
    15 struct nodea
    16 {
    17     int x, y;
    18     bool operator < (const nodea&dd)const
    19     {
    20         return x<dd.x;
    21     }
    22 }a[N];
    23 
    24 struct nodeb
    25 {
    26     int x, y;
    27     bool operator < (const nodeb&dd)const
    28     {
    29         return x<dd.x;
    30     }
    31 }b[N];
    32 
    33 
    34 int main()
    35 {
    36     int t;
    37     int i, j;
    38     scanf("%d", &t);
    39     while(t--)
    40     {
    41         scanf("%d %d", &n, &m);
    42         for(i=0; i<n; i++)
    43         {
    44             scanf("%d %d", &a[i].x, &a[i].y);
    45         }
    46         for(i=0; i<m; i++)
    47         {
    48             scanf("%d %d", &b[i].x, &b[i].y);
    49         }
    50         sort(a, a+n);
    51         sort(b, b+m);
    52         long long int cnt=0;
    53         long long int sum=0;
    54         j=0;
    55         for(i=0; i<n; i++)
    56         {
    57             while(a[i].x > b[j].x && j<m )
    58             {
    59                 cnt+=b[j].y;
    60                 j++;
    61             }
    62             sum=sum+cnt*a[i].y;
    63             //printf("%lld---", sum );
    64         }
    65         printf("%lld
    ", sum );
    66     }
    67     return 0;
    68 }
    View Code
  • 相关阅读:
    2016/01/13开始学习git:分支管理:创建、合并、删除分支
    2016/01/13开始学习git:远程仓库
    2016/01/13开始学习git:删除文件
    2016/01/13开始学习git:管理修改、撤销修改
    2016/01/12开始学习git:版本如何回退
    2016/01/11开始学习git:查看仓库状态和修改文件
    2016/01/11开始学习git:创建版本库后,add和commit
    2016/01/10开始学习git:安装msysgit
    Django web框架开发基础-django实现留言板功能
    Django web框架开发基础-01
  • 原文地址:https://www.cnblogs.com/yspworld/p/4310760.html
Copyright © 2020-2023  润新知