zoukankan      html  css  js  c++  java
  • hdu--4334-双指针

    这个方法其实 蛮常用的 

    一般给你3个集合  然后让你满足某个等式 a[x]+b[y] = c[z]

    我们通常都是枚举一个集合 然后用两个指针分别指向另外2个集合中的一头一尾  然后进行大小比较来 指针移动

    当然 这也是和 二分一样建立在 有序的基础之上进行的

    这题 用这个方法解决 就可以了

    当然 二分也可以

     1 #include <iostream>
     2 #include <algorithm>
     3 using namespace std;
     4 
     5 int n;
     6 const int size = 210;
     7 typedef __int64 LL;
     8 LL sum1[size*size] , sum2[size*size] , sum3[size];
     9 LL a[10][size];
    10 
    11 bool solve( )
    12 {
    13     bool flag = false;
    14     for( int i = 0 ; i<n ; i++ )
    15     {
    16         LL* st = sum1;
    17         LL* end = sum2 + n*n-1 ;
    18         while( st<=sum1+n*n-1 && end>=sum2 )
    19         {
    20             if( *st + *end  + sum3[i] == 0 )
    21             {
    22                 flag = true;
    23                 break;
    24             }
    25             else if( *st + *end + sum3[i] < 0 )
    26             {
    27                 ++ st;
    28             }
    29             else
    30             {
    31                 -- end;
    32             }
    33         }
    34         if( flag )
    35         {
    36             break;
    37         }
    38     }
    39     return flag;
    40 }
    41 
    42 int main()
    43 {
    44     cin.sync_with_stdio(false);
    45     int t , cnt , tot;
    46     bool ans;
    47     cin >> t;
    48     while( t-- )
    49     {
    50         cin >> n;
    51         for( int i = 1 ; i<=5 ; i++ )
    52         {
    53             for( int j = 1 ; j<=n ; j++ )
    54             {
    55                 cin >> a[i][j];
    56             }
    57         }
    58         tot = 0;
    59         for( int j = 1 ; j<=n ; j++ )
    60         {
    61             for( int k = 1 ; k<=n ; k++ )
    62             {
    63                 sum1[tot++] = a[1][j] + a[2][k];
    64             }
    65         }
    66         tot = cnt = 0;
    67         for( int j = 1 ; j<=n ; j++ )
    68         {
    69             for( int k = 1 ; k<=n ; k++ )
    70             {
    71                 sum2[tot++] = a[3][j] + a[4][k];
    72             }
    73             sum3[cnt++] = a[5][j];
    74         }
    75         sort( sum1 , sum1+n*n );
    76         sort( sum2 , sum2+n*n );
    77         ans = solve( );
    78         if( ans )
    79             cout << "Yes" << endl;
    80         else
    81             cout << "No" << endl;
    82     }
    83     return 0;
    84 }
    View Code

    today:

      约你出来

      你不出来

      放我鸽子

  • 相关阅读:
    详解Oracle安装与配置.
    如何做大规模软件的配置管理
    关于软件权限设置的一点心得体会
    使用RDLC报表(一)
    详细解析Linux scp命令的应用
    spring propertyplaceholderconfigurer
    hibernate ehcache
    由Eclipse内存不足谈谈JVM内存
    事务策略: API 层策略
    什么叫控制反转(IoC )
  • 原文地址:https://www.cnblogs.com/radical/p/4126963.html
Copyright © 2011-2022 走看看