답안 #477271

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
477271 2021-10-01T14:24:07 Z AkiYuu Building Bridges (CEOI17_building) C++17
컴파일 오류
0 ms 0 KB
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
#define forw(i,a,b) for(ll i=a;i<=b;i++)
#define forb(i,a,b) for(ll i=a;i>=b;i--)
typedef long long int ll;
ll res,n,h[200005],w[200005],max1[200005],pre,dp[200005];
void sub1()
{
    for (int i=2;i<=n;i++)
    {
        max1[i]=1e18;
        res=0;
        for (int j=i-1;j>=1;j--)
            {
                ll p=(h[i]-h[j])*(h[i]-h[j])+res+max1[j];
                if (p<max1[i]) max1[i]=p;
                res+=w[j];
            }
    }
    cout<<max1[n];
}
ll sqr(ll a)
{
    return a*a;
}
void sub2()
{
    ll ans=1e18;
    ans=(h[n]-h[1])*(h[n]-h[1])+pre-w[1]-w[n];
    map<int, set<int> > save;
    forw(i,2,n-1)
    {
        forw(j,-20,20)
        if (save[j].size())
        { 
            auto it =save[j].lower_bound((h[i]+h[1])/2);
            ans=min(ans,sqr(h[1]-h[i])+sqr(h[i]-h[n])+pre-w[1]-w[i]-w[n]);
            if (it!=save[j].end())
            {
                ans=min(ans,sqr(h[1]-(*it))+sqr((*it)-h[i])+sqr(h[n]-h[i])+pre-w[1]-w[i]-w[n]-j);
            }
            if (it!=save[j].begin())
            {
                --it;
                ans=min(ans,sqr(h[1]-(*it))+sqr((*it)-h[i])+sqr(h[n]-h[i])+pre-w[1]-w[i]-w[n]-j);
            }
        }
        save[w[i]].insert(h[i]);
    }
    cout<<ans<<endl;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    freopen("xaycau.INP","r",stdin);
    freopen("xaycau.OUT","w",stdout);
    scanf("%lld",&n);
    for (int i=1;i<=n;i++)
        scanf("%lld",&h[i]);
    bool ok=true;
    for (int i=1;i<=n;i++)#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
#define forw(i,a,b) for(ll i=a;i<=b;i++)
#define forb(i,a,b) for(ll i=a;i>=b;i--)
typedef long long int ll;
ll res,n,h[200005],w[200005],max1[200005],pre,dp[200005];
void sub1()
{
    for (int i=2;i<=n;i++)
    {
        max1[i]=1e18;
        res=0;
        for (int j=i-1;j>=1;j--)
            {
                ll p=(h[i]-h[j])*(h[i]-h[j])+res+max1[j];
                if (p<max1[i]) max1[i]=p;
                res+=w[j];
            }
    }
    cout<<max1[n];
}
ll sqr(ll a)
{
    return a*a;
}
void sub2()
{
    ll ans=1e18;
    ans=(h[n]-h[1])*(h[n]-h[1])+pre-w[1]-w[n];
    map<int, set<int> > save;
    forw(i,2,n-1)
    {
        forw(j,-20,20)
        if (save[j].size())
        { 
            auto it =save[j].lower_bound((h[i]+h[1])/2);
            ans=min(ans,sqr(h[1]-h[i])+sqr(h[i]-h[n])+pre-w[1]-w[i]-w[n]);
            if (it!=save[j].end())
            {
                ans=min(ans,sqr(h[1]-(*it))+sqr((*it)-h[i])+sqr(h[n]-h[i])+pre-w[1]-w[i]-w[n]-j);
            }
            if (it!=save[j].begin())
            {
                --it;
                ans=min(ans,sqr(h[1]-(*it))+sqr((*it)-h[i])+sqr(h[n]-h[i])+pre-w[1]-w[i]-w[n]-j);
            }
        }
        save[w[i]].insert(h[i]);
    }
    cout<<ans<<endl;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    freopen("xaycau.INP","r",stdin);
    freopen("xaycau.OUT","w",stdout);
    scanf("%lld",&n);
    for (int i=1;i<=n;i++)
        scanf("%lld",&h[i]);
    bool ok=true;
    for (int i=1;i<=n;i++)
    {
        scanf("%lld",&w[i]);
        pre+=w[i];
        if (abs(w[i])>20) ok=false;
    }
    if (!ok)sub1();
    else sub2();
    //sub2();
    return 0;
}
// sub1 : n<=1000 , w lớn => vét n^2, gọi dp[i] là giá trị nhỏ nhất xây cầu từ 1-> n
// sub2 : n lớn, abs(w) <=20 , chỉ cần chọn 2 cột giữa 1 và n
//          => for theo w và chọn cột có chiều cao tối ưu giữa i và 1
//          => chọn cột có chiều cao gần với (h[i]+h[1])/2 nhất

    {
        scanf("%lld",&w[i]);
        pre+=w[i];
        if (abs(w[i])>20) ok=false;
    }
    if (!ok)sub1();
    else sub2();
    //sub2();
    return 0;
}
// sub1 : n<=1000 , w lớn => vét n^2, gọi dp[i] là giá trị nhỏ nhất xây cầu từ 1-> n
// sub2 : n lớn, abs(w) <=20 , chỉ cần chọn 2 cột giữa 1 và n
//          => for theo w và chọn cột có chiều cao tối ưu giữa i và 1
//          => chọn cột có chiều cao gần với (h[i]+h[1])/2 nhất

