Submission #683049

# Submission time Handle Problem Language Result Execution time Memory
683049 2023-01-17T15:22:17 Z omikron123 Balloons (CEOI11_bal) C++14
100 / 100
150 ms 10784 KB
// https://oj.uz/problem/view/CEOI11_bal

#include <cstdio>
#include <algorithm>
#include <functional>
#include <vector>
#include <cstring>
#include <stack>
using namespace std;
typedef long long ll;

/*
1. y[i] = min( (x[i]-x[j])^2 / (4*y[j]) )
2. 检查每一个之前的气球导致O(n^2),会TLE. 
3. 我们注意到:
  a. 对于一个气球(y[i],x[i]),任何比这个气球位置更左,大小更小的气球都是不会和将来的气球接触到的。
  b. 如果i碰到一个左侧更大的气球j,则肯定不会碰到j左侧更大的气球。
4. 所以问题变成了nearest larger value的问题,我们要维护一个直径单调减的气球的stack。
5. 每次新增气球时,从stack顶开始计算y[i],如果比栈顶大,就pop再计算。如果小于等于栈顶,则这个大小合适。
*/

int n;  // 2e5
int x[200005], r[200005];
double ans[200005];
stack<pair<double,int>> st;     // (radius, x), radius decreasing, x increasing

int main() {
    scanf("%d", &n);
    for (int i = 0; i < n; i++)
        scanf("%d %d", &x[i], &r[i]);
    
    for (int i = 0; i < n; i++) {
        double y = r[i];
        while (!st.empty()) {
            y = min(y, (ll)(x[i]-st.top().second)*(x[i]-st.top().second) / st.top().first / 4);
            if (y > st.top().first) 
                st.pop();
            else
                break;
        }
        st.push({y, x[i]});
        ans[i] = y;
    }
    for (int i = 0; i < n; i++)
        printf("%lf\n", ans[i]);

    return 0;
}

Compilation message

bal.cpp: In function 'int main()':
bal.cpp:28:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   28 |     scanf("%d", &n);
      |     ~~~~~^~~~~~~~~~
bal.cpp:30:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   30 |         scanf("%d %d", &x[i], &r[i]);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB 10 numbers
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB 2 numbers
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB 505 numbers
# Verdict Execution time Memory Grader output
1 Correct 2 ms 340 KB 2000 numbers
# Verdict Execution time Memory Grader output
1 Correct 12 ms 896 KB 20000 numbers
# Verdict Execution time Memory Grader output
1 Correct 29 ms 1868 KB 50000 numbers
2 Correct 26 ms 2880 KB 49912 numbers
# Verdict Execution time Memory Grader output
1 Correct 78 ms 3168 KB 100000 numbers
# Verdict Execution time Memory Grader output
1 Correct 72 ms 3668 KB 115362 numbers
2 Correct 64 ms 6600 KB 119971 numbers
# Verdict Execution time Memory Grader output
1 Correct 100 ms 4684 KB 154271 numbers
2 Correct 98 ms 10784 KB 200000 numbers
# Verdict Execution time Memory Grader output
1 Correct 150 ms 5652 KB 200000 numbers
2 Correct 98 ms 10704 KB 199945 numbers