Skip to content

Commit 98cb727

Browse files
Add files via upload
1 parent bdb9097 commit 98cb727

File tree

3 files changed

+176
-0
lines changed

3 files changed

+176
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <algorithm>
4+
5+
using namespace std;
6+
7+
int is_possible(vector <int> &A, int n)
8+
{
9+
for(int i = 1, current = n; i < A.size(); i++, current++)
10+
{
11+
if(abs(A[i] - current) > 1)
12+
{
13+
return false;
14+
}
15+
}
16+
17+
return true;
18+
}
19+
20+
void solve()
21+
{
22+
int no_of_elements;
23+
cin >> no_of_elements;
24+
25+
vector <int> A(no_of_elements + 1);
26+
for(int i = 1; i <= no_of_elements; i++)
27+
{
28+
cin >> A[i];
29+
}
30+
31+
int possible = false;
32+
for(int beginning = A[1] - 1; beginning <= A[1] + 1; beginning++)
33+
{
34+
if(is_possible(A, beginning))
35+
{
36+
possible = true;
37+
}
38+
}
39+
40+
cout << (possible ? "Yes" : "No") << "\n";
41+
}
42+
43+
int main()
44+
{
45+
int no_of_test_cases;
46+
cin >> no_of_test_cases;
47+
48+
while(no_of_test_cases--)
49+
solve();
50+
51+
return 0;
52+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <algorithm>
4+
5+
using namespace std;
6+
7+
void solve()
8+
{
9+
int no_of_elements, budget;
10+
cin >> no_of_elements >> budget;
11+
12+
vector <int> A(no_of_elements + 1);
13+
for(int i = 1; i <= no_of_elements; i++)
14+
{
15+
cin >> A[i];
16+
}
17+
18+
sort(A.begin(), A.end());
19+
20+
vector <long long> prefix_sum(no_of_elements + 1);
21+
for(int i = 1; i <= no_of_elements; i++)
22+
{
23+
prefix_sum[i] = prefix_sum[i - 1] + A[i];
24+
}
25+
26+
vector <int> no_of_days_used(no_of_elements + 2, 0);
27+
for(int i = 1; i <= no_of_elements; i++)
28+
{
29+
if(prefix_sum[i] > budget)
30+
{
31+
break;
32+
}
33+
34+
long long low = 1, high = 1e9 + 1;
35+
while(high - low > 1)
36+
{
37+
long long mid = (low + high)/2;
38+
39+
if(( prefix_sum[i] + (mid - 1)*i ) > budget)
40+
{
41+
high = mid;
42+
}
43+
else
44+
{
45+
low = mid;
46+
}
47+
}
48+
49+
no_of_days_used[i] = low;
50+
51+
//cout << "i = " << i << " Days = " << no_of_days_used[i] << "\n";
52+
}
53+
54+
long long answer = 0;
55+
for(int i = 1; i <= no_of_elements; i++)
56+
{
57+
answer += (no_of_days_used[i] - no_of_days_used[i + 1])*i;
58+
}
59+
60+
cout << answer << "\n";
61+
}
62+
63+
int main()
64+
{
65+
int no_of_test_cases;
66+
cin >> no_of_test_cases;
67+
68+
while(no_of_test_cases--)
69+
solve();
70+
71+
return 0;
72+
}
73+
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#include <iostream>
2+
3+
using namespace std;
4+
5+
void solve()
6+
{
7+
string S;
8+
cin >> S;
9+
10+
int possible = (S.size() == 1 ? false : true);
11+
for(int i = 0; i < S.size(); i++)
12+
{
13+
if(i == 0)
14+
{
15+
if(S[i] != S[i + 1])
16+
{
17+
possible = false;
18+
}
19+
continue;
20+
}
21+
22+
if(i + 1 == S.size())
23+
{
24+
if(S[i] != S[i - 1])
25+
{
26+
possible = false;
27+
}
28+
29+
break;
30+
}
31+
32+
if(S[i - 1] != S[i] && S[i] != S[i + 1])
33+
{
34+
possible = false;
35+
break;
36+
}
37+
}
38+
39+
cout << (possible ? "YES" : "NO") << "\n";
40+
}
41+
42+
int main()
43+
{
44+
int no_of_test_cases;
45+
cin >> no_of_test_cases;
46+
47+
while(no_of_test_cases--)
48+
solve();
49+
50+
return 0;
51+
}

0 commit comments

Comments
 (0)