Compilation message

building.cpp:63:27: error: stray '#' in program
   63 |     for (int i=1;i<=n;i++)#include <bits/stdc++.h>
      |                           ^
building.cpp: In function 'int main()':
building.cpp:63:28: error: 'include' was not declared in this scope
   63 |     for (int i=1;i<=n;i++)#include <bits/stdc++.h>
      |                            ^~~~~~~
building.cpp:63:37: error: 'bits' was not declared in this scope
   63 |     for (int i=1;i<=n;i++)#include <bits/stdc++.h>
      |                                     ^~~~
building.cpp:63:42: error: 'stdc' was not declared in this scope; did you mean 'std'?
   63 |     for (int i=1;i<=n;i++)#include <bits/stdc++.h>
      |                                          ^~~~
      |                                          std
building.cpp:64:9: error: '#pragma' is not allowed here
   64 | #pragma GCC optimize("O3")
      |         ^~~
building.cpp:71:1: error: a function-definition is not allowed here before '{' token
   71 | {
      | ^
building.cpp:86:1: error: a function-definition is not allowed here before '{' token
   86 | {
      | ^
building.cpp:90:1: error: a function-definition is not allowed here before '{' token
   90 | {
      | ^
building.cpp:116:1: error: a function-definition is not allowed here before '{' token
  116 | {
      | ^
building.cpp:142:25: error: 'i' was not declared in this scope
  142 |         scanf("%lld",&w[i]);
      |                         ^
building.cpp:69:4: warning: unused variable 'res' [-Wunused-variable]
   69 | ll res,n,h[200005],w[200005],max1[200005],pre,dp[200005];
      |    ^~~
building.cpp:69:8: warning: unused variable 'n' [-Wunused-variable]
   69 | ll res,n,h[200005],w[200005],max1[200005],pre,dp[200005];
      |        ^
building.cpp:69:10: warning: unused variable 'h' [-Wunused-variable]
   69 | ll res,n,h[200005],w[200005],max1[200005],pre,dp[200005];
      |          ^
building.cpp:69:30: warning: unused variable 'max1' [-Wunused-variable]
   69 | ll res,n,h[200005],w[200005],max1[200005],pre,dp[200005];
      |                              ^~~~
building.cpp:69:47: warning: unused variable 'dp' [-Wunused-variable]
   69 | ll res,n,h[200005],w[200005],max1[200005],pre,dp[200005];
      |                                               ^~
building.cpp:57:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   57 |     freopen("xaycau.INP","r",stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
building.cpp:58:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   58 |     freopen("xaycau.OUT","w",stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
building.cpp:59:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   59 |     scanf("%lld",&n);
      |     ~~~~~^~~~~~~~~~~
building.cpp:61:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   61 |         scanf("%lld",&h[i]);
      |         ~~~~~^~~~~~~~~~~~~~