首页 技术 正文
技术 2022年11月17日
0 收藏 605 点赞 3,808 浏览 2418 个字

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 ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,075
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,551
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,399
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,811
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,893