最近刷题刷的有点晕...状态不是很好= =
1.拦截导弹
题目链接:http://noi.openjudge.cn/ch0206/8780/
其实本质就是求最长下降子序列
2.Maximum sum
题目链接:http://noi.openjudge.cn/ch0206/1481/
本质是求两个区域内的最大子段和
思路:先求出以第i个数为结尾的最大子段和,再求出前i个数的最大子段和,同时倒序求出以第i个数为开头的最大子段和,再求出后i个数的最大子段和
最后再把两者相加找到最大值。
int main()
{
int t;
int a[50002];
int l[50002];
int r[50002];
int rm[50002];
int lm[50002];
cin >> t;
while (t--)
{
int n;
cin >> n;
for (int i = 0; i < n; ++i)
cin >> a[i];
l[0] = a[0];
r[n - 1] = a[n - 1];
for (int i = 1; i < n; ++i)
{
if (l[i - 1] < 0)
l[i] = a[i];
else
l[i] = l[i - 1] + a[i];
}
lm[0] = l[0];
for (int i = 1; i < n; ++i)
lm[i] = max(lm[i - 1], l[i]);
for (int i = n - 2; i >= 0; --i)
{
if (r[i + 1] < 0)
r[i] = a[i];
else
r[i] = r[i + 1] + a[i];
}
rm[n - 1] = r[n - 1];
for (int i = n - 2; i >= 0; --i)
rm[i] = max(rm[i + 1], r[i]);
int cmax = MIN;
for (int i = 0; i < n - 1; ++i)
{
cmax = max(cmax, lm[i] + rm[i + 1]);
}
cout << cmax << endl;
}
return 0;
}
题目链接:http://noi.openjudge.cn/ch0206/1808/
可以用之前奇技淫巧的办法直接做,比较快速。
4.山区建小学
题目链接:http://noi.openjudge.cn/ch0206/7624/
这个题首先要明白的一点是在一个区域内建一个小学,该小学一定是在该区域最中间时路程和最短。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
const int inf = 99999999;
int f[510][510];//f[i][j]表示在前i个村庄建j个小学的最短路程
int dis[510][510];//从i到j距离
int d[510];
int s[510][510];
//s[管辖区起点][管辖区终点]=这片辖区内建一个学校,区内村庄到学校的距离和
int n, m;
int dist(int i, int j)
{
int x = 0;
int mid = (i + j) / 2;
for (int k = i; k <= j; k++)
x += dis[k][mid];
return x;
}
int main()
{
scanf("%d%d", &m, &n);
int i, j, x;
d[1] = 0;
for (i = 2; i <= m; i++)
{//从2开始,方便求各村庄间距离
scanf("%d", &x);
d[i] = d[i - 1] + x;
}
for (i = 1; i <= m; i++)
for (j = i; j <= m; j++)
{
if (i == j)
dis[i][j] = 0;
else
dis[i][j] = dis[j][i] = abs(d[j] - d[i]);
}
//初始化两两距离
for (i = 1; i <= m; i++)
for (j = 1; j <= m; j++)
s[i][j] = dist(i, j);
//计算一个管辖从i到j村庄的学校到这些村庄的距离和
for (i = 1; i <= m; i++)
for (j = 1; j <= m; j++)
f[i][j] = inf;
for (i = 1; i <= m; i++)
f[i][i] = 0;
for (i = 1; i <= m; i++)
f[i][1] = s[1][i];//只建一个学校的情况
//f初始化
for (i = 2; i <= m; i++)
{//村庄
for (j = 2; j <= min(i, n); j++)
{//学校
for (int k = j - 1; k <= i - 1; k++)
{//枚举已有的学校管辖的范围
if (i != j)
f[i][j] = min(f[i][j], f[k][j - 1] + s[k + 1][i]);
}
}
}
printf("%d", f[m][n]);
return 0;
}
5.买书
题目链接:http://noi.openjudge.cn/ch0206/6049/
典型的动规
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
const int inf = 0x3f3f3f3f;
int main()
{
int n;
int f[1010] = { 0 };
int a[4] = { 10, 20, 50, 100 };
cin >> n;
f[0] = 1;
for (int i = 0; i < 4; ++i)
for (int j = a[i]; j <= n; ++j)
f[j] += f[j - a[i]];
cout << f[n] << endl;
return 0;
}
6.摘花生,最低通行费,移动路线
题目链接:http://noi.openjudge.cn/ch0206/2728/
http://noi.openjudge.cn/ch0206/7614/
http://noi.openjudge.cn/ch0206/2718/
一个类型的题目,这种在地图上走来走去的题最轻松了....
7.踩方格
题目链接:http://noi.openjudge.cn/ch0206/4982/
找规律型动规题
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
const int inf = 0x3f3f3f3f;
/*
设l[i],r[i],u[i]分别为第一步走左、右、上,总共走i格的方案数,f[i]为总方案数。
l[i]=l[i-1]+u[i-1], r[i]=r[i-1]+u[i-1], u[i]=l[i-1]+r[i-1]+u[i-1];
f[i]=l[i]+r[i]+u[i] (可发现 u[i]=f[i-1])
=2*l[i-1]+2*r[i-1]+3*u[i-1]
=2*f[i-1]+u[i-1]
=2*f[i-1]+f[i-2]
*/
int main()
{
int n;
int dp[22];
cin >> n;
dp[1] = 3;
dp[2] = 7;
for (int i = 3; i <= n; ++i)
dp[i] = dp[i - 1] * 2 + dp[i - 2];
cout << dp[n] << endl;
return 0;
}
8.吃糖果
题目链接:http://noi.openjudge.cn/ch0206/1944/
找规律型动规
吃N个糖果 = 第一天吃1个糖果 + 之后吃N - 1个糖果 或者第一天吃2个糖果 之后吃N-2个糖果
int main()
{
int n;
int dp[30];
dp[1] = 1;
dp[2] = 2;
cin >> n;
for (int i = 3; i <= n; ++i)
dp[i] = dp[i - 1] + dp[i - 2];
cout << dp[n] << endl;
return 0;
}
题目链接:http://noi.openjudge.cn/ch0206/1996/
本质求以a[i]为终点的上升子序列 + 以a[i]为起点的下降子序列之和的最大值
#include <iostream>
#include <memory.h>
#include <algorithm>
using namespace std;
int n;
int sce[1010];
int u[1010];
int d[1010];
int up(int i)
{
for (int j = 1; j <= i; ++j)
{
for (int k = 0; k < j; ++k)
{
if (sce[k] < sce[j])
u[j] = max(u[j], u[k] + 1);
}
}
return u[i];
}
int down(int i)
{
for (int j = n - 2; j >= i; --j)
for (int k = n - 1; k > j; --k)
if (sce[k] < sce[j])
d[j] = max(d[j], d[k] + 1);
return d[i];
}
int main()
{
cin >> n;
for (int i = 0; i < n; ++i)
cin >> sce[i];
for (int i = 0; i < n; ++i)
u[i] = d[i] = 1;
int cmax = 0;
for (int i = 0; i < n; ++i)
{
u[i] = up(i);
d[i] = down(i);
if (cmax < u[i] + d[i] - 1)
cmax = u[i] + d[i] - 1;
}
cout << cmax << endl;
return 0;
}