美文网首页
2020-09-07 纪念品分组

2020-09-07 纪念品分组

作者: JalorOo | 来源:发表于2020-09-07 21:44 被阅读0次

https://www.luogu.com.cn/problem/P1094

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <sstream>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;

template<typename DataType>
DataType qmi(DataType m, int k)
{
    DataType res = 1, t = m;
    while (k)
    {
        if (k&1) res = res * t;
        t = t * t;
        k >>= 1;
    }
    return res;
}

int read(){
    int x = 0,f = 1;
    char c = getchar();
    while (c<'0'||c>'9') {
        if (c=='-') {
            f = -1;
        }
        c = getchar();
    }
    while (c>='0'&&c<='9') {
        x = x*10+c-'0';
        c = getchar();
    }
    return x*f;
}

#define fi(a,b) for(int i = a; i <= b; i++)
#define fj(a,b) for(int j = a; j >= b; j--)



void quickSort(int *a,int left,int right){
    int i,j;
    
    int temp,t;
    
    temp = a[(left+right)/2];//基准值
    
    i = left;
    j = right;
    
    while(i<=j){
        while (a[j] > temp) {
            j--;
        }
        
        while (a[i] < temp) {
            i++;
        }
        
        if (i<=j) {
            t = a[i];
            a[i] = a[j];
            a[j] = t;
            //继续下一步
            i++;
            j--;
        }
        
    }
    
    if(left<j)quickSort(a,left, j);//继续分治
    if(i<right)quickSort(a,i, right);//继续分治
}

int w,n,map[30005],ans = 0;
int main(){
    //输入部分
    w = read();
    n = read();
    fi(1, n){
        map[i] = read();
    }
    
    //排序,然后一一匹配
    quickSort(map, 1, n);
    
//    fi(1, n){
//        cout<<map[i]<<"\t";
//    }
    
    int i = 1, j = n;
    while (i<=j) {
        if(i==j){
            ans++;
            //cout<<map[j]<<endl;
            break;
        }
        if(map[i] + map[j]<=w){
            //cout<<map[i]<<" "<<map[j]<<endl;
            ans++;
            i++;
            j--;
        } else {
            //cout<<map[j]<<endl;
            ans++;
            j--;
        }
    }
    cout<<ans;
}
/*
100
9
 90
 20
 20
 30
 50
 60
 70
 80
 90
 
 20+90! 90
 20+90! 90
 20+80
 20+70
 30+60
 50
 
============
6
*/

相关文章

网友评论

      本文标题:2020-09-07 纪念品分组

      本文链接:https://www.haomeiwen.com/subject/joqrektx.html