打表+dp
问题 G: Strange Bank
时间限制: 1 Sec 内存限制: 128 MB
[提交] [状态]
题目描述
To make it difficult to withdraw money, a certain bank allows its customers to withdraw only one of the following amounts in one operation:
1 yen (the currency of Japan)
6 yen, 62(=36) yen, 63(=216) yen, ...
9 yen, 92(=81) yen, 93(=729) yen, ...
At least how many operations are required to withdraw exactly N yen in total?
It is not allowed to re-deposit the money you withdrew.
Constraints
1≤N≤100000
N is an integer.
输入
Input is given from Standard Input in the following format:
N
输出
If at least x operations are required to withdraw exactly N yen in total, print x.
样例输入 Copy
127
样例输出 Copy
4
提示
By withdrawing 1 yen, 9 yen, 36(=62) yen and 81(=92) yen, we can withdraw 127 yen in four operations.
注意要把dp设的很大。。。。。。因为是min。。。。
#include<bits/stdc++.h>
using namespace std;
int A[100005],dp[100005];
int main()
{
int x;
scanf("%d",&x);
int cnt=0,temp=6;
A[cnt++]=1;
while(temp<100005)
{
A[cnt++]=temp;
temp*=6;
}
temp=9;
while(temp<100005)
{
A[cnt++]=temp;
temp*=9;
}
sort(A,A+cnt);
for(int i=1;i<=x;i++)
dp[i]=100005;
for(int i=0; i<cnt; i++)
{
for(int j=A[i]; j<=x; j++)
{
dp[j]=min(dp[j],dp[j-A[i]]+1);
}
}
printf("%d",dp[x]);
return 0;
}
暴力法,理解题意很重要
问题 H: Good Grid
时间限制: 1 Sec 内存限制: 128 MB
[提交] [状态]
题目描述
There is a grid with N rows and N columns of squares. Let (i,j) be the square at the i-th row from the top and the j-th column from the left.
These squares have to be painted in one of the C colors from Color 1 to Color C. Initially, (i,j) is painted in Color ci,j.
We say the grid is a good grid when the following condition is met for all i,j,x,y satisfying 1≤i,j,x,y≤N:
If (i+j)%3=(x+y)%3, the color of (i,j) and the color of (x,y) are the same.
If (i+j)%3≠(x+y)%3, the color of (i,j) and the color of (x,y) are different.
Here, X%Y represents X modulo Y.
We will repaint zero or more squares so that the grid will be a good grid.
For a square, the wrongness when the color of the square is X before repainting and Y after repainting, is DX,Y.
Find the minimum possible sum of the wrongness of all the squares.
Constraints
1≤N≤500
3≤C≤30
1≤Di,j≤1000(i≠j),Di,j=0(i=j)
1≤ci,j≤C
All values in input are integers.
输入
Input is given from Standard Input in the following format:
N C
D1,1 … D1,C
:
DC,1 … DC,C
c1,1 … c1,N
:
cN,1 … cN,N
输出
If the minimum possible sum of the wrongness of all the squares is x, print x.
样例输入 Copy
2 3 0 1 1 1 0 1 1 4 0 1 2 3 3
样例输出 Copy
3
提示
Repaint (1,1) to Color 2. The wrongness of (1,1) becomes D1,2=1.
Repaint (1,2) to Color 3. The wrongness of (1,2) becomes D2,3=1.
Repaint (2,2) to Color 1. The wrongness of (2,2) becomes D3,1=1.
In this case, the sum of the wrongness of all the squares is 3.
Note that Di,j≠Dj,i is possible.
题意:
N C
D1,1 … D1,C
:
DC,1 … DC,C
c1,1 … c1,N
:
cN,1 … cN,N
对于这个数据的理解:一共有n*n个方格;c种颜色
先输入每个颜色的对应变化需要的代价(Dij)
然后输入这个放格里面的最初的颜色(cij)
解题思路:
这道题的数据并不大,就暴力枚举,找一个数组mod【余数(0、1、2)只有这三种,表示对应的cij位置】【对应的颜色】;
将余数为0、1、2的颜色分别变成相同的颜色(每个余数对应一种颜色
//https://www.cnblogs.com/FJ-LinHua/p/9166162.html
#include<bits/stdc++.h>
using namespace std;
int d[505][505],mod[505][505];
int main()
{
int n,c;
scanf("%d %d",&n,&c);
for(int i=1;i<=c;i++)
{
for(int j=1;j<=c;j++)
scanf("%d",&d[i][j]);
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
int x;
scanf("%d",&x);
mod[(i+j)%3][x]++;
}
}
int a,b,p, ans=0xfffffff;
for(int i=1;i<=c;i++)
{
a=0;
for(int j=1;j<=c;j++)
{
a+=mod[0][j]*d[j][i];
}
for(int j=1;j<=c;j++)
{
b=0;
if(j==i) continue;
for(int k=1;k<=c;k++)
b+=mod[1][k]*d[k][j];
for(int k=1;k<=c;k++)
{
p=0;
if(k==i||k==j)continue;
for(int l=1;l<=c;l++)
p+=d[l][k]*mod[2][l];
ans=min(ans,a+b+p);
}
}
}
printf("%d",ans);
return 0;
}