1.Angry Cows

http://www.usaco.org/index.php?page=viewproblem2&cpid=597

dp题+vector数组运用

将从左向右与从右向左扫描结合。先从左到右DP,确定每个干草捆向右爆炸的最小半径,再从右到左,确定每个干草捆向左爆炸的最小半径。通过扫描每个干草捆,用这两个数字来确定我们应该设置初始引爆点的最佳位置。

#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
#define INF 2000000000
int main()
{
//freopen("angry.in", "r", stdin);
//freopen("angry.out", "w", stdout);
int n;
scanf("%d",&n);
vector<int> a(n);
for(int i=;i<n;i++)
{
scanf("%d",&a[i]);
a[i]*=;
}
sort(a.begin(), a.end());
a.resize(unique(a.begin(),a.end())-a.begin()); vector<int> DP[];
for(int it=;it<;it++)
{
int l=;
DP[it].resize(n,INF);
DP[it][]=-;
for(int i=;i<n;i++)
{
while(l+<i&&abs(a[i]-a[l+])>DP[it][l+]+)
{
l++;
}
DP[it][i]=min(abs(a[i]-a[l]),DP[it][l+]+);
}
reverse(a.begin(),a.end());
}
reverse(DP[].begin(),DP[].end()); int i=,j=n-,res=INF;
while(i<j)
{
res=min(res,max((a[j]-a[i])/,+max(DP[][i],DP[][j])));
if(DP[][i+]<DP[][j-])
i++;
else
j--;
}
printf("%d.%d\n",res/,(res%?:));
return ;
}

2.Radio Contact

这个问题实际上是一个隐藏的 动态时间扭曲问题,其中误差函数是FJ和Bessie之间的平方距离。

因此,可以通过动态编程解决问题。对于Farmer John和Bessie的每个可能的位置,我们可以通过尝试向前迈出FJ,向前走Bessie,向前移动他们来计算他们达到最终位置所需的最小能量。

#include <vector>
#include <cstring>
#include <cstdio>
#include <map>
#include <iostream>
using namespace std;
#define INF 0x7FFFFFFFFFFFFFFFLL
long long memo[][]; vector<pair<long long, long long> > F;
vector<pair<long long, long long> > B;
long long solve(int fi, int bi) {
/* The energy cost of the radio for this timestep. */
long long base = (F[fi].first - B[bi].first) * (F[fi].first - B[bi].first) +
(F[fi].second - B[bi].second) * (F[fi].second - B[bi].second);
if (fi + == F.size() && bi + == B.size()) {
return base;
}
long long& ref = memo[fi][bi];
if (ref != -) return ref;
/* Don't include the cost of the first timestep. */
if (fi == && bi == ) base = ;
ref = INF;
if (fi + < F.size()) {
/* Step FJ forward. */
ref = min(ref, base + solve(fi + , bi));
}
if (bi + < B.size()) {
/* Step Bessie forward. */
ref = min(ref, base + solve(fi, bi + ));
}
if (fi + < F.size() && bi + < B.size()) {
/* Step both forward. */
ref = min(ref, base + solve(fi + , bi + ));
}
return ref;
}
int main() {
//freopen("radio.in", "r", stdin);
//freopen("radio.out", "w", stdout);
map<char, int> dx, dy;
dx['E'] = ; dx['W'] = -;
dy['N'] = ; dy['S'] = -;
int N, M;
scanf("%d%d",&N,&M);
int fx, fy, bx, by;
scanf("%d%d%d%d",&fx,&fy,&bx,&by);
string SF, SB;
cin >> SF >> SB;
/* Compute FJ's path. */
F.push_back(make_pair(fx, fy));
for (int i = ; i < SF.size(); i++) {
fx += dx[SF[i]];
fy += dy[SF[i]];
F.push_back(make_pair(fx, fy));
}
/* Compute Bessie's path. */
B.push_back(make_pair(bx, by));
for (int i = ; i < SB.size(); i++) {
bx += dx[SB[i]];
by += dy[SB[i]];
B.push_back(make_pair(bx, by));
}
memset(memo, -, sizeof(memo));
cout << solve(, ) << endl;
return ;
}

最新文章

  1. Spring Security笔记:Hello World
  2. iOS 网络监测
  3. Ubuntu下解决adb devices:???????????? no permissions的方法
  4. MySQL数据库的导入和导出
  5. shell命令date
  6. Scala 具体的并行集合库【翻译】
  7. 初步揭秘node.js中的事件
  8. [转载]Magento 店铺多语言设置
  9. 通过正则获取url参数
  10. 传输层之UDP
  11. [TypeScript] Inheritance
  12. MySQL调试
  13. SlimDX的DirectSound模块
  14. spring-boot-oracle spring-batch
  15. QT读取xml配置文件
  16. bash的基础特性
  17. 洛谷.4008.[NOI2003]editor文本编辑器(块状链表)
  18. jQuery时间格式转换
  19. 【java工具】AES CBC加密
  20. MySQL数据库索引(上)

热门文章

  1. POJ 2253 Frogger(最短路&amp;Floyd)题解
  2. BZOJ 3572 【HNOI2014】 世界树
  3. shell 判断一个字符串是否为空
  4. [原][译][osgearth]关于Features &amp; Symbology (特征与符号)(OE绘制矢量几何与特殊字符)讲解(OE官方文档翻译)
  5. Android多个Module统一配置相同jar或库的版本号
  6. SSM(Spring+SpringMVC+Mybatis)+Mysql 框架整合搭建流程以及其间注意事项
  7. Luogu P4062 [CTSC2018]混合果汁 (主席树)
  8. snmpwalk-MIB of S5700-idc-stack
  9. (UDP)socket编程
  10. LINUX中的RCU机制的分析