Skip to content

Commit 9686a02

Browse files
Create Qpwoeirut And The City.cpp
1 parent b01e3d1 commit 9686a02

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <algorithm>
4+
5+
#define all(v) (v).begin(), (v).end()
6+
using namespace std;
7+
8+
void solve()
9+
{
10+
int no_of_elements;
11+
cin >> no_of_elements;
12+
13+
vector <int> A(no_of_elements + 1);
14+
for(int i = 1; i <= no_of_elements; i++)
15+
{
16+
cin >> A[i];
17+
}
18+
19+
const long long oo = 1e18;
20+
vector <long long> minimum_cost(no_of_elements + 1, oo), maximum_coverage(no_of_elements + 1, 0);
21+
for(int i = 2; i <= no_of_elements - 1; i++)
22+
{
23+
long long amount_here = max(0, max(A[i - 1], A[i + 1]) + 1 - A[i]);
24+
25+
if(i < 4)
26+
{
27+
minimum_cost[i] = amount_here;
28+
maximum_coverage[i] = 1;
29+
}
30+
else if(i >= 4)
31+
{
32+
//minimum_cost[i] = amount_here + min(minimum_cost[i - 2], minimum_cost[i - 3]);
33+
34+
if(maximum_coverage[i - 3] == maximum_coverage[i - 2] && minimum_cost[i - 3] < minimum_cost[i - 2])
35+
{
36+
minimum_cost[i] = amount_here + minimum_cost[i - 3];
37+
maximum_coverage[i] = maximum_coverage[i - 3] + 1;
38+
}
39+
else
40+
{
41+
minimum_cost[i] = amount_here + minimum_cost[i - 2];
42+
maximum_coverage[i] = maximum_coverage[i - 2] + 1;
43+
}
44+
}
45+
46+
//cout << "F(" << i << ") = " << minimum_cost[i] << "\n";
47+
}
48+
49+
long long answer;
50+
51+
if(maximum_coverage[no_of_elements - 1] == maximum_coverage[no_of_elements - 2])
52+
{
53+
answer = min(minimum_cost[no_of_elements - 1], minimum_cost[no_of_elements - 2]);
54+
}
55+
else
56+
{
57+
answer = (maximum_coverage[no_of_elements - 1] > maximum_coverage[no_of_elements - 2] ?
58+
minimum_cost[no_of_elements - 1] : minimum_cost[no_of_elements - 2]);
59+
}
60+
61+
cout << answer << "\n";
62+
}
63+
64+
int main()
65+
{
66+
int no_of_test_cases;
67+
cin >> no_of_test_cases;
68+
69+
while(no_of_test_cases--)
70+
solve();
71+
72+
return 0;
73+
}
74+
75+
 

0 commit comments

Comments
 (0)