zoukankan      html  css  js  c++  java
  • CCF 201809_2

    问题描述

      小H和小W来到了一条街上,两人分开买菜,他们买菜的过程可以描述为,去店里买一些菜然后去旁边的一个广场把菜装上车,两人都要买n种菜,所以也都要装n次车。具体的,对于小H来说有n个不相交的时间段[a1,b1],[a2,b2]...[an,bn]在装车,对于小W来说有n个不相交的时间段[c1,d1],[c2,d2]...[cn,dn]在装车。其中,一个时间段[s, t]表示的是从时刻s到时刻t这段时间,时长为t-s。
      由于他们是好朋友,他们都在广场上装车的时候会聊天,他们想知道他们可以聊多长时间。
    输入格式
      输入的第一行包含一个正整数n,表示时间段的数量。
      接下来n行每行两个数ai,bi,描述小H的各个装车的时间段。
      接下来n行每行两个数ci,di,描述小W的各个装车的时间段。
    输出格式
      输出一行,一个正整数,表示两人可以聊多长时间。
    样例输入
    4
    1 3
    5 6
    9 13
    14 15
    2 4
    5 7
    10 11
    13 14
    样例输出
    3
    数据规模和约定
      对于所有的评测用例,1 ≤ n ≤ 2000, ai < bi < ai+1,ci < di < ci+1,对于所有的i(1 ≤ i ≤ n)有,1 ≤ ai, bi, ci, di ≤ 1000000。

    第一次只得了10分,发现忘记考虑一个人某一段时间过长的情况,如甲【1,9】,乙【1,3】,【4,6】,【7,9】,这样甲一段时间就包含了乙的三段时间
     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 
     4 int main()
     5 {
     6     int n;
     7     while(~scanf("%d",&n))
     8     {
     9         long long sum=0;
    10         long long a[2005][2];
    11         long long b[2005][2];
    12         for(int i=0;i<n;i++)
    13         {
    14             scanf("%lld",&a[i][0]);
    15             scanf("%lld",&a[i][1]);
    16         }
    17         for(int i=0;i<n;i++)
    18         {
    19             scanf("%lld",&b[i][0]);
    20             scanf("%lld",&b[i][1]);
    21             long long x=a[i][0]>=b[i][0]?a[i][0]:b[i][0];
    22             long long y=a[i][1]<=b[i][1]?a[i][1]:b[i][1];
    23             if(y>x)
    24                 sum=sum+y-x;
    25         }
    26         printf("%lld
    ",sum);
    27     }
    28     return 0;
    29 }
    View Code

    后来在网上找到了另一个思路

    参考大佬代码

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 
     4 #define N 1000005
     5 int a[N];
     6 int main()
     7 {
     8     int n,x,y;
     9     scanf("%d",&n);
    10     for(int i=1;i<=2*n;i++)
    11     {
    12         scanf("%d%d",&x,&y);
    13         for(int j=x;j<y;j++)
    14           a[j]++;
    15     }
    16     long long ans=0;
    17     for(int i=1;i<N;i++)
    18       if(a[i]==2)
    19         ans++;
    20     printf("%lld
    ",ans);
    21     return 0;
    22 } 
    View Code

    参考链接:https://blog.csdn.net/SongBai1997/article/details/83308345

                              https://www.cnblogs.com/syq816/p/9872416.html

     
  • 相关阅读:
    基于Linux C的socketEthereal程序和Package分析 (一个)
    [Firebase + PWA] Keynote: Progressive Web Apps on Firebase
    [Redux] Navigating with React Router <Link>
    [Redux] Adding React Router to the Project
    [Redux] Refactoring the Entry Point
    [Redux] Persisting the State to the Local Storage
    [Redux] Supplying the Initial State
    [PWA] Caching with Progressive libraries
    [TypeScript] Understanding Generics with RxJS
    [React + Mobx] Mobx and React intro: syncing the UI with the app state using observable and observer
  • 原文地址:https://www.cnblogs.com/carrothhh/p/11443900.html
Copyright © 2011-2022 走看看