vva 10558 A Brief Gerrymander(dp 메모리 검색)
Problem E: A Brief Gerrymander
The evil ruling party, the Liberatives, are redistributing the electoral regions (ridings) in your city, and are nefariously attempting to pack certain opposition-friendly neighborhoods into as few ridings as possible. If this plan succeeds, it will be the end of democracy as we know it! As a card-carrying member of the noble, compassionate Conservals (the main opposition party), you must foil their devious plot and safeguard the future of our country.
Under the new plan, the city will be divided up into a grid of rectangles by selecting certain major streets and avenues as boundaries. All streets and avenues stretch straight across the city, and are numbered starting from the southwest corner of the city. The city is bounded by four roads: 1st Street (west edge), 100th Street (east edge), 1st Avenue (south edge), 100th Avenue (north edge). Clearly these four roads must represent district boundaries; however, only a subset of the streets and avenues in between will divide districts. The Liberatives have already been able to fix the North-South (street) boundaries; however, they have been forced to allow the Conservals to select the East-West (avenue) boundaries.
You know the location of all the opposition-friendly neighborhoods, which vote strongly Conserval. A neighborhood is exactly one block between adjacent streets and avenues. For instance, one neighborhood might lie between 47th and 48th Street and 67th and 68th Avenue. Place the avenue boundaries so that as many ridings as possible contain at least one opposition-friendly neighborhood.
Input consists of multiple cases, each describing a city. The first line will give N, the number of Conserval neighborhoods in the city; the following N lines will contain the street and avenue number of the southwest corner of each neighborhood. The next line will contain S, the number of street boundaries, and the S street numbers that they lie on, in increasing order. The final line will consist of A (at least 2), the number of avenue boundaries you must place. Input will be terminated by a line containing -1.
For each case, output a single line giving A followed by A distinct avenue numbers for the optimal redistriubution, in increasing order. If there are multiple such solutions, any one will do.
Sample Input
2
49 49
50 50
2 1 100
3
-1
Sample Output
3 1 50 100
제목: 제목의 뜻을 도무지 이해할 수가 없어서 결국 제목을 보러 갔어요...그다음에 거의 참고를 했어요.http://www.cnblogs.com/scau20110726/archive/2012/10/04/2711436.html
사고방식: 이 문제를 예처리할 때 관건은 s[i][j]수 그룹을 구하고 i에서 j 횡선에 몇 개의 구역이 있을 수 있음을 나타낸다.그 다음은 간단한 기억화 검색이야.dp[i][j]는 제i조 횡선을 표시하고 제j칼을 잘라야 한다는 뜻으로 제i조 횡선을 자르든 말든 고려하면 된다.
코드:
#include <stdio.h>
#include <string.h>
const int MAXN = 110;
int n, map[MAXN][MAXN], m, cut[MAXN], A;
int only[MAXN][MAXN], f[MAXN][MAXN], s[MAXN][MAXN];
int dp[MAXN][MAXN], path[MAXN][MAXN], vis[MAXN][MAXN];
void init() {
int a, b, i, j, k;
memset(dp, 0, sizeof(dp));
memset(vis, 0, sizeof(vis));
memset(map, 0, sizeof(map));
for (i = 1; i <= n; i ++) {
scanf("%d%d", &a, &b);
map[b][a] = 1;
}
scanf("%d", &m);
for (i = 1; i <= m; i ++) {
scanf("%d", &cut[i]);
}
scanf("%d", &A);
for (k = 1; k < m; k ++)
for (i = 1; i < 100; i ++) {
only[i][k] = f[i][k] = 0;
for(j = cut[k]; j < cut[k+1]; j ++) {
if (map[i][j] == 1) {
only[i][k] = 1;
break;
}
}
f[i][k] = only[i][k];
f[i][k] += f[i-1][k];
}
for (i = 1; i < 100; i ++)
for (j = i + 1; j <= 100; j ++)
{
s[i][j] = 0;
for (k = 1; k < m; k ++)
if (f[j - 1][k] - f[i][k] + only[i][k])
s[i][j] ++;
}
}
int DP(int i, int j) {
int k, ans;
if (vis[i][j])
return dp[i][j];
vis[i][j] = 1;
if (j == 0) {
return dp[i][j] = s[i][100];
}
for (k = i + 1; k < 100; k ++) {
if (100 - k - 1 < j - 1)
break;
ans = DP(k, j - 1);
if (ans + s[i][k] > dp[i][j]) {
dp[i][j] = ans + s[i][k];
path[i][j] = k;
}
}
return dp[i][j];
}
void print(int i, int j) {
if (j <= 0) return;
printf(" %d", path[i][j]);
print(path[i][j], j - 1);
}
int main() {
while (~scanf("%d", &n) && n != -1) {
init();
DP(1, A - 2);
printf("%d", A);
printf(" 1");
print(1, A - 2);
printf(" 100
");
}
return 0;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.