1 条题解
-
0
C++ :
#include <cstdlib> #include <cstdio> #include <iostream> #include <algorithm> #include <vector> using namespace std; int main() { int max,n,s=0; vector<int>a;a.push_back(0); scanf("%d%d",&max,&n); for(int i=1;i<=n;i++) { int in; scanf("%d",&in); s++; a.push_back(in); } sort(a.begin(),a.end()); while(a.size()>2) { if(a.size()>=3) { if(a[1]+a[2]>max) break; } for(int i=a.size()-1;i>=2;i--) { if(a[i]+a[1]<=max) { s--; a.erase(a.begin()+1); a.erase(a.begin()+i-1); break; } } } printf("%d",s); return 0; }
Pascal :
program jco41; type tlist=array[1..30005]of longint; var f:tlist; n,w:longint; i,j,k,l:longint; ans:longint; head,tail:longint; procedure qsort(var a:tlist;n:longint); procedure sort(l,r:longint); var i,j,x,y: longint; begin i:=l; j:=r; x:=a[(l+r) div 2]; repeat while a[i]<x do inc(i); while x<a[j] do dec(j); if not(i>j) then begin y:=a[i]; a[i]:=a[j]; a[j]:=y; inc(i); dec(j); end; until i>j; if l<j then sort(l,j); if i<r then sort(i,r); end; begin sort(1,n); end; begin readln(w); readln(n); for i:=1 to n do begin readln(f[i]); end; qsort(f,n); head:=1; tail:=n; ans:=0; repeat if f[head]+f[tail]>w then begin inc(ans); dec(tail); end else begin inc(ans); inc(head); dec(tail); end; until (head>tail); writeln(ans); end.
- 1
信息
- ID
- 719
- 时间
- 1000ms
- 内存
- 128MiB
- 难度
- (无)
- 标签
- 递交数
- 0
- 已通过
- 0
- 上传者