bzoj2241
枚举矩形的长和宽,判断是否可行。时间复杂度:O((n*m)^3)
剪枝:1.最优化剪枝 2.如果矩形面积不能整除地图点权之和,则直接判断不可行。
const int maxn = 105, maxm = 105;
int n, m, sum, ans;
int map[maxn][maxm];
int tmp[maxn][maxm];
bool check(int row,int col)
{
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
tmp[i][j] = map[i][j];
for(int i = 1; i + row - 1 <= n; i++)
for(int j = 1; j + col - 1 <= m; j++)
if(tmp[i][j])
{
for(int p = row - 1; p >= 0; p--)
for(int q = col - 1; q >= 0; q--)
tmp[i+p][j+q] -= tmp[i][j];
}
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
if(tmp[i][j]) return false;
return true;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("bzoj2241.in","r",stdin);
freopen("bzoj2241.out","w",stdout);
#endif
read(n), read(m);
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
read(map[i][j]), sum += map[i][j];
for(int i = n; i > 0; i--)
for(int j = m; j > 0; j--)
if(i*j > ans && !(sum%(i*j)) && check(i,j)) ans = i*j;
write(sum/ans);
#ifndef ONLINE_JUDGE
fclose(stdin);
fclose(stdout);
#endif
return 0;
}
uva11729
按执行时间排序,贪心即可。
证明:交换相邻安排任务的顺序,证明一定不比原决策更优。
int main()
{
int CaseNum = 0;
#ifndef ONLINE_JUDGE
freopen("Uva11729.in","r",stdin);
freopen("Uva11729.out","w",stdout);
#endif
while(scanf("%d",&n) != EOF)
{
if(!n) break;
++CaseNum, ans = 0;
printf("Case %d: ",CaseNum);
for(int i = 1; i <= n; i++)
std::cin >> c[i].second >> c[i].first;
std::sort(c + 1, c + n + 1);
for(int i = n, t = 0; i > 0; i--)
t += c[i].second ,ans = std::max(ans, t + c[i].first);
write(ans), puts("");
}
#ifndef ONLINE_JUDGE
fclose(stdin);
fclose(stdout);
#endif
return 0;
}
hiho 1162
可以找规律,OEIS上有定义: http://oeis.org/A001835
Number of ways of packing a 3 X 2(n-1) rectangle with dominoes. - David Singmaster.
f(1)=1 f(2)=3 f(n)=4∗f(n−1)−f(n−2)(n>2)
状态压缩的递推也可以处理,当然两种递推都需要矩阵乘法优化。
const int Mod = 12357;
int n;
struct Matrix
{
long long a[2][2];
}P, T, I, emp;
Matrix operator *(const Matrix &A,const Matrix &B)
{
Matrix ret = emp;
for(int i = 0; i < 2; i++)
for(int j = 0; j < 2; j++)
{
for(int k = 0; k < 2; k++)
ret.a[i][j] += A.a[i][k]*B.a[k][j]%Mod;
ret.a[i][j] %= Mod;
}
return ret;
}
Matrix powermod(Matrix Q,int k)
{
Matrix ret = I;
while(k)
{
if(k&1) ret = ret*Q;
Q = Q*Q, k >>= 1;
}
return ret;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("cover.in","r",stdin);
freopen("cover.out","w",stdout);
#endif
read(n);
if(n&1) putchar('0');
else
{
n >>= 1;
T.a[0][0] = 4;
T.a[0][1] = 1;
T.a[1][0] = Mod - 1;
T.a[1][1] = 0;
P.a[0][0] = 3;
P.a[0][1] = 1;
I.a[0][0] = 1;
I.a[1][1] = 1;
P = P*powermod(T, n - 1);
write(P.a[0][0]);
}
#ifndef ONLINE_JUDGE
fclose(stdin);
fclose(stdout);
#endif
return 0;
}
NOIP2012 国王游戏
贪心按 ai∗bi 排序,然后高精度处理。
const int maxn = 1005, Base = 10, maxl = 10000;
int n, a[maxn], b[maxn];
std::pair<int,int> c[maxn];
BigNum emp, poi, tmp, ans;
int main()
{
#ifndef ONLINE_JUDGE
freopen("game.in","r",stdin);
freopen("game.out","w",stdout);
#endif
read(n), read(a[0]), read(b[0]);
for(int i = 1; i <= n; i++)
read(a[i]), read(b[i]), c[i] = std::make_pair(a[i]*b[i], b[i]);
std::sort(c + 1, c + n + 1);
for(int i = 1; i <= n; i++)
a[i] = c[i].first/c[i].second, b[i] = c[i].second;
poi.len = 1, poi.num[1] = 1;
for(int i = 1; i <= n; i++)
{
poi *= a[i-1], tmp = poi, tmp /= b[i];
if(tmp > ans) ans = tmp;
}
ans.print();
#ifndef ONLINE_JUDGE
fclose(stdin);
fclose(stdout);
#endif
return 0;
}