B. School Marks(典型貪心)
阿新 • • 發佈:2019-03-03
ref 統計 個數 iostream class 位數 tor 分數 ++
鏈接
[https://codeforces.com/contest/540/problem/B]
題意
某個人有n門成績,k門已知,剩下的他可以個瞎改,但有個要求,最後分數和不超過x,且每門成績不超過p,中位數不小於y
分析
直接統計小於y的門數,總和最小就是y為中位數,
只需判斷小於y的門數,若大於n/2,中位數就小於y了不可行,
還有就是總和超過x也不行,否則就可以構造,前n/2補1,後n/2補y
但須註意一個數據
9 7 2 14 1
2 2 2 1 1 2 2
代碼
#include<iostream> #include<algorithm> #include<vector> #include<queue> #include<string.h> using namespace std; #define ll long long const int N=1e3+10; int n,k,p,x,y; int a[N]; int main(){ int n; //freopen("in.txt","r",stdin); while(cin>>n>>k>>p>>x>>y){ // memset(sum,0,sizeof(sum)); int sum=0,cnt=0; for(int i=1;i<=k;i++) { cin>>a[i]; if(a[i]<y) cnt++; sum+=a[i]; } //cout<<cnt<<endl; if(cnt>n/2){ cout<<-1<<endl; continue; } else{ x-=sum; int q=cnt; cnt=n/2-cnt; cnt=min(n-k,cnt);//上述特殊數據的處理 if(x<((n-(cnt+k))*y+cnt)){ cout<<-1<<endl; continue; } else{ // cout<<cnt<<' '<<k<<endl; for(int i=1;i<=cnt;i++) cout<<1<<' '; for(int i=1;i<=n-(cnt+k);i++) cout<<y<<' '; cout<<endl; } } } return 0; }
B. School Marks(典型貪心)