poj 3388 Japanese Puzzle

Description

A brand-new Japanese puzzle is coming from the East to strike the
world-popular Sudoku game and become an international hit. The rules of this
puzzle are kept in secret yet, but the goal is already advertised: given a
square grid n × n, where each square contains a
block with one of k types of pictures, the player has to
rearrange it to get the maximal possible number of equal first rows (two rows
are considered equal if both of them are filled with the same pictures in the
same order). An unnamed insider of the game production company told the press
that the game is about moving blocks of pictures according to some rules, while
the overall set of pictures isn’t changed (no pictures removed, no new pictures
added). She also mentioned that the puzzle is so exciting because there are
thousands of ways to swap two arbitrary pictures on a grid leaving the rest of
the grid intact.

Andy works at the puzzles review magazine, and of course he got interest in
this Japanese news. He realized that the information known so far is enough to
find the number of equal first rows in a puzzle winning position. Now Andy wants
to write a computer program for calculating this number for any given starting
configuration.

For example, if you are given a puzzle which looks this way:














+
? ?
? ? ?

one of the optimal rearrangements could look like














? ?
? ?
+ ?

Input

The first line of the input file contains two
integers n (1 ≤ n ≤ 40 000)
and k (1 ≤ k ≤ 50 000). Each of the
next k lines contains the number of blocks with the
corresponding type of
picture li (li > 0,
sum of all li is
exactlyn2).

Output

Output the maximal possible number of equal first rows at the first line of
the output file. The following n lines must contain contents
of the row which gives the maximum. Each line shows a single number of picture,
in order they must appear. If there are many optimal solutions, any is
acceptable.

Sample Input

3 4
3
3
2
1

Sample Output

2
1
2
3

Source

Northeastern
Europe 2006
, Northern Subregion

二分的好题目,首先直接求不知道怎么求,那就想到枚举每一个,这样就产生了二分的思想,二分出结果后,在对结果输出,这样就不会超时了


#include<map>
#include<set>
#include<stack>
#include<queue>
#include<cmath>
#include<vector>
#include<cstdio>
#include<string>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define inf 0x0f0f0f0f

using namespace std;
const int maxn=100000+10;

int type[maxn],n,m;

bool find_if(int x)
{
int cut=0;
for (int i=1;i<=m;i++)
{
if (cut==n) break;
cut+=type[i]/x;
}
if (cut>=n){
return true;
}
else return false;
}

int find(int x,int y)
{
while (x<y)
{
int mid=x+(y-x)/2;
if (find_if(mid)) x=mid+1;
else y=mid;
}
return x-1;
}

int main()
{
int x;
while(scanf("%d%d",&n,&m)!=EOF)
{
for (int i=1;i<=m;i++)
{
scanf("%d",&type[i]);
}
int ans=find(1,n+1);
printf("%d\n",ans);
int i=1;
while(n)
{
while(type[i]>=ans && n)
{
printf("%d\n",i);
n--;
type[i]-=ans;
}
i++;
}
}
return 0;
}

作者 chensunrise

poj 3388 Japanese Puzzle,布布扣,bubuko.com

时间: 2024-10-14 10:10:36

poj 3388 Japanese Puzzle的相关文章

POJ 3221 Diamond Puzzle

Description A diamond puzzle is played on a tessellated hexagon like the one shown in Figure 1 below. And in this problem the faces produced by the tessellation are identified as they are numbered in the same figure. If two faces share a side, they a

POJ 1651 Multiplication Puzzle (区间dp)

题目大意:对n个数组成的序列取数,规定最两边不能取,每次取一个a[i],得到 a[l] * a[i] * a[r] 的分数(a[l]是a[i]左边的数,a[r]是a[i]右边的数),并把这个数从序列中移走,求n-2次取数后的得分和的最小值 分析:正着确定状态不好做,不如反着来,设dp[l][r]为向区间[l, r]中填满数所得到分数和的最小值,考虑最近一次填数的位置,不难得出: dp[l][r] = fmin(dp[l][m] + dp[m][r] + a[l] * a[m] * a[r]) (

POJ 3678 Katu Puzzle(2-sat 模板题)

题目链接:http://poj.org/problem?id=3678 Description Katu Puzzle is presented as a directed graph G(V, E) with each edge e(a, b) labeled by a boolean operator op (one of AND, OR, XOR) and an integer c (0 ≤ c ≤ 1). One Katu is solvable if one can find each

[2-SAT] poj 3678 Katu Puzzle

题目链接: http://poj.org/problem?id=3678 Katu Puzzle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7888   Accepted: 2888 Description Katu Puzzle is presented as a directed graph G(V, E) with each edge e(a, b) labeled by a boolean operator 

poj 3678 Katu Puzzle(2-sat)

Description Katu Puzzle is presented as a directed graph G(V, E) with each edge e(a, b) labeled by a boolean operator op (one of AND, OR, XOR) and an integer c (0 ≤ c ≤ 1). One Katu is solvable if one can find each vertex Vi a value Xi (0 ≤ Xi ≤ 1) s

POJ 1651 Multiplication Puzzle(区间dp)

Language: Default Multiplication Puzzle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6693   Accepted: 4083 Description The multiplication puzzle is played with a row of cards, each containing a single positive integer. During the move

poj 1651 Multiplication Puzzle【区间DP】

题目链接:http://poj.org/problem?id=1651 题意:初使ans=0,每次消去一个值,位置在pos(pos!=1 && pos !=n) 同时ans+=a[pos-1]*a[pos]*a[pos+1],一直消元素直到最后剩余2个,求方案最小的ans是多少? 代码: #include <stdio.h> #include <ctime> #include <math.h> #include <limits.h> #inc

poj 3221 Diamond Puzzle 反向bfs

分析: 简单的bfs,但要注意因为输入cases很多,对每个初始状态都搜一遍会超时,其实可以从终止状态反向搜一遍,对搜到的每个状态打表存下即可. 代码: //poj 3221 //sep9 #include <iostream> #include <queue> using namespace std; int n; int fac[]={1,1,2,6,24,120,720,5040,40320}; int vis[10000],g[10000]; int pos[8][8];

poj - 1651 - Multiplication Puzzle(dp)

题意:n个数(3 <= N <= 100)的序列,每次取一个数(不可以取最左最右)a[k],这时得到一个权值为a[k]左边的数 * a[k] * a[k]右边的数,问最小权值和. 题目链接:http://poj.org/problem?id=1651 -->>状态:dp[i][j]表示第i个数到第j个数组成的序列的最小权值和. 状态转移方程:dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j] + a[i] * a[k] * a[j]);(枚举最