2 条题解

  • 0
    @ 2025-10-8 17:01:42
    #include<bits/stdc++.h>
    using namespace std;
    typedef long long LL;
    const int N=1e5+10;
    LL l[N], r[N]; int m, n; LL F; 
    struct node{LL s, q;} a[N];
    bool cmp(node n1, node n2){
    	return n1.s < n2.s;
    }
    void work(){
    	sort(a+1, a+n+1, cmp);
    	int t=m/2; LL sum=0;
    	priority_queue<LL> Q1;
    	for(int i=1; i<=n; i++){
    		if(i<=t){
                Q1.push(a[i].q);
                sum += a[i].q;
                continue;
            }
            l[i] = sum;
            if(a[i].q < Q1.top()){
                sum -= Q1.top();
                Q1.pop();
                Q1.push(a[i].q);
                sum += a[i].q;
            }
    	}
    	sum=0;
    	priority_queue<LL> Q2;
    	for(int i=n; i>=1; i--){
    		if(i > n-t){
                Q2.push(a[i].q);
                sum += a[i].q;
                continue;
            }
            r[i] = sum;
            if(a[i].q < Q2.top()){
                sum -= Q2.top();
                Q2.pop();
                Q2.push(a[i].q);
                sum += a[i].q;
            }
    	}
    	for(int i=n-t; i > t; i--){
    		if(l[i] + a[i].q + r[i] <= F){
    			printf("%lld\n", a[i].s);
    			return ;
    		}
    	}
    	printf("-1\n");
    	return ;
    }
    int main(){
    	scanf("%d%d%lld", &m, &n, &F);
    	for(int i=1; i<=n; i++) scanf("%lld%lld", &a[i].s, &a[i].q);
    	work();
    	return 0;
    }
    
    • 0
      @ 2025-10-8 17:01:31

      by hansang:

      #include<bits/stdc++.h>
      using namespace std;
      typedef long long LL;
      const int N=1e5+10;
      LL l[N], r[N]; int m, n; LL F;
      struct node{LL s, q;} a[N];
      bool cmp(node n1, node n2){
      return n1.s<n2.s;
      }
      void work(){
      sort(a+1, a+n+1, cmp);
      int t=m/2; LL sum=0;
      priority_queue<LL> Q1;
      for(int i=1; i<=n; i++){
      if(i<=t){
      Q1.push(a[i].q);
      sum+=a[i].q;
      continue;
      }
      l[i]=sum;
      if(a[i].q<Q1.top()){
      sum-=Q1.top();
      Q1.pop();
      Q1.push(a[i].q);
      sum+=a[i].q;
      }
      }
      sum=0;
      priority_queue<LL> Q2;
      for(int i=n; i>=1; i--){
      if(i>n-t){
      Q2.push(a[i].q);
      sum+=a[i].q;
      continue;
      }
      r[i]=sum;
      if(a[i].q<Q2.top()){
      sum-=Q2.top();
      Q2.pop();
      Q2.push(a[i].q);
      sum+=a[i].q;
      }
      }
      for(int i=n-t; i>t; i--){
      if(l[i]+a[i].q+r[i]<=F){
      printf("%lld\n", a[i].s);
      return ;
      }
      }
      printf("-1\n");
      return ;
      }
      int main(){
      scanf("%d%d%lld", &m, &n, &F);
      for(int i=1; i<=n; i++) scanf("%lld%lld", &a[i].s, &a[i].q);
      work();
      return 0;
      }

      • 1

      *【贪心】中位数最大[USACO04MAR] Financial Aid

      信息

      ID
      2608
      时间
      1000ms
      内存
      128MiB
      难度
      6
      标签
      递交数
      19
      已通过
      10
      上传者