标签:b. school marks cf #301 div. 2
题意:有n个数,先给出p个数,现在要求另外n-p个数,使得这n个数的中位数大于等于y,并且n个数之和不大于x。没有方案就输出-1.
代码:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <string> #include <map> #include <stack> #include <vector> #include <set> #include <queue> #pragma comment (linker,"/STACK:102400000,102400000") #define maxn 1005 #define MAXN 2005 #define mod 1000000009 #define INF 0x3f3f3f3f #define pi acos(-1.0) #define eps 1e-6 #define lson rt<<1,l,mid #define rson rt<<1|1,mid+1,r #define FRE(i,a,b) for(i = a; i <= b; i++) #define FREE(i,a,b) for(i = a; i >= b; i--) #define FRL(i,a,b) for(i = a; i < b; i++) #define FRLL(i,a,b) for(i = a; i > b; i--) #define mem(t, v) memset ((t) , v, sizeof(t)) #define sf(n) scanf("%d", &n) #define sff(a,b) scanf("%d %d", &a, &b) #define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c) #define pf printf #define DBG pf("Hi\n") typedef long long ll; using namespace std; int n,p,k,x,y; int a[maxn]; int ans[maxn]; int main() { // freopen("C:/Users/asus1/Desktop/IN.txt","r",stdin); int i,j; while (~scanf("%d%d%d%d%d",&n,&p,&k,&x,&y)) { int s=0,l=0,r=0,num=0,cnt=0; for (i=0;i<p;i++) { scanf("%d",&a[i]); s+=a[i]; if (a[i]<y) l++; //记录比y小的数有多少 else if (a[i]>=y) r++;//记录大于等于y的数有多少 } if (l>=n/2+1){ //如果比y小的数超过了一半,那么中位数不可能达到y了 printf("-1\n"); continue; } int xx=n/2+1-r; while (xx>0){ //填充右边,尽量填y使得和最小 ans[cnt++]=y; xx--;s+=y; } xx=n-r-l-cnt; while (xx>0) //填充左边,填1使和尽量小 { ans[cnt++]=1; xx--;s++; } if (s>x){ //和超过x输出-1 printf("-1\n"); continue; } printf("%d",ans[0]); for (i=1;i<cnt;i++) printf(" %d",ans[i]); printf("\n"); } return 0; }
B. School Marks (CF #301 (Div. 2))
标签:b. school marks cf #301 div. 2
原文地址:http://blog.csdn.net/u014422052/article/details/45417729