Hack我自己
查看原帖
Hack我自己
370599
why_cb楼主2023/2/14 18:10

本题目虽然已经有了很多 hack ,

但是很不幸,我又 hack 自己的 AC 代码


输入:

5 2 6
1 1
1 1
3 3
4 2
4 2

正确输出:

4

错误代码输出:

3

错误代码:

#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<string.h>
#include<queue>
#include<stack>
#include<vector>
#include<map>
#include<set> 
using namespace std;

#define fi first
#define se second
#define pb push_back
typedef double db;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<int,ll> pil;
typedef pair<ll,int> pli;
typedef pair<ll,ll> pll;
const ll N=5e4+86;

ll n,k,m,cnt;
struct Cow
{
	ll p,c;
}a[N];
priority_queue<ll> pq;
bool cmp1(Cow tmp1,Cow tmp2)
{
	return tmp1.c<tmp2.c;
}
bool cmp2(Cow tmp1,Cow tmp2)
{
	return tmp1.p<tmp2.p;
}

template<typename T>
inline void read(T &x)
{
	T k=1;char ch=getchar();x=0;
	while(ch<'0'||ch>'9'){if(ch=='-') k=-1;ch=getchar();}
	while(ch>='0'&&ch<='9')x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
	x*=k;
}

int main()
{
	read(n),read(k),read(m);
	for(int i=1;i<=n;i++)
		read(a[i].p),read(a[i].c);
	sort(a+1,a+1+n,cmp1);
	if(k<n) sort(a+k+1,a+1+n,cmp2);
	for(int i=1;i<=n;i++)
	{
		if(k)
		{
			if(m<a[i].c) break;
			k--,cnt++,m-=a[i].c;
			pq.push(-a[i].p+a[i].c);
		}
		else if(m>=a[i].p)
		{
			cnt++,m-=a[i].p;
			if(!pq.empty()&&a[i].p-a[i].c+pq.top()>0) 
				m+=a[i].p-a[i].c+pq.top(),pq.pop(),pq.push(-a[i].p+a[i].c);
		}
		else if(!pq.empty()&&m+a[i].p-a[i].c+pq.top()>=a[i].p)
		{
			cnt++,m+=-a[i].c+pq.top();
			pq.pop(),pq.push(-a[i].p+a[i].c);
		}
	}
	printf("%lld\n",cnt);
	return 0;
}

AC记录

2023/2/14 18:10
加载中...