Load Balancing

给出每个学生的学分。   将学生按学分分成四组,使得sigma (sumi-n/4)最小。         算法:   折半枚举

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <sstream>
#include <iomanip>
using namespace std;
typedef long long LL;
const int INF=0x4fffffff;
const int EXP=1e-;
const int MS=; int num[MS]; // 学分为i的人数
int sum[MS]; // 学分<=i的人数 int main()
{
int T,kase=,n,x;
scanf("%d",&T);
while(T--)
{
memset(num,,sizeof(num));
memset(sum,,sizeof(sum));
scanf("%d",&n);
for(int i=;i<n;i++)
{
scanf("%d",&x);
num[x]++;
}
sum[]=num[]; // note: sum[0]!=0;
for(int i=;i<MS;i++)
sum[i]=sum[i-]+num[i];
// 折半枚举
int ansa,ansb,ansc;
int ta,tc;
double sum1,sum2,sum3=INF;
double ave=double(n)/4.0;
for(int b=;b<MS-;b++)
{
sum1=INF*1.0;
sum2=INF*1.0;
for(int a=;a<b;a++)
{
double t=fabs(double(sum[a])-ave)+fabs(double(sum[b]-sum[a])-ave);
if(t<sum1)
{
sum1=t;
ta=a;
}
} for(int c=b+;c<MS-;c++)
{
double t=fabs(double(sum[c]-sum[b])-ave)+fabs(double(sum[MS-]-sum[c])-ave);
if(t<sum2)
{
sum2=t;
tc=c;
}
}
if(sum1+sum2<sum3)
{
sum3=sum1+sum2;
ansa=ta;
ansb=b;
ansc=tc;
}
}
printf("Case %d: %d %d %d\n",kase++,ansa,ansb,ansc);
}
return ;
}

Load Balancing 折半枚举大法好啊的更多相关文章

  1. UVA 12904 Load Balancing 暴力

    Load Balancing Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/vi ...

  2. CSU OJ PID=1514: Packs 超大背包问题,折半枚举+二分查找。

    1514: Packs Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 61  Solved: 4[Submit][Status][Web Board] ...

  3. 【架构】How To Use HAProxy to Set Up MySQL Load Balancing

    How To Use HAProxy to Set Up MySQL Load Balancing Dec  2, 2013 MySQL, Scaling, Server Optimization U ...

  4. CF# Educational Codeforces Round 3 C. Load Balancing

    C. Load Balancing time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  5. Codeforces Educational Codeforces Round 3 C. Load Balancing 贪心

    C. Load Balancing 题目连接: http://www.codeforces.com/contest/609/problem/C Description In the school co ...

  6. [zz] pgpool-II load balancing from FAQ

    It seems my pgpool-II does not do load balancing. Why? First of all, pgpool-II' load balancing is &q ...

  7. How Network Load Balancing Technology Works--reference

    http://technet.microsoft.com/en-us/library/cc756878(v=ws.10).aspx In this section Network Load Balan ...

  8. Network Load Balancing Technical Overview--reference

    http://technet.microsoft.com/en-us/library/bb742455.aspx Abstract Network Load Balancing, a clusteri ...

  9. NYOJ 1091 超大01背包(折半枚举)

    这道题乍一看是普通的01背包,最最基础的,但是仔细一看数据,发现普通的根本没法做,仔细观察数组发现n比较小,利用这个特点将它划分为前半部分和后半部分这样就好了,当时在网上找题解,找不到,后来在挑战程序 ...

随机推荐

  1. 第二百一十二天 how can I 坚持

    在家待了一天,过个周六日也就这样,时间.感觉好堕落. 下午心情特烦闷.好想结婚.为什么不认输,为什么会这样.这到底是一种什么心态. 哎.不懂自己. 睡觉. fordream.

  2. Linux查找文件夹名

    @(编程) find / -type d -name filename type的类型 -type c File is of type c: b block (buffered) special c ...

  3. 关于缺省路由传递问题的探讨(下)[ip default-network、ip default-gateway等]

    之前文章介绍的是没有路由协议的环境下,那么在有路由协议的环境下: ip default-network IGRP/EIGRP: IP Default-Network所指定的网络必须在EIGRP进程中通 ...

  4. 正则表达式匹配中文字符串的文章URL

    http://www.cnblogs.com/yitian/archive/2008/11/14/1333569.html

  5. AJAX的工作原理及其优缺点

    1.什么是AJAX?AJAX全称为“Asynchronous JavaScript and XML”(异步JavaScript和XML),是一种创建交互式网页应用的网页开发技术.它使用:使用XHTML ...

  6. 修改SVN账户密码的方法

        Case1:     在Eclipse 使用SVN 的过程中大多数人往往习惯把访问SVN 的用户名密码自动保存起来以便下次自动使用,不要再次手工输入,但是有些时候需要变更密码或者用户名,这时候 ...

  7. 3.依赖倒置原则(Dependence Inversion Principle)

    1.定义 高层模块不应该依赖于低层模块,二者都应该依赖于抽象:抽象不应该依赖细节:细节应该依赖抽象. 2.定义解读 依赖倒置原则在程序编码中经常运用,其核心思想就是面向接口编程,高层模块不应该依赖低层 ...

  8. 高性能的JavaScript -- 读书笔记

    高性能的JavaScript 一.      加载和运行 将脚本放在底部 脚本下载解析执行时,页面已经加载完成并显示在用户面前 成组脚本 减少外部脚本文件数量,整合成一个文件 延迟脚本 动态脚本元素 ...

  9. cocos2d制作动态光晕效果基础——blendFunc

    转自:http://www.2cto.com/kf/201207/144191.html 最近的项目要求动态光晕的效果. 何谓动态光晕?之前不知道别人怎么称呼这个效果, 不过在我看来,“动态光晕”这个 ...

  10. CMSIS-DAP调试器

    http://www.keil.com/support/man/docs/dapdebug/dapdebug_introduction.htm CMSIS-DAP is the interface f ...