hdu 4925 贪心 自己从小到大做数据找方法规律

时间:2021-10-03 11:24:02

http://acm.hdu.edu.cn/showproblem.php?pid=4925

自己逐个做数据找规律。提供下我的找的:

1 2

1 3

2 2

2 3

3 3

然后发现这种矩阵是最优的:

hdu 4925 贪心  自己从小到大做数据找方法规律

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMTAyNjk2OA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

然后初始化的时候搞一个100*100的矩阵。然后每次对这个矩阵统计下2^1 2^2 2^3 2^4各有几个就可以

WA了一次。由于我的统计方法里,n==1 m==1这样的会出现错误

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <map>
#include <set>
#include <queue>
using namespace std; #define ls(rt) rt*2
#define rs(rt) rt*2+1
#define ll long long
#define ull unsigned long long
#define rep(i,s,e) for(int i=s;i<e;i++)
#define repe(i,s,e) for(int i=s;i<=e;i++)
#define CL(a,b) memset(a,b,sizeof(a))
#define IN(s) freopen(s,"r",stdin)
#define OUT(s) freopen(s,"w",stdout)
const ll ll_INF = ((ull)(-1))>>1;
const double EPS = 1e-8;
const int INF = 100000000;
ll n,m;
const int MAXN = 110;
bool mat[MAXN][MAXN]; ll p[5]={1,2,4,8,16};
ll cnt[5]; void init()
{
for(int i=1;i<=100;i++)
for(int j=1;j<=100;j++)
{
if(i%2)mat[i][j]=j%2;
else mat[i][j]=!(j%2);
}
} ll solve()
{
CL(cnt,0);
//ll a1=0,a2,a3,a4=0;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
if(mat[i][j])
{
ll ret=0;
if(i-1>=1)ret++;
if(i+1<=n)ret++;
if(j-1>=1)ret++;
if(j+1<=m)ret++;
cnt[ret]++;
} }
return p[1]*cnt[1]+p[2]*cnt[2]+p[3]*cnt[3]+p[4]*cnt[4];
} int main()
{
//IN("hdu4925.txt");
int ncase;
init();
scanf("%d",&ncase);
while(ncase--)
{
scanf("%I64d%I64d",&n,&m);
if(n==1 && m==1)printf("1\n");
else printf("%I64d\n",solve());
}
return 0;
}