Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit def144a

Browse files
Add files via upload
1 parent 6fd6dd4 commit def144a

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
We have to ask ourselves how many segments run through i ?
2+
3+
Segments Run through i = Segments Run through (i - 1) - Segments Ending at (i - 1) + segments starting at (i)
4+
5+
Case 1
6+
7+
If (A[i] > A[i - 1])
8+
9+
It means that (A[i] - A[i - 1]) new segments start at i
10+
11+
------
12+
13+
Case 2
14+
15+
If (A[i] < A[i - 1])
16+
17+
It means that (A[i - 1] - A[i]) segments end at (i - 1)
18+
19+
-----
20+
21+
Case 3
22+
23+
If (A[i] = A[i - 1])
24+
25+
It means that the same number of segments run through both i and (i - 1)
26+
27+
------
28+
29+
Let us add the number of segments starting at every position from i = 1 to N
30+
31+
This is max(0, A[i] - A[i - 1])
32+
33+
-----
34+
35+
int main()
36+
{
37+
int no_of_elements;
38+
cin >> no_of_elements;
39+
40+
vector <int> A(no_of_elements + 1);
41+
for(int i = 1; i <= no_of_elements; i++)
42+
{
43+
cin >> A[i];
44+
}
45+
46+
int segments = 0;
47+
for(int i = 1; i <= no_of_elements; i++)
48+
{
49+
int segments_starting_here = max(0, A[i] - A[i - 1]);
50+
segments += segments_starting_here;
51+
}
52+
53+
cout << segments << "\n";
54+
return 0;
55+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
The area of a right triangle is (base x height)/2
2+
3+
It is guaranteed that the area of the triangle is an integer in this problem so we don't need to handle floating points
4+
5+
------
6+
7+
int main()
8+
{
9+
int height, base, hypotenuse;
10+
cin >> height >> base >> hypotenuse;
11+
12+
int area = (height*base)/2;
13+
cout << area << "\n";
14+
15+
return 0;
16+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /