UVA 1382 Distant Galaxy

思路和书上写的一样的,不再赘述了,在北大的网站上提交会超时,uva上和杭电上面都可以过,看来poj上的测试数据比较凶残。

#include <stdio.h>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
using namespace std;

vector<int> all_rows;
vector<int> all_cols;
typedef int ROW_VALUE;
typedef int NODE_NUMBER;
typedef int COL_VALUE;
map<ROW_VALUE, vector<COL_VALUE> > m_row;
map<COL_VALUE, vector<ROW_VALUE> > m_col;

int cmp(const void *a, const void *b){
	int *pa = (int*)a;
	int *pb = (int*)b;

	return *pa-*pb;
}

void func(int n){
	static int case_n = 1;

	if(all_rows.size()<=2 || all_cols.size()<=2){
		printf("Case %d: %d\n", case_n++, n);
		return;
	}

	int i;
	int row_index1, row_index2, col_index1, col_index2, col_index;
	int row_value_min, row_value_max, col_value;
	int left[105], on[105], on2[105];
	int max_node_num, cur_node_num, max_left;
	map<COL_VALUE, NODE_NUMBER> record;

	qsort(&(all_rows[0]), all_rows.size(), sizeof(int), cmp);
	qsort(&(all_cols[0]), all_cols.size(), sizeof(int), cmp);

	max_node_num = 0;
	for(row_index1=0; row_index1<all_rows.size()-1; row_index1++){
		for(row_index2=row_index1+1; row_index2<all_rows.size(); row_index2++){
			//更新on, on2
			row_value_min = all_rows[row_index1];
			row_value_max = all_rows[row_index2];
			for(col_index=0; col_index<all_cols.size(); col_index++){
				col_value = all_cols[col_index];
				on2[col_index] = on[col_index] = 0;
				for(i=0; i<m_col[col_value].size(); i++){
					if(m_col[col_value][i]>=row_value_min && m_col[col_value][i]<=row_value_max)
						on2[col_index]++;
					if(m_col[col_value][i]>row_value_min && m_col[col_value][i]<row_value_max)
						on[col_index]++;
				}
			}

			//更新left
			record.clear();

			for(i=0; i<all_cols.size(); i++){
				record[all_cols[i]] = 0;
			}
			for(i=0; i<m_row[all_rows[row_index1]].size(); i++){
				col_value = m_row[all_rows[row_index1]][i];
				record[col_value]++;
			}
			for(i=0; i<m_row[all_rows[row_index2]].size(); i++){
				col_value = m_row[all_rows[row_index2]][i];
				record[col_value]++;
			}

			for(i=0; i<all_cols.size(); i++){
				if(0 == i){
					left[i] = 0;
					continue;
				}
				else{
					left[i] = left[i-1]+record[all_cols[i-1]];
				}
			}

			//开始迭代不同的列
			max_left = on[0]-left[0];
			for(col_index2=1; col_index2<all_cols.size(); col_index2++){
				cur_node_num = on2[col_index2]+left[col_index2]+max_left;
				if(cur_node_num > max_node_num)
					max_node_num = cur_node_num;
				max_left = max_left>on[col_index2]-left[col_index2] ? max_left : on[col_index2]-left[col_index2];
			}
		}
	}
	printf("Case %d: %d\n", case_n++, max_node_num);
}

int main(void){
	int n, i;
	int row, col;
	set<int> s1, s2;

	//freopen("input.dat", "r", stdin);
	while(scanf("%d", &n), n){
		all_rows.clear(); s1.clear();
		all_cols.clear(); s2.clear();
		m_row.clear(); m_col.clear();
		for(i=0; i<n; i++){
			scanf("%d %d", &row, &col);
			if(s1.find(row) == s1.end()){
				s1.insert(row);
				all_rows.push_back(row);
			}
			if(s2.find(col) == s2.end()){
				s2.insert(col);
				all_cols.push_back(col);
			}
			m_row[row].push_back(col);
			m_col[col].push_back(row);
		}
		func(n);
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值