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 675e3b1

Browse files
Add files via upload
1 parent e27da64 commit 675e3b1

File tree

3 files changed

+162
-0
lines changed

3 files changed

+162
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
The beginning element is either A[i] - 1, A[i], A[i] + 1
2+
3+
Let us notice that the first element uniquely determines the whole sequence.
4+
5+
We will check if any of these 3 options leads to a consecutive segment.
6+
7+
-----
8+
9+
int is_possible(vector <int> &A, int n)
10+
{
11+
for(int i = 1, current = n; i < A.size(); i++, current++)
12+
{
13+
if(abs(A[i] - current) > 1)
14+
{
15+
return false;
16+
}
17+
}
18+
19+
return true;
20+
}
21+
22+
void solve()
23+
{
24+
int no_of_elements;
25+
cin >> no_of_elements;
26+
27+
vector <int> A(no_of_elements + 1);
28+
for(int i = 1; i <= no_of_elements; i++)
29+
{
30+
cin >> A[i];
31+
}
32+
33+
int possible = false;
34+
for(int beginning = A[1] - 1; beginning <= A[1] + 1; beginning++)
35+
{
36+
if(is_possible(A, beginning))
37+
{
38+
possible = true;
39+
}
40+
}
41+
42+
cout << (possible ? "Yes" : "No") << "\n";
43+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
Let us keep an array which will tell us the maximum number of times we can buy a prefix of length i
2+
3+
Let P[i] be the perfix sum of A[1, ... i]
4+
5+
We can buy the prefix for i days if
6+
7+
P[i] + (x - 1)i <= Budget
8+
9+
We can binary search for the value of x for each array element.
10+
11+
------
12+
13+
After that, we will try to find out the number of times we have bought a segment ending at i, exactly
14+
15+
In order to do this, we will notice that we can buy the prefix ending at i
16+
17+
no_of_days[i] - no_of_days[i + 1] times.
18+
19+
-----
20+
21+
22+
void solve()
23+
{
24+
int no_of_elements, budget;
25+
cin >> no_of_elements >> budget;
26+
27+
vector <int> A(no_of_elements + 1);
28+
for(int i = 1; i <= no_of_elements; i++)
29+
{
30+
cin >> A[i];
31+
}
32+
33+
sort(A.begin(), A.end());
34+
35+
vector <long long> prefix_sum(no_of_elements + 1);
36+
for(int i = 1; i <= no_of_elements; i++)
37+
{
38+
prefix_sum[i] = prefix_sum[i - 1] + A[i];
39+
}
40+
41+
vector <int> no_of_days_used(no_of_elements + 2, 0);
42+
for(int i = 1; i <= no_of_elements; i++)
43+
{
44+
if(prefix_sum[i] > budget)
45+
{
46+
break;
47+
}
48+
49+
long long low = 1, high = 1e9 + 1;
50+
while(high - low > 1)
51+
{
52+
long long mid = (low + high)/2;
53+
54+
if(( prefix_sum[i] + (mid - 1)*i ) > budget)
55+
{
56+
high = mid;
57+
}
58+
else
59+
{
60+
low = mid;
61+
}
62+
}
63+
64+
no_of_days_used[i] = low;
65+
66+
//cout << "i = " << i << " Days = " << no_of_days_used[i] << "\n";
67+
}
68+
69+
long long answer = 0;
70+
for(int i = 1; i <= no_of_elements; i++)
71+
{
72+
answer += (no_of_days_used[i] - no_of_days_used[i + 1])*i;
73+
}
74+
75+
cout << answer << "\n";
76+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
If the length of the segment is even, we can just keep using blocks of 2
2+
If the length of the segment is odd, we can use one blocks of 3, after which it is even and reduced to the case above.
3+
4+
The only time it is impossible is when the length of the segment is 1.
5+
6+
-----
7+
8+
void solve()
9+
{
10+
string S;
11+
cin >> S;
12+
13+
int possible = (S.size() == 1 ? false : true);
14+
for(int i = 0; i < S.size(); i++)
15+
{
16+
if(i == 0)
17+
{
18+
if(S[i] != S[i + 1])
19+
{
20+
possible = false;
21+
}
22+
continue;
23+
}
24+
25+
if(i + 1 == S.size())
26+
{
27+
if(S[i] != S[i - 1])
28+
{
29+
possible = false;
30+
}
31+
32+
break;
33+
}
34+
35+
if(S[i - 1] != S[i] && S[i] != S[i + 1])
36+
{
37+
possible = false;
38+
break;
39+
}
40+
}
41+
42+
cout << (possible ? "YES" : "NO") << "\n";
43+
}

0 commit comments

Comments
(0)

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