zoukankan      html  css  js  c++  java
  • 51nod1267(双指针)

    题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1267

    题意:中文题诶~

    思路:双指针

    求a+b+c+d=0,令a+b=e, c+d=f,即e+f=0;

    所以可以先给所有数两两求和,并记录其下标,再根据和的大小排序。

    再用双指针扫描即可。

    代码:

     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <algorithm>
     4 #define MAXN 1010
     5 using namespace std;
     6 
     7 int a[MAXN], pos=0;
     8 
     9 struct node{
    10     int x, y;
    11     int value;
    12 }gg[MAXN*MAXN];
    13 
    14 bool cmp(node a, node b){
    15     return a.value < b.value;
    16 }
    17 
    18 int is_ok(int l, int r){
    19     if(gg[l].value+gg[r].value==0)
    20         if(gg[l].x==gg[r].x||gg[l].y==gg[r].y||gg[l].x==gg[r].y||gg[l].y==gg[r].x) return 2;
    21         else return 1;
    22     else if(gg[l].value+gg[r].value>0) return 3;
    23     else return 4;
    24 }
    25 
    26 bool solve(){
    27     int l=0, r=pos-1;
    28     while(l<=r){
    29         int cnt=is_ok(l, r);
    30         if(cnt==1) return true;
    31         else if(cnt==2)
    32             if(gg[l].value==gg[l+1].value) l+=1;
    33             else if(gg[r].value==gg[r-1].value) r-=1;
    34             else l+=1, r-=1;
    35         else if(cnt==3) r-=1;
    36         else l+=1;
    37     }
    38     return false;
    39 }
    40 
    41 int main(void){
    42     int n;
    43     cin >> n;
    44     for(int i=0; i<n; i++){
    45         cin >> a[i];
    46         for(int j=0; j<i; j++){
    47             gg[pos].x=j;
    48             gg[pos].y=i;
    49             gg[pos++].value=a[i]+a[j];
    50         }
    51     }
    52     sort(gg, gg+pos, cmp);
    53     int flag=solve();
    54     if(flag) cout << "Yes" << endl;
    55     else cout << "No" << endl;
    56     return 0;
    57 }
    View Code
  • 相关阅读:
    Extension:WYSIWYG
    partprobe
    Centos install Parosid
    linux 打造man中文帮助手册图解
    男人到了二十几岁后
    Mediawiki update to 1.24
    华为笔试题
    排序算法
    求素质的算法
    判断有符号和无符号数和符号
  • 原文地址:https://www.cnblogs.com/geloutingyu/p/6685849.html
Copyright © 2011-2022 走看看