zoukankan      html  css  js  c++  java
  • 洛谷P1842 奶牛玩杂技

    洛谷P1842 奶牛玩杂技

    贪心

    做法很经典 比较相邻两个 而且这个式子更简单 Sx-wy>=Sy-Wx (x放下面更优)

    移项之后排序直接弄就好

     1 #include <cstdio> 
     2 #include <algorithm>
     3 #define For(i,j,k) for(int i=j;i<=k;i++) 
     4 #define Dow(i,j,k) for(int i=j;i>=k;i--) 
     5 using namespace std ; 
     6 
     7 const int N = 50011,inf = 1e8 ; 
     8 struct node{
     9     int w,s,sum ; 
    10 }a[N] ;
    11 int n ; 
    12 
    13 inline int read() 
    14 {
    15     int x = 0 , f = 1 ; 
    16     char ch = getchar() ; 
    17     while(ch<'0'||ch>'9') { if(ch=='-') f = -1 ; ch = getchar(); } 
    18     while(ch>='0'&&ch<='9') { x = x * 10+ch-48 ; ch = getchar(); } 
    19     return x * f ; 
    20 }
    21 inline bool cmp(node a,node b) 
    22 {
    23     return a.sum < b.sum ; 
    24 }
    25 
    26 int main() 
    27 {
    28     n = read() ; 
    29     For(i,1,n) 
    30         a[i].w=read(),a[i].s=read(),a[i].sum=a[i].s+a[i].w ; 
    31     sort(a+1,a+n+1,cmp) ; 
    32     int ans = -inf,tot = 0 ; 
    33     For(i,1,n) {
    34         ans=max(ans,tot-a[i].s) ; 
    35         tot+=a[i].w ; 
    36     }
    37     printf("%d
    ",ans) ; 
    38     return 0 ; 
    39 }
  • 相关阅读:
    数组乘积更新
    win向linux传文件
    遇到autoreconf: not found
    python thread
    aptitude
    virtualbox安装ubuntu出现“The system is running in low-graphics mode”
    webform用户控件
    LinQ to SQL
    表单验证
    文件上传
  • 原文地址:https://www.cnblogs.com/third2333/p/7625506.html
Copyright © 2011-2022 走看看