1.Angry Cows

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

dp题+vector数组运用

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

  1. #include <cstdio>
  2. #include <algorithm>
  3. #include <vector>
  4. using namespace std;
  5. #define INF 2000000000
  6. int main()
  7. {
  8. //freopen("angry.in", "r", stdin);
  9. //freopen("angry.out", "w", stdout);
  10. int n;
  11. scanf("%d",&n);
  12. vector<int> a(n);
  13. for(int i=;i<n;i++)
  14. {
  15. scanf("%d",&a[i]);
  16. a[i]*=;
  17. }
  18. sort(a.begin(), a.end());
  19. a.resize(unique(a.begin(),a.end())-a.begin());
  20.  
  21. vector<int> DP[];
  22. for(int it=;it<;it++)
  23. {
  24. int l=;
  25. DP[it].resize(n,INF);
  26. DP[it][]=-;
  27. for(int i=;i<n;i++)
  28. {
  29. while(l+<i&&abs(a[i]-a[l+])>DP[it][l+]+)
  30. {
  31. l++;
  32. }
  33. DP[it][i]=min(abs(a[i]-a[l]),DP[it][l+]+);
  34. }
  35. reverse(a.begin(),a.end());
  36. }
  37. reverse(DP[].begin(),DP[].end());
  38.  
  39. int i=,j=n-,res=INF;
  40. while(i<j)
  41. {
  42. res=min(res,max((a[j]-a[i])/,+max(DP[][i],DP[][j])));
  43. if(DP[][i+]<DP[][j-])
  44. i++;
  45. else
  46. j--;
  47. }
  48. printf("%d.%d\n",res/,(res%?:));
  49. return ;
  50. }

2.Radio Contact

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

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

  1. #include <vector>
  2. #include <cstring>
  3. #include <cstdio>
  4. #include <map>
  5. #include <iostream>
  6. using namespace std;
  7. #define INF 0x7FFFFFFFFFFFFFFFLL
  8. long long memo[][];
  9.  
  10. vector<pair<long long, long long> > F;
  11. vector<pair<long long, long long> > B;
  12. long long solve(int fi, int bi) {
  13. /* The energy cost of the radio for this timestep. */
  14. long long base = (F[fi].first - B[bi].first) * (F[fi].first - B[bi].first) +
  15. (F[fi].second - B[bi].second) * (F[fi].second - B[bi].second);
  16. if (fi + == F.size() && bi + == B.size()) {
  17. return base;
  18. }
  19. long long& ref = memo[fi][bi];
  20. if (ref != -) return ref;
  21. /* Don't include the cost of the first timestep. */
  22. if (fi == && bi == ) base = ;
  23. ref = INF;
  24. if (fi + < F.size()) {
  25. /* Step FJ forward. */
  26. ref = min(ref, base + solve(fi + , bi));
  27. }
  28. if (bi + < B.size()) {
  29. /* Step Bessie forward. */
  30. ref = min(ref, base + solve(fi, bi + ));
  31. }
  32. if (fi + < F.size() && bi + < B.size()) {
  33. /* Step both forward. */
  34. ref = min(ref, base + solve(fi + , bi + ));
  35. }
  36. return ref;
  37. }
  38. int main() {
  39. //freopen("radio.in", "r", stdin);
  40. //freopen("radio.out", "w", stdout);
  41. map<char, int> dx, dy;
  42. dx['E'] = ; dx['W'] = -;
  43. dy['N'] = ; dy['S'] = -;
  44. int N, M;
  45. scanf("%d%d",&N,&M);
  46. int fx, fy, bx, by;
  47. scanf("%d%d%d%d",&fx,&fy,&bx,&by);
  48. string SF, SB;
  49. cin >> SF >> SB;
  50. /* Compute FJ's path. */
  51. F.push_back(make_pair(fx, fy));
  52. for (int i = ; i < SF.size(); i++) {
  53. fx += dx[SF[i]];
  54. fy += dy[SF[i]];
  55. F.push_back(make_pair(fx, fy));
  56. }
  57. /* Compute Bessie's path. */
  58. B.push_back(make_pair(bx, by));
  59. for (int i = ; i < SB.size(); i++) {
  60. bx += dx[SB[i]];
  61. by += dy[SB[i]];
  62. B.push_back(make_pair(bx, by));
  63. }
  64. memset(memo, -, sizeof(memo));
  65. cout << solve(, ) << endl;
  66. return ;
  67. }

USACO 2016 January Contest, Gold解题报告的更多相关文章

  1. USACO 2016 February Contest, Gold解题报告

    1.Circular Barn   http://www.usaco.org/index.php?page=viewproblem2&cpid=621 贪心 #include <cstd ...

  2. USACO 2016 US Open Contest, Gold解题报告

    1.Splitting the Field http://usaco.org/index.php?page=viewproblem2&cpid=645 给二维坐标系中的n个点,求ans=用一个 ...

  3. USACO Section2.2 Preface Numbering 解题报告 【icedream61】

    preface解题报告----------------------------------------------------------------------------------------- ...

  4. USACO Section2.1 Hamming Codes 解题报告 【icedream61】

    hamming解题报告----------------------------------------------------------------------------------------- ...

  5. USACO Section2.1 Healthy Holsteins 解题报告 【icedream61】

    holstein解题报告 --------------------------------------------------------------------------------------- ...

  6. USACO Section2.1 The Castle 解题报告

    castle解题报告 —— icedream61 博客园(转载请注明出处)--------------------------------------------------------------- ...

  7. USACO Section1.5 Prime Palindromes 解题报告

    pprime解题报告 —— icedream61 博客园(转载请注明出处)--------------------------------------------------------------- ...

  8. USACO Section2.3 Controlling Companies 解题报告 【icedream61】

    concom解题报告------------------------------------------------------------------------------------------ ...

  9. USACO Section2.3 Money Systems 解题报告 【icedream61】

    money解题报告------------------------------------------------------------------------------------------- ...

随机推荐

  1. js模拟类的创建以及继承的四部曲

    <script> 1)创建父类 function Person(){ } Person.prototype.age = 18;//给父类添加属性 var p1 = new Person() ...

  2. JVM的异常处理

    异常处理的两大组成要素:抛出异常和捕获异常.这两大要素共同实现程序控制流的非正常转移. 抛出异常分为:显式和隐式两种. 显式抛异常的主题是应用程序,它指的是在程序中使用 “throw”  关键字.手动 ...

  3. python 获取二维数组所有元素

    import itertools original_list = [[,,],[,,], [], [,,]] new_merged_list = list(itertools.chain(*origi ...

  4. 【Demo】CSS3 动画文字

    效果图: 完整代码: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> &l ...

  5. 微信小程序封装storage(含错误处理)

    这次给你们安利的是微信小程序封装storage,先说下微信官方的 wx.getStorage({ key:"", success: function (res) { }, fail ...

  6. mysql插入中文数据报错 java.sql.SQLException: Incorrect string value: '\xE5\x90\x88\xE8\xAE\xA1' for column

    1.我们创建数据库的时候没有更改数据库的字符集为utf8. 在mysql工具中,右击数据库,->"改变数据库",->选择“基字符集”为utf-8; 2,数据库中表的字符 ...

  7. poj 2029 Get Many Persimmon Trees 各种解法都有,其实就是瞎搞不算吧是dp

    连接:http://poj.org/problem?id=2029 题意:给你一个map,然后在上面种树,问你h*w的矩形上最多有几棵树~这题直接搜就可以.不能算是DP 用树状数组也可作. #incl ...

  8. IOS-网络(GET请求和POST请求、HTTP通信过程、请求超时、URL转码)

    // // ViewController.m // IOS_0129_HTTP请求 // // Created by ma c on 16/1/29. // Copyright © 2016年 博文科 ...

  9. 字典序全排列(java实现)

    import java.util.Arrays; /** *字典序全排列 *字符串的全排列 *比如单词"too" 它的全排列是"oot","oto&q ...

  10. Xilinx SDK使用教程

    本文参考 Xilinx SDK软件内置的教程,打开方法:打开SDK->Help->Cheet Sheets...->Xilinx SDK Tutorials,这里有6篇文档.本文详细 ...