zoukankan      html  css  js  c++  java
  • [USACO15OPEN]haybales Trappe…

    嘟嘟嘟

    刚开始我以为如果这头牛撞开一个干草堆的话,获得的冲刺距离只有新增的部分,但实际上是加上原来的部分的。

    暴力很好写,区间排完序后一次判断每一个区间是否能逃脱,复杂度O(n2)。

    优化想起来也不难:如果一个区间 i 能逃脱,区间 j 能到达 i,则 j 也能逃脱。所以对于每个区间开一个标记数组,记录能否逃脱。然后枚举区间的时候向两边扩,如果到达了一个能逃脱的区间就返回,并标记。复杂度虽然不能准确算出来,但能A这道题。

     1 #include<cstdio>
     2 #include<iostream>
     3 #include<cmath>
     4 #include<algorithm>
     5 #include<cstring>
     6 #include<cstdlib>
     7 #include<cctype>
     8 #include<vector>
     9 #include<stack>
    10 #include<queue>
    11 using namespace std;
    12 #define enter puts("") 
    13 #define space putchar(' ')
    14 #define Mem(a, x) memset(a, x, sizeof(a))
    15 #define rg register
    16 typedef long long ll;
    17 typedef double db;
    18 const int INF = 0x3f3f3f3f;
    19 const db eps = 1e-8;
    20 const int maxn = 1e5 + 5;
    21 inline ll read()
    22 {
    23   ll ans = 0;
    24   char ch = getchar(), last = ' ';
    25   while(!isdigit(ch)) {last = ch; ch = getchar();}
    26   while(isdigit(ch)) {ans = ans * 10 + ch - '0'; ch = getchar();}
    27   if(last == '-') ans = -ans;
    28   return ans;
    29 }
    30 inline void write(ll x)
    31 {
    32   if(x < 0) x = -x, putchar('-');
    33   if(x >= 10) write(x / 10);
    34   putchar(x % 10 + '0');
    35 }
    36 
    37 int n;
    38 struct Node
    39 {
    40   int siz, id;
    41   bool operator < (const Node &oth)const
    42   {
    43     return id < oth.id;
    44   }
    45 }t[maxn];
    46 bool vis[maxn];
    47 
    48 bool solve(int x)
    49 {
    50   if(vis[x]) return 1;
    51   int dis = t[x + 1].id - t[x].id;
    52   int L = x, R = x + 1;
    53   while(L >= 1 && R <= n)
    54     {
    55       bool flg = 0;
    56       if(dis > t[L].siz)
    57     {
    58       flg = 1; L--;
    59       if(vis[L]) {vis[x] = 1; return 1;}
    60       dis += t[L + 1].id - t[L].id;
    61     }
    62       if(dis > t[R].siz)
    63     {
    64       flg = 1; R++;
    65       if(vis[R - 1]) {vis[x] = 1; return 1;}
    66       dis += t[R].id - t[R - 1].id;
    67     }
    68       if(!flg) return 0;
    69     }
    70   return 1;
    71 }
    72 
    73 ll ans = 0;
    74 
    75 int main()
    76 {
    77   n = read();
    78   for(int i = 1; i <= n; ++i) t[i].siz = read(), t[i].id = read();
    79   sort(t + 1, t + n + 1);
    80   vis[0] = vis[n] = 1;
    81   for(int i = 1; i <= n; ++i)
    82     if(!solve(i)) ans += t[i + 1].id - t[i].id;
    83   write(ans), enter;
    84   return 0;
    85 }
    View Code
  • 相关阅读:
    javaScript设计模式探究【1】
    Java基础算法集50题
    DataTable学习笔记排序细则、列隐藏[3]
    javaScript设计模式探究【4】工厂模式
    javaScript设计模式探究【3】
    一次面试感想+js最近学习体会
    DataTable学习笔记范例应用篇[2]
    腾讯2013实习生招聘面经
    初品cakephp
    php编译中配置libxml2的错误
  • 原文地址:https://www.cnblogs.com/mrclr/p/9836943.html
Copyright © 2011-2022 走看看