现在需要最大化他们表现较差一方面的能力,即让
尽可能大,问这个值最大是多少。
进阶:时间复杂度
,空间复杂度![](https://www.nowcoder.com/equation?tex=O(n)%5C)
第一行一个正整数,代表员工数。
接下来行每行两个正整数
,分别用来描述第
个员工的推理和阅读能力。
仅一行一个一位小数用来表示答案。
3 2 2 3 1 1 3
2.0
选择第一个和第二个员工或第一个和第三个时,较差方面的能力都是,选择第二个和第三个时较差方面能力是
。
n = int(input()) a = [[] for i in range(n)] for i in range(n): a[i] = input().split() for i in range(n): for j in range(len(a[i])): a[i][j] = int(a[i][j]) m_min = [] for i in range(n-1): for j in range(i+1, n): m1 = (a[i][0] + a[j][0]) / 2 m2 = (a[i][1] + a[j][1]) / 2 m_min.append(min(m1, m2)) print(max(m_min))
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<pair<int, int>> dat(n); for(int i = 0; i < n; i++) cin >> dat[i].first >> dat[i].second; sort(dat.begin(), dat.end(), [](const pair<int, int> &a, const pair<int, int> &b) { if(a.first == b.first) return a.second < b.second; return a.first < b.first; }); int s = 0, cmax = 0; for(int cur = n - 1; cur >= s; cur--) { int cA = dat[cur].first; int cB = dat[cur].second; for(int i = cur - 1; i >= s; i--) { if(dat[i].second + cB >= dat[i].first + cA) { cmax = max(cmax, dat[i].first + cA); s = i; break; } cmax = max(cmax, dat[i].second + cB); } } cout << cmax/2.0; return 0; }
#include <bits/stdc++.h> typedef long long ll; using namespace std; struct node { int x,y; bool operator<(const node B)const { return x<B.x; } }a[200005]; int n,b[200005],maxv[200005]; bool check(int x) { for(int i=1;i<=n;i++) { /**< 在i之后找到能和a[i].x的和满足要求的位置 */ node temp={x-a[i].x,0}; int t=lower_bound(a+i+1,a+n+1,temp)-a; if(t==n+1) continue;/**< 显然t之后的x都满足要求,那么最大的y是否满足要求? */ if(a[i].y+maxv[t]>=x) return true; } return false; } int main() { ios::sync_with_stdio(0),cin.tie(0); int i,j; cin>>n; for(i=1;i<=n;i++) cin>>a[i].x>>a[i].y; sort(a+1,a+n+1); for(i=n;i>=1;i--)/**< 后缀数组存储最大值 */ maxv[i]=max(maxv[i+1],a[i].y); int l=0,r=1e9,mid,best=0; while(l<=r) { mid=l+r>>1; if(check(mid)) best=mid,l=mid+1; else r=mid-1; } printf("%.1f",best/2.0); return 0; }
# -*- coding:utf-8 -*- # name 岁岁 # 辅助 熊 # time 2022/11/13 23:42 # 先: 0 1 3 7 8 4 9 2 5 6 # 中: 7 3 8 1 9 4 0 5 2 6 # 后: 7 8 3 9 4 1 5 6 2 0 import sys from collections import deque from math import sqrt import heapq sys.setrecursionlimit(100000) from collections import deque import time from functools import lru_cache class node: def __init__(self,x=0,y=0): self.time = x self.w = y class Solution: @classmethod def solution(cls): n = int(input()) li = [] for _ in range(n): a, b = map(int, input().split()) li.append((a,b)) li.sort(key = lambda x: x[0] - x[1]) l = 0 r = n - 1 ans = 0 while l < r: ans = max(ans, min((li[l][0] + li[r][0]) / 2, (li[l][1] + li[r][1]) / 2)) l += 1 r -= 1 print('%.1f'%ans) if __name__ == '__main__': Solution.solution()
为什么这样也行
有没有变态的例子来测试一下。
import java.util.*; public class Main{ public static void main(String[] args){ Scanner scanner=new Scanner(System.in); int n=Integer.parseInt(scanner.nextLine()); PriorityQueue<double[]> q=new PriorityQueue<>((a1,b) -> { double x=(a1[0]+ a1[1]); double y=(b[0]+b[1]); if(x>y){ return -1;} else{return 1;} }); for(int i=0;i<n;i++){ String[] s=scanner.nextLine().split(" "); q.offer(new double []{Integer.parseInt(s[0]),Integer.parseInt(s[1])}); } double p1[]=q.poll(); double p2[]=q.poll(); double min=0; while (q.size()>0){ if(min<Math.min((p1[1]+p2[1])/2.0,(p1[0]+p1[0])/2.0)){ min=Math.min((p1[1]+p2[1])/2.0,(p1[0]+p1[0])/2.0); } p1=p2; p2=q.poll(); } System.out.println(min); } }
#include <iostream> #include <bits/stdc++.h> using namespace std; bool cmp(pair<int,int> &a,pair<int,int> &b){ if (a.first==b.first) return a.second>b.second; return a.first<b.first; } int main() { int n; cin>>n; vector<pair<int,int>> person(n); for (int i=0;i<n;i++){ cin>>person[i].first>>person[i].second; } sort(person.begin(),person.end(),cmp); stack<int> st; st.push(0); int ansx=person[0].first; int ansy=person[0].second; int ans=0; for (int i=1;i<n-1;i++){ ans=max(ans,min(person[st.top()].first+person[i].first,person[st.top()].second+person[i].second)); while (!st.empty()&&person[st.top()].second<person[i].second){ st.pop(); } st.push(i); } int i=n-1; while (!st.empty()){ ans=max(ans,min(person[st.top()].first+person[i].first,person[st.top()].second+person[i].second)); st.pop(); } double fin=ans/2.0; cout<<setiosflags(ios::fixed)<<setprecision(1)<<fin; return 0; }可以按照a排序之后用单调栈维护
n = int(input()) ab = [] for i in range(n): ab.append(list(map(int, input().split()))) res = 0 ab.sort(key = lambda x:abs(x[1]-x[0])) maxa = ab[0][0] maxb = ab[0][1] for a,b in ab: if a>b: res = max((b+maxb)/2,res) else: res = max((a+maxa)/2,res) if a>maxa: maxa = a if b>maxb: maxb = b print(res)根据第一的思路加个python版本
#include <stdio.h> #include <stdlib.h> int cmp (const void * a, const void * b) { int * p1 = (int *) a; int * p2 = (int *) b; return ( *p2+*(p2+1)-*p1-*(p1+1) ); } int main(void){ int n; scanf("%d",&n); int A[n][2]; for (int i=0;i<n;i++){ scanf("%d %d",&A[i][0],&A[i][1]); } qsort(A,n, sizeof(A[0]),cmp); int minone=0; int maxminone=minone; int du,tui; for (int i=0;i<n;i++){ int j=i+1; while(A[i][0]+A[j][0]+A[i][1]+A[j][1]>2*maxminone){ du=(A[i][0]+A[j][0]); tui=(A[i][1]+A[j][1]); if(du<tui){ minone=du; } else{ minone=tui; } if(minone>maxminone){ maxminone=minone; } j++; } } float result=maxminone/2.0; printf("%.1f",result); return 0; }