zoukankan      html  css  js  c++  java
  • 7-39 魔法优惠券 (25分)--sort()

    //sort(a+i,a+j)------->a[i]到a[j-1]被排序。
    1
    #include <iostream> 2 #include <algorithm> 3 4 using namespace std; 5 long int pos[1000001], neg[1000001],price[1000001]; 6 long int N, M; 7 long int k1 = -1, k2 = -1; 8 long int p1 = -1, p2 = -1; 9 bool cmp_less(long int a,long int b) 10 { 11 return a > b; 12 } 13 bool cmp_greater(long int a, long int b) 14 { 15 return a < b; 16 } 17 int main() 18 { 19 cin >> N; 20 for (long int i = 0; i < N; i++) 21 { 22 long int temp; cin >> temp; 23 if (temp > 0) 24 { 25 pos[++k1] = temp; 26 } 27 else 28 { 29 neg[++k2] = temp; 30 } 31 } 32 cin >> M; 33 for (long int i = 0; i < M; i++) 34 { 35 cin >> price[i]; 36 } 37 sort(pos, pos + N,cmp_less); 38 sort(neg, neg + N, cmp_greater); 39 sort(price, price + M, cmp_less); 40 long int sum = 0; 41 long int p = M - 1; 42 long int flag = 0; 43 44 for (long int i = 0; i < M; i++) 45 { 46 if (price[i] < 0 && flag == 0) 47 { 48 sort(price + i, price + M, cmp_greater); 49 flag = 1; 50 } 51 if (price[i] > 0) 52 { 53 if (k1 != p1) 54 { 55 sum += price[i] * pos[++p1]; 56 } 57 } 58 if (price[i] < 0) 59 { 60 if (k2 != p2) 61 { 62 sum += price[i] * neg[++p2]; 63 } 64 } 65 } 66 cout << sum; 67 return 0; 68 }
  • 相关阅读:
    函数式宏定义与普通函数
    linux之sort用法
    HDU 4390 Number Sequence 容斥原理
    HDU 4407 Sum 容斥原理
    HDU 4059 The Boss on Mars 容斥原理
    UVA12653 Buses
    UVA 12651 Triangles
    UVA 10892
    HDU 4292 Food
    HDU 4288 Coder
  • 原文地址:https://www.cnblogs.com/2020R/p/12772012.html
Copyright © 2011-2022 走看看