有100名学生的成绩,每个班级50人,要求两个班级的平均分越接近越好,如何分配两个班级?
用C++实现。
#include <iostream>
#include <cstring>
#include <ctime>
const int N=5000;
const int M=100;
const int K=50;
int A[N+1][M+1][K+1];
int B[N+1];
int Score[M+1];
using namespace std;
int main(int argc,char **argv){
srand(time(NULL));
Score[0]=0;
memset(A,0,sizeof(int)*(N+1)*(M+1)*(K+1));
for(int i=1;i<=M;i++)
Score[i]=rand()%41+60;
int k=0;
for(int i=1;i<=M;i++){
for(int j=0;j<=N;j++)
if(j+Score[i]<=2500 && k<=50 && A[j][i][k]+Score[i]<A[j][i][k]-Score[i])
A[j+Score[i]][i][k++]=A[j][i][k]+Score[i];
else
A[j][i][k]-=Score[i];
}
}
这是我的代码,麻烦之指出错误。应该是在状态转移方程那里。
This is not a standard backpack problem, and it is quite difficult to put it on. It is better to deal with it as a normal dp.
I don’t understand your program very well. I wrote a java program based on my understanding.
The首先求出 所有学生的 总成绩 / 2, 设为half, 此题 即为找出50个学生, 其总成绩最接近此 half值
.dp table is
Set<Integer> dp[][] = new HashSet[N / 2 + 1][half + 1]; 对每一个dp[i][j], 即选出i个学生, 其总成绩最接近j; 每一个Set包含 此 i个学生在score数组中的下标
.Only the simplest test has been done, and there is no guarantee that the program is completely correct.
How to use one-dimensional array to save memory: