Submission #977736

# Submission time Handle Problem Language Result Execution time Memory
977736 2024-05-08T10:17:50 Z Requiem Building Bridges (CEOI17_building) C++17
0 / 100
63 ms 131072 KB
#include<bits/stdc++.h>
#define int long long
#define pb push_back
#define fast ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
#define MOD 1000000007
#define INF 1e18
#define fi first
#define se second
#define endl "\n"
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define FORD(i,a,b) for(int i=a;i>=b;i--)
#define sz(a) ((int)(a).size())
#define pi 3.14159265359
#define TASKNAME "bbridge"
template<typename T> bool maximize(T &res, const T &val) { if (res < val){ res = val; return true; }; return false; }
template<typename T> bool minimize(T &res, const T &val) { if (res > val){ res = val; return true; }; return false; }
using namespace std;
typedef pair<int,int> ii;
typedef pair<int,ii> iii;
typedef vector<int> vi;
const int MAXN = 3e5 + 9;
int h[MAXN] , w[MAXN];
int prew[MAXN];
int n;

namespace subtask1{
    bool checkSubtask1(){
        return n <= 3000;
    }

    int sqr(int x){
        return x * x;
    }

    int dp[MAXN];
    void solveSubtask1(){
        memset(dp, 0x3f, sizeof(dp));
        dp[1] = 0;
        for(int i = 2; i <= n; i++){
            for(int j = i - 1; j >= 1; j--){
                minimize(dp[i], dp[j] + sqr(abs(h[i] - h[j])) + (prew[i - 1] - prew[j]) );
            }
        }

        cout << dp[n] << endl;
    }
}

namespace subtask2{
    bool checkSubtask2(){
         return true;
    }

    struct line{
         int a, b;
         int val(int x){
             return a * x + b;
         }

         line(int a = 0, int b = 0): a(a), b(b) {}

         bool operator == (const line &other){
              return !(a == other.a and b == other.b);
         }
    };
    const int MAXH = 1e6 + 9;
    line lichao[MAXH << 2];
    int dp[MAXN];

    void reset(){
         for(int i = 0; i < 4 * MAXH; i++){
             lichao[i] = {0, 1000000000};
         }
    }

    void update(int node, int l, int r, line cur){
         int mid = (l + r) >> 1;
         bool f1 = cur.val(l) <= lichao[node].val(l);
         bool f2 = cur.val(mid) <= lichao[node].val(mid);

         if (f2) swap(lichao[node], cur);
         if (r == l + 1) return;
         else if (f1 != f2) update(node << 1, l, mid, cur);
         else {
             update(node << 1 | 1, mid, r, cur);
         }
    }

    int get(int node, int l, int r, int pos){
        if (l == r - 1){
            return lichao[node].val(pos);
        }
        int mid = (l + r) >> 1;
        if (pos < mid) return min(lichao[node].val(pos), get(node << 1, l, mid, pos));
        else return min(lichao[node].val(pos), get(node << 1 | 1, mid + 1, r, pos));
    }
    void solveSubtask2(){
        reset();
        dp[1] = 0;
        update(1, 0, 1e6, line(-2 * h[1], dp[1] + h[1] * h[1] - prew[1]));

        for(int i = 1; i <= n; i++){
            dp[i] = get(1, 0, 1e6, h[i]) + prew[i - 1] + h[i] * h[i];
            update(1, 0, 1e6, line(-2 * h[i], dp[i] + h[i] * h[i] - prew[i]));
//            cout << dp[i] << ' ';
        }
        cout << dp[n] << endl;

    }
}
main()
{
    fast;
   if (fopen(TASKNAME".inp","r")){
        freopen(TASKNAME".inp","r",stdin);
        freopen(TASKNAME".out","w",stdout);
   }
   cin >> n;
   for(int i = 1; i <= n; i++){
       cin >> h[i];
   }

   for(int i = 1; i <= n; i++){
       cin >> w[i];
       prew[i] = prew[i - 1] + w[i];
   }

//   if (subtask1::checkSubtask1()) return subtask1::solveSubtask1(), 0;
   if (subtask2::checkSubtask2()) return subtask2::solveSubtask2(), 0;
}
/**
Warning:
- MLE / TLE?
- Gioi han mang?
- Gia tri max phai luon gan cho -INF
- long long co can thiet khong?
- tran mang.
- code can than hon
**/

Compilation message

building.cpp:111:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  111 | main()
      | ^~~~
building.cpp: In function 'int main()':
building.cpp:115:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  115 |         freopen(TASKNAME".inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
building.cpp:116:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  116 |         freopen(TASKNAME".out","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 39 ms 69968 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 63 ms 131072 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 39 ms 69968 KB Output isn't correct
2 Halted 0 ms 0 KB -