posts - 12,  comments - 40,  trackbacks - 0

http://acm.pku.edu.cn/JudgeOnline/problem?id=3139
Balancing the Scale
去年上海赛区的B题,比赛没有做出来

注意隐含条件:每个数字不大于1024
1、有这个条件后,则表达式之和不超过1024 * 10,这样就可以开一个10240的表,记录得到当前值的所有4字组合(用16位2进制表示)

2、对于10240内的每两个值进行配对(只要两个4字组合中8个数字没有重复的数字即可),每配一个对,则说明这个8字组合的分解方法数目加1(这个方法数目记录在一个数组中,也用16位2进制表示8字组合)
3、对于每两个互补的8字组合,他们的分解方法数目相乘然后求和即是答案
posted on 2007-08-09 22:34 LSM 阅读(878) 评论(1)  编辑 收藏 引用 所属分类: 其他

FeedBack:
# re: Poj 3139-Balancing the Scale[未登录]
2008-12-21 08:41 | feng
#include <iostream>
#include <algorithm>
#include <cstring>

using namespace std;

const int MAX_VAL = 1024 * 4 + 1023 * 3 + 1022 * 2 + 1021;
const int UNI_16 = 0xFFFF;

struct Node {
int comb;
int cnt;
Node* next;

Node(int c, Node* n) {
comb = c;
cnt = 1;
next = n;
}
};

Node* g_value[MAX_VAL + 1];
int g_num[16];
int g_cnt8[UNI_16];

void addValue(int value, int comb);
bool card4(int set, int* num);
void clear();
void getAllCnts8();
void getAllValues4();

void addValue(int value, int comb) {
Node*& head = g_value[value];
if (head != NULL && head->comb == comb) {
head->cnt++;
}
else {
Node* node = new Node(comb, head);
head = node;
}
}

//If the cardinality of the set is 4,
//then put the 4 numbers in to array num and return true.
bool card4(int set, int* num) {
int card = 0;
int mask = 1;
for (int i = 0; i < 16; i++) {
if (mask & set) {
if (card == 4) {
card = 0;
break;
}
num[card] = g_num[i];
card++;
}
mask <<= 1;
}
return card == 4;
}

void clear() {//Free all the linked lists.
for (int i = 0; i <= MAX_VAL; i++) {
if (g_value[i] != NULL) {
Node* p = g_value[i];
while (p != NULL) {
Node* pre = p;
p = p->next;
delete pre;
}
g_value[i] = NULL;
}
}
}

void getAllCnts8() {
memset(g_cnt8, 0, sizeof(g_cnt8));
for (int val = 0; val <= MAX_VAL; val++) {
if (g_value[val] != NULL) {
for (Node* i = g_value[val]; i != NULL; i = i->next) {
for (Node* j = i->next; j != NULL; j = j->next) {
if ((i->comb & j->comb) == 0) {
g_cnt8[i->comb | j->comb] += i->cnt * j->cnt;
}
}
}
}
}
}

void getAllValues4() {
sort(g_num, g_num + 16);//Must sort, or "next_permutation()" won't work.
int num[4];
for (int comb = 0xF; comb <= 0xF000; comb++) {
if (card4(comb, num)) {
do {
int value = num[0] * 4 + num[1] * 3 + num[2] * 2 + num[3];
addValue(value, comb);
} while (next_permutation(num, num + 4));
}
}
}

bool input() {
bool hasNext = false;
cin >> g_num[0];
if (g_num[0] != 0) {
hasNext = true;
for (int i = 1; i < 16; i++) {
cin >> g_num[i];
}
}
return hasNext;
}

int solve() {
getAllValues4();
getAllCnts8();
int cnt = 0;
for (int comb8 = 0xFF; comb8 <= 0xFF00; comb8++) {
cnt += g_cnt8[comb8] * g_cnt8[comb8 ^ UNI_16];
}
return cnt / 2;
}

int main() {
memset(g_value, 0, sizeof(g_value));
int i = 1;
while (input()) {
cout << "Case " << i << ": " << solve() << endl;
clear();
i++;
}
return 0;
}

  回复  更多评论
  

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2007年8月>
2930311234
567891011
12131415161718
19202122232425
2627282930311
2345678

常用链接

留言簿(4)

随笔分类

随笔档案

牛牛 ACM/ICPC

最新随笔

搜索

  •  

最新随笔

最新评论

阅读排行榜

评论排行榜