# [第一波模拟\day1\T2]{分班}(divide.cpp)

## 【输入输出样例】

 divide.in divide.out 1 10 3 1 4 16 11 12 13 10 15 16 17 18 14 4 5 1 186 3 4

## 【数据规模】

 编号 M N A B X G 1 5 1 1<=A<=B<=M 1<=X[i]<=10^5 -1000<=G[i]<=1000 2 <=10 <=3 3 <=100 <=10 4 <=1000 <=50 5 <=10000 <=200 0<=G[i]<=1000 6 7 8 -1000<=G[i]<=1000 9 10

## 【题目分析】

f[i,j] = min {f[i-1,k]+(sum[j]-sum[k])*G[i]} 其中A≤j-k≤ B

sum[i] = ∑(x[i]-Ave)^2

f[i,j] = min {f[i-1,k]-sum[k]*G[i]}+sum[j]*G[i]

Ans=min{f[i,m]}(1<=i<=n)

Room=i;

Student:  for(i=m-b;i<=m-a;i++)//枚举room-1班结束位置

if(f[room-1,i]+G[room]*(sum[m]-sum[i])==Ans)

Stu=m-i;

#include<stdio.h>
#include<string.h>
#define RG register
#define ll long long
const long long oo = 1LL << 60;
#define min(a, b) ((a) < (b) ? (a) : (b))
{
RG int c = getchar(), f = 1;
for(; c < '0' || c > '9'; c = getchar())
if(c == '-') f = -1;
for(x = 0; c >= '0' && c <= '9'; c = getchar()) x = x * 10 + c - '0';
x *= f;
}
int T, m, n, A, B, room, stu;
ll X[10003], G[203], Aver, f[203][10003], ans;
struct Que
{
int head, tail, ck[10000 + 3];
void fresh() {head = 1; tail = 0;}
void pop_top(RG int p)
void push(RG int i, RG ll k, RG int p)
{
while(head <= tail && f[i - 1][ck[tail]] - G[i] * X[ck[tail]] > k) --tail;
ck[++tail] = p;
}
}Q;
int main()
{
freopen("divide.in", "r", stdin);
freopen("divide.out", "w", stdout);
{
Aver = 0;
for(RG int i = 1, x; i <= m; ++i) read(x), X[i] = x, Aver += X[i];
Aver /= (ll)m;
for(RG int i = 1; i <= m; ++i) X[i] -= Aver, X[i] *= X[i], X[i] += X[i - 1];
for(RG int i = 1, g; i <= n; ++i) read(g), G[i] = g;
for(RG int i = 0; i <= n; ++i)
for(RG int j = 0; j <= m; ++j) f[i][j] = oo;
f[0][0] = 0;
for(RG int i = 1; i <= n; ++i)
{
Q.fresh();
for(RG int j = i * A; j <= min(i * B, m); ++j)
{
Q.pop_top(j);
Q.push(i, f[i - 1][j - A] - G[i] * X[j - A], j - A);
f[i][j] = f[i - 1][Q.front()] + G[i] * (X[j] - X[Q.front()]);
}
}
ans = oo;
for(RG int i = 1; i <= n; ++i)
if(ans > f[i][m]) ans = f[i][m], room = i;
if(room == 1) stu = m;
else
for(RG int j = m - B; j <= m - A; ++j)
if(f[room - 1][j] + G[room] * (X[m] - X[j]) == ans) stu = m - j;
printf("%I64d %d %d\n", ans, room, stu);
}
fclose(stdin);
fclose(stdout);
return 0;
}


posted @ 2017-10-19 18:01  keshuqi  阅读(435)  评论(0编辑  收藏  举报