zoukankan      html  css  js  c++  java
  • UVa 11039 Building designing

      这道题我把它想麻烦了,本来可以按绝对值直接排序的,我却把它们分成两组,再排序然后再从中选数进行合并,不知道怎么想的,不过倒是对sort函数又了解了一些。

      下面是最初写的代码:  

    View Code
     1 #include <cstdio>
     2 #include <algorithm>
     3 #include <functional>
     4 #include <cstdlib>
     5 using namespace std;
     6 
     7 const int maxn = 500000+10;
     8 int color[2][maxn];
     9 
    10 int main()
    11 {
    12 #ifdef LOCAL
    13     freopen("in", "r", stdin);
    14 #endif
    15     int p;
    16     scanf("%d", &p);
    17     while(p--)
    18     {
    19         int n;
    20         scanf("%d", &n);
    21         int size[2];
    22         size[0] = size[1] = 0;
    23         for(int i = 0; i < n; i++)
    24         {
    25             int num;
    26             scanf("%d", &num);
    27             if(num > 0)   color[1][size[1]++] = num;
    28             else if(num < 0)   color[0][size[0]++] = abs(num);
    29         }
    30         if(size[0] == 0 || size[1] == 0)
    31         {
    32             printf("1\n");
    33             continue;
    34         }
    35         sort(color[0], color[0]+size[0], greater<int>());
    36         sort(color[1], color[1]+size[1], greater<int>());
    37         int which;
    38         int p[2];
    39         p[0] = p[1] = 0;
    40         which = (color[0][0] > color[1][0]) ? 0 : 1;
    41         int t = color[which][0];
    42         p[which]++;
    43         int ans = 1;
    44         which = 1 - which;
    45         while(p[which] < size[which])
    46         {
    47             if(color[which][p[which]] < t)
    48             {
    49                 t = color[which][p[which]];
    50                 ans++;
    51                 p[which]++;
    52                 which = 1 - which;
    53             }
    54             else p[which]++;
    55         }
    56         printf("%d\n", ans);
    57     }
    58     return 0;
    59 }

      这个是后来看别人代码后幡然醒悟写的:

    View Code
     1 #include <cstdio>
     2 #include <algorithm>
     3 #include <cstdlib>
     4 using namespace std;
     5 
     6 const int maxn = 500000+10;
     7 int a[maxn];
     8 
     9 bool compare(int x, int y)
    10 {
    11     return abs(x) > abs(y);
    12 }
    13 
    14 int isPos(int x)
    15 {
    16     if(x > 0)   return 1;
    17     return -1;
    18 }
    19 
    20 int main()
    21 {
    22 #ifdef LOCAL
    23     freopen("in", "r", stdin);
    24 #endif
    25     int p;
    26     scanf("%d", &p);
    27     while(p--)
    28     {
    29         int n;
    30         scanf("%d", &n);
    31         for(int i = 0; i < n; i++)
    32             scanf("%d", &a[i]);
    33         sort(a, a+n, compare);
    34         int ans = 1;
    35         int sign = isPos(a[0]);
    36         for(int i = 1; i < n; i++)
    37             if(a[i] * sign < 0)
    38             {
    39                 ans++;
    40                 sign *= -1;
    41             }
    42         printf("%d\n", ans);
    43     }
    44     return 0;
    45 }

       其实这道题按升序降序都是没有影响的,因为它只要求输出个数,如果是序列的话就该考虑一下升序降序了,比如让绝对值之和最小。

  • 相关阅读:
    [译]JavaScript源码转换:非破坏式与再生式
    [译]ES6中的代理对象
    tensorflow 如何获取graph中的所有tensor name
    python3中的str和bytes
    git submodule 添加 更新 删除 教程
    《重构:改善既有代码的设计》摘抄
    thrift入门教程/thrift资料集合
    将python2代码升级为python3代码最佳实践
    python标准库:subprocess——子进程管理
    安装“python-snappy”遇到“error: command 'x86_64-linux-gnu-gcc' failed with exit status 1”
  • 原文地址:https://www.cnblogs.com/xiaobaibuhei/p/3000915.html
Copyright © 2011-2022 走看看