1. 程式人生 > 實用技巧 >csp——第21次CCF計算機軟體能力認證

csp——第21次CCF計算機軟體能力認證

T1:

#include<iostream>
using  namespace std;

int n;
int  si;
int wi;
int res = 0;

int main()
{
    cin>>n;
    for(int i=0;i  < n;i++){
        cin>>si>>wi;
        res  += si * wi;
    }    
    if(res  < 0 ) res  = 0;
    cout<<res<<endl;

    return 0;
}

T2:

#include<bits/stdc++.h>
#include <iostream>
#include<cstring>
#include <algorithm>
using  namespace std;

const int maxn = 100005;
int m;
int sum = 0;
int idx = 0;
int cnt[maxn];
typedef struct node{
    long long  y;
    int  r;
}p;
p a[maxn];

bool cmp(p n1, p n2){
    
    if(n1.y == n2.y)
        return  n1.r <= n2.r;
    else
         return n1.y <= n2.y;
}

int main()
{
    cin>>m;
    for (int i=0 ; i  < m ;i++){
        cin >> a[i].y>> a[i].r;
      }    
    sort(a,a+m,cmp);  
    cnt[0] = a[0].r;
    for(int i = 1;i< m;i++){
            cnt[i] = cnt[i-1]+a[i].r;
      }

      sum = cnt[m-1];
      idx = 0;
      for(int i=1;i<m;i++)
    {  
         if(a[i].y == a[i-1].y) continue;
         
         int j = i;
         while(a[j].y== a[i].y){
                j++;
         }
         //tmp = cnt[m-1]-cnt[i-1] + i - cnt[i-1] 
         if(sum <=  (cnt[m-1]-cnt[i-1] + i - cnt[i-1]) )
            {//kao lv hcong fu zhi!!!

                sum = cnt[m-1]-cnt[i-1] + i - cnt[i-1] ;
                idx = i;
            }
            i  = j-1;
    } 
    //        1  2 3 4 5 
 // cnt[m-1]-cnt[i-1] +    i -  cnt[i-1]
 // sum = max(sum,cnt[m-1]);
 //   a[i].r  ==>  max    >=i 1  + < i 0
 //sum =   cnt[m-1]-cnt[i-1] + i - cnt[i-1]
    /*
    if(sum < cnt[m-1]){
        idx = 0;
    }*/
    cout<<a[idx].y;
    return 0;
}