234C - Weather - CodeForces Solution


dp implementation *1300

Please click on ads to support us..

Python Code:

t=*map(int,open('input.txt','r').readlines()[1].split()),
c=sum(x<=0 for x in t);n=len(t)
for v in t[:-1]:c+=(v>0)-(v<0);n=min(n,c)
print(n,file=open('output.txt','w'))



    

C++ Code:

#include <map>
#include <iostream>
#include <set>
#include <vector>
#include <string>
#include <fstream>
#define ull unsigned long long
#define ll long long


using namespace std;

int main() {
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    
    ll n;
    cin >> n;
    vector<ll> mass(n);
    vector<ll> dp1(n);
    vector<ll> dp2(n);

    
    for (ll i = 0; i < n; ++i) {
        cin >> mass[i];
        dp1[i]=0, dp2[i]=0;
    }

    for (ll i = 1; i < n; ++i) {
        dp1[i] = dp1[i-1];
        if (mass[i-1] >= 0) {
            ++dp1[i];
        }
    }
    for (ll i = n-2; i >= 0; --i) {
        dp2[i] = dp2[i+1];
        if (mass[i+1] <= 0) {
            ++dp2[i];
        } 
    }
    ll res = 999999999;
    for (ll i = 1; i < n-1; ++i) {
        if (mass[i] == 0) {
            res = min(res, dp1[i] + dp2[i] + 1);
        } else {
            res = min(res, dp1[i] + dp2[i]);
        }
    } 
    if (mass[n-1] > 0) {
        res = min(res, dp1[n-1] + dp2[n-1]);
    } if (mass[0] < 0) {
        res = min(res, dp1[0] + dp2[0]);
    } if (res == 999999999) {
        res = mass.size();
    }
    cout << res;
    return 0;
}
/* Sun Mar 12 2023 14:39:01 GMT+0300 (Moscow Standard Time) */

/* Sun Mar 12 2023 14:39:06 GMT+0300 (Moscow Standard Time) */

/* Sun Mar 12 2023 14:39:21 GMT+0300 (Moscow Standard Time) */

/* Sun Mar 12 2023 15:10:56 GMT+0300 (Moscow Standard Time) */

/* Sun Mar 12 2023 15:11:02 GMT+0300 (Moscow Standard Time) */

/* Sun Mar 12 2023 15:11:17 GMT+0300 (Moscow Standard Time) */

/* Sun Mar 12 2023 15:11:56 GMT+0300 (Moscow Standard Time) */

/* Sun Mar 12 2023 15:12:02 GMT+0300 (Moscow Standard Time) */

/* Sun Mar 12 2023 15:12:17 GMT+0300 (Moscow Standard Time) */

/* Sun Mar 12 2023 15:59:10 GMT+0300 (Moscow Standard Time) */

/* Sun Mar 12 2023 15:59:16 GMT+0300 (Moscow Standard Time) */

/* Sun Mar 12 2023 15:59:32 GMT+0300 (Moscow Standard Time) */

/* Mon Mar 13 2023 08:07:34 GMT+0300 (Moscow Standard Time) */


Comments

Submit
0 Comments
More Questions

987. Vertical Order Traversal of a Binary Tree
952. Largest Component Size by Common Factor
212. Word Search II
174. Dungeon Game
127. Word Ladder
123. Best Time to Buy and Sell Stock III
85. Maximal Rectangle
84. Largest Rectangle in Histogram
60. Permutation Sequence
42. Trapping Rain Water
32. Longest Valid Parentheses
Cutting a material
Bubble Sort
Number of triangles
AND path in a binary tree
Factorial equations
Removal of vertices
Happy segments
Cyclic shifts
Zoos
Build a graph
Almost correct bracket sequence
Count of integers
Differences of the permutations
Doctor's Secret
Back to School
I am Easy
Teddy and Tweety
Partitioning binary strings
Special sets