1 条题解

  • 0
    @ 2021-6-15 1:39:16

    C++ :

    #include<iostream>
    #include<algorithm>
    using namespace std;
    int a[30005];
    int main(){
    	int w,n;
    	cin>>w>>n;
    	for (int i=0;i<n;i++)
    		cin>>a[i];
    	sort(a,a+n);
    	int i,j,ans=0;
    	for (i=0,j=n-1;i<j;){
    		if (a[i]+a[j]>w)
    			ans++,j--;
    		else ans++,i++,j--;
    	}
    	if (i==j) ans++;
    	cout<<ans<<endl;
    	return 0;
    }
    

    Pascal :

    var i,j,t,n,w,s,x,y:longint;p:array[1..30000]of longint; 
        a:array[1..30000]of boolean; 
    procedure qsort(l,r:longint); 
    var mid,i,j:longint; 
    begin
      mid:=p[(l+r) div 2]; 
      i:=l;j:=r; 
      repeat
        while p[i]<mid do inc(i); 
        while p[j]>mid do dec(j); 
        if i<=j then begin t:=p[i];p[i]:=p[j];p[j]:=t;i:=i+1;j:=j-1;end; 
      until i>j; 
      if l<j then qsort(l,j); 
      if i<r then qsort(i,r); 
    end; 
    begin
      readln(w); 
      readln(n); 
      for i:=1 to n do readln(p[i]); 
      fillchar(a,sizeof(a),true); 
      qsort(1,n); 
      s:=0;x:=w;y:=0; 
      for i:=n downto 1 do
        begin
          if a[i]=false then continue; 
          x:=x-p[i]; 
          y:=y+1; 
          a[i]:=false; 
          j:=1; 
          repeat
            if a[j] and(p[j]<=x) then begin x:=x-p[j];a[j]:=false;end; 
            j:=j+1; 
            y:=y+1; 
          until (y=2)or(j=n)or(p[j]>x)or(x=0); 
          s:=s+1; 
          x:=w; 
        end; 
      writeln(s); 
    end. 
    
    • 1

    信息

    ID
    254
    时间
    1000ms
    内存
    125MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者