zoukankan      html  css  js  c++  java
  • hihocoder_offer收割编程练习赛55_1

    题目链接: https://hihocoder.com/contest/offers55/problem/1

    解题思路: 括号的作用,可以考虑去括号,然后就是一系列的加减运算。

                      当没有括号时,减去所有小的数,

                      当有括号时,考虑用两个减号和一个括号,组成  -(A1-A2-A3-A4...),这样去括号以后,就只有一个减号,减去最小值即可。

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 
     4 int a[105];
     5 int P, Q, K;
     6 int n;
     7 
     8 int main()
     9 {
    10 #ifndef ONLINE_JUDGE
    11     freopen("test_1.txt", "r", stdin);
    12 #endif // ONLINE_JUDGE
    13     scanf("%d", &n);
    14     scanf("%d%d%d", &P, &Q, &K);
    15     for (int i = 1; i <= n; ++i)
    16     {
    17         scanf("%d", &a[i]);
    18     }
    19     sort(a+1, a+n+1);
    20     if (K==0)
    21     {
    22         int ans = a[n];
    23     for (int i = 1; i <= Q; ++i)
    24         {
    25             ans = ans - a[i];
    26         }
    27     for (int i = 1; i <= P; ++i)
    28         ans = ans + a[Q+i];
    29     printf("%d
    ", ans);
    30     }
    31     else
    32         {
    33             int ans = a[n];
    34         int i = 1;
    35     for (i = 1; i <= min(1, Q); ++i)
    36         ans = ans - a[i];
    37     for (; i < n; ++i)
    38         ans += a[i];
    39     printf("%d
    ", ans);
    40         }
    41     return 0;
    42 }
  • 相关阅读:
    Java 字符串总结
    782B The Meeting Place Cannot Be Changed(二分)
    初学hash
    Codeforces Round #395 C. Timofey and a tree
    Java集合之ArrayList
    CA Loves GCD (BC#78 1002) (hdu 5656)
    hdu 5661 Claris and XOR
    hdu 5945 Fxx and game
    pay包注释(二)
    编程风格
  • 原文地址:https://www.cnblogs.com/djingjing/p/8854698.html
Copyright © 2011-2022 走看看