Submission #362068

#TimeUsernameProblemLanguageResultExecution timeMemory
362068shahriarkhanK blocks (IZhO14_blocks)C++14
100 / 100
595 ms81284 KiB
#include<bits/stdc++.h>
using namespace std ;

const int INF = 1e9 ;

int main()
{
    int n , k ;
    scanf("%d%d",&n,&k) ;
    int a[n+1] ;
    for(int i = 1 ; i <= n ; ++i) scanf("%d",&a[i]) ;
    pair<int,int> dp[n+1][k+1] ;
    dp[0][0] = {0,0} ;
    for(int i = 1 ; i <= n ; ++i) dp[i][0] = {INF,INF} ;
    for(int j = 1 ; j <= k ; ++j)
    {
        stack<pair<int,int> > s ;
        for(int i = j ; i <= n ; ++i)
        {
            dp[i][j] = {dp[i-1][j-1].first + dp[i-1][j-1].second , a[i]} ;
            while(!s.empty() && s.top().second <= a[i])
            {
                if((s.top().first + a[i])<(dp[i][j].first + dp[i][j].second))
                {
                    dp[i][j] = {s.top().first,a[i]} ;
                }
                s.pop() ;
            }
            if(!s.empty())
            {
                if((s.top().first+s.top().second)<(dp[i][j].first+dp[i][j].second))
                {
                    dp[i][j] = s.top() ;
                }
            }
            s.push(dp[i][j]) ;
        }
    }
    printf("%d\n",dp[n][k].first + dp[n][k].second) ;
    return 0 ;
}

Compilation message (stderr)

blocks.cpp: In function 'int main()':
blocks.cpp:9:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
    9 |     scanf("%d%d",&n,&k) ;
      |     ~~~~~^~~~~~~~~~~~~~
blocks.cpp:11:40: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   11 |     for(int i = 1 ; i <= n ; ++i) scanf("%d",&a[i]) ;
      |                                   ~~~~~^~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...