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 折半枚举大法好啊的更多相关文章
- UVA 12904 Load Balancing 暴力
Load Balancing Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/vi ...
- CSU OJ PID=1514: Packs 超大背包问题,折半枚举+二分查找。
1514: Packs Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 61 Solved: 4[Submit][Status][Web Board] ...
- 【架构】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 ...
- 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 ...
- Codeforces Educational Codeforces Round 3 C. Load Balancing 贪心
C. Load Balancing 题目连接: http://www.codeforces.com/contest/609/problem/C Description In the school co ...
- [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 ...
- 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 ...
- Network Load Balancing Technical Overview--reference
http://technet.microsoft.com/en-us/library/bb742455.aspx Abstract Network Load Balancing, a clusteri ...
- NYOJ 1091 超大01背包(折半枚举)
这道题乍一看是普通的01背包,最最基础的,但是仔细一看数据,发现普通的根本没法做,仔细观察数组发现n比较小,利用这个特点将它划分为前半部分和后半部分这样就好了,当时在网上找题解,找不到,后来在挑战程序 ...
随机推荐
- F2063 Could not compile used unit 'tt.pas'
install packge error F2063 Could not compile used unit 'tt.pas' 有可能是工程的pas文件相对路径不对.在工程管理看是否能打开文件,如果打 ...
- 使用「max-height」实现自适应高度
.tab-content{ max-height: 0; overflow: hidden; -webkit-transition: max-height .8s; -moz-transition: ...
- Codeforces Round #368 (Div. 2) B. Bakery (模拟)
Bakery 题目链接: http://codeforces.com/contest/707/problem/B Description Masha wants to open her own bak ...
- compiled python files
[compiled python files] As an important speed-up of the start-up time for short programs that use a ...
- class DelegatingFilterProxy
/** * Proxy for a standard Servlet Filter, delegating to a Spring-managed bean that * implements the ...
- hdu-4753-Fishhead’s Little Game-记忆化搜索
网赛的一道题目,当时没做出来. 由题意可知,最多只有12条边未知. 所以最多只有(1<<12)种状态. 所以记忆化搜索这种状态下,枚举添加任意一条边之后的状态的最优值. #include& ...
- hdoj 1729 Stone Games(SG函数)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1729 看了题目感觉像Nim,但是有范围限制,有点不知道SG函数该怎么写 看了题解,最后才明白该怎么去理 ...
- android ListView进阶
ListView 1.在android 开发中很多时候都要用到ListView的这个控件的,但用这个控件的时候会遇到一些问题,如在ListView中有Button按钮,就需要将按钮的监听事件给分离出来 ...
- 文件字符读写函数fscanf()和 fgets() 比较
一. 文件格式化读入函数 fscanf() int fscanf(文件指针,格式化字符串,输入列表); 返回值: 整形,输入列表中定义字符串的个数. 1, 例如读取字符串: char str1[ ...
- flash 定义主舞台窗口大小
1:[SWF(width=100 height=100)] 写在主类上面2:设置stageScaleMode属性为false;