大佬们先看一段丑陋的代码
#include <bits/stdc++.h>
using namespace std;
bool cmp(long long x,long long b){
if(x != b) return x>b;
}
long long n,b,a[30000];
unsigned long long sum=0;
int main(){
cin>>n>>b;
for(int i=1;i<=n;i++) cin>>a[i];
sort(a+1,a+n+1,cmp);
for(int i=1;i<=n;i++){
sum=sum+a[i];
if(sum>=b){
cout<<i;
break;
}
}
return 0;
}
这里注意第3行的cmp函数,将if去掉就可以过
蒟蒻分析认为这是cmp出了问题
然后我将这个cmp复制下来,写了另外一个排序
#include <bits/stdc++.h>
const int N = 1e6+10;
const int M = (1 << 20) + 3;
const int INF = 0x3f3f3f3f;
using namespace std;
bool cmp(long long x,long long b){
if(x != b) return x < b;
}
long long a[N],b;
int main(){
freopen("sort.in","r",stdin);
freopen("sort.out","w",stdout);
int n;
cin >> n;
for(int i = 1; i <= n; i++){
cin >> a[i];
}
sort(a+1,a+n+1,cmp);
for(int i = 1; i <= n; i++){
cout << a[i] <<" ";
}
return 0;
}
#include <bits/stdc++.h>
const int N = 1e6+10;
const int M = (1 << 20) + 3;
const int INF = 0x3f3f3f3f;
using namespace std;
long long a[N];
int main(){
freopen("sort.in","r",stdin);
freopen("sort.ans","w",stdout);
int n;
cin >> n;
for(int i = 1; i <= n; i++){
cin >> a[i];
}
sort(a+1,a+n+1);
for(int i = n; i > 0; i--){
cout << a[i] <<" ";
}
return 0;
}
:loop
give.exe
text.exe
answer.exe
fc sort.out sort.ans
if not errorlevel 1 goto loop
#include <bits/stdc++.h>
const long long N = 1e15+10;
const int M = 1e4+10;
const int INF = 0x3f3f3f3f;
using namespace std;
int main(){
srand(time(0));
freopen("sort.in","w",stdout);
int n = rand() % 1000005;
cout << n << endl;
for(int i = 1; i <= n; i++){
long long a = rand() % N;
cout<< a << " ";
}
return 0;
}
那么从理论上来说,如果cmp是错误的,在对拍的时候应该会报错,但事实上却没有
想过会不会是give是随机化的样例不够完全
于是蒟蒻下载了样例
但是跑出来还是正确的