Luogu 1314(二分答案)(NOIP 2011)

传送门

NOIP 2011 D2T2

题解:

二分W的值,每次O(n+m)计算检验结果(前缀和),如果S>Y则缩小W,否则增大W。

P.S.1A,贼稳!

#include
#include
#include
#include
using namespace std;
const int MAXN=2e5+4;
typedef long long ll;
int n,m,L[MAXN],R[MAXN],w[MAXN],v[MAXN],l=0,r=0;
ll S,ans=1e18,sumw[MAXN]={0},sumv[MAXN]={0};
inline bool cck(int mid) {
	ll res=0;
	for (register int i=1;i<=n;++i) sumw[i]=sumw[i-1]+(w[i]>=mid),sumv[i]=sumv[i-1]+v[i]*(w[i]>=mid);
	for (register int i=0;iS;
}
int main() {
//	freopen("P1314.in","r",stdin);
	scanf("%d%d%lld",&n,&m,&S);
	for (register int i=1;i<=n;++i) scanf("%d%d",&w[i],&v[i]),r=max(r,w[i]);
	for (register int i=0;i>1;
		if (cck(mid)) l=mid+1;
		else r=mid-1;
	}
	printf("%lld\n",ans);
	return 0;
}




你可能感兴趣的:(杂题)