There are nn consecutive seat places in a railway carriage. Each place is either empty or occupied by a passenger.
The university team for the Olympiad consists of aa student-programmers and bb student-athletes. Determine the largest number of students from all a+ba+b students, which you can put in the railway carriage so that:
- no student-programmer is sitting next to the student-programmer;
- and no student-athlete is sitting next to the student-athlete.
In the other words, there should not be two consecutive (adjacent) places where two student-athletes or two student-programmers are sitting.
Consider that initially occupied seat places are occupied by jury members (who obviously are not students at all).
The first line contain three integers nn, aa and bb (1≤n≤2⋅1051≤n≤2⋅105, 0≤a,b≤2⋅1050≤a,b≤2⋅105, a+b>0a+b>0) — total number of seat places in the railway carriage, the number of student-programmers and the number of student-athletes.
The second line contains a string with length nn, consisting of characters "." and "*". The dot means that the corresponding place is empty. The asterisk means that the corresponding place is occupied by the jury member.
Print the largest number of students, which you can put in the railway carriage so that no student-programmer is sitting next to a student-programmer and no student-athlete is sitting next to a student-athlete.
5 1 1 *...*
2
6 2 3 *...*.
4
11 3 10 .*....**.*.
7
3 2 3 ***
0
In the first example you can put all student, for example, in the following way: *.AB*
In the second example you can put four students, for example, in the following way: *BAB*B
In the third example you can put seven students, for example, in the following way: B*ABAB**A*B
The letter A means a student-programmer, and the letter B — student-athlete.
记录每段.的长度,a > b, 对于某段可填区间长度为sum,a最多能填min((sum+1)/2, a), b最多能填min((sum)/2, b);
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
using namespace std;
#define INF 0x3f3f3f3f
#define N 200010
char s[N];
vector <int> v;
int main()
{
int n, a, b, ans;
scanf("%d%d%d", &n, &a, &b);
if(a<b)
swap(a, b);
scanf("%s", s+1);
ans = 0;
int cnt = 0;
for(int i = 1; i <= n; i++) {
if(s[i] == '*') {
if(cnt > 0)
v.push_back(cnt);
cnt = 0;
} else
cnt ++;
}
if(cnt)
v.push_back(cnt);
cnt = 0;
for(int i = 0; i < v.size(); i++) {
if(a==0 && b == 0)
break;
if(a < b)
swap(a,b);
int sum = v[i];
int x = (sum+1)/2, y = sum/2;
y = min(b, y);
x = min(a, x);
a -= x;
b -= y;
ans += y+x;
}
printf("%d\n", ans);
return 0;
}
You are given an array of positive integers. While there are at least two equal elements, we will perform the following operation. We choose the smallest value x that occurs in the array 2 or more times. Take the first two occurrences of x in this array (the two leftmost occurrences). Remove the left of these two occurrences, and the right one is replaced by the sum of this two values (that is, 2⋅x).
Determine how the array will look after described operations are performed.
For example, consider the given array looks like [3,4,1,2,2,1,1]. It will be changed in the following way: [3,4,1,2,2,1,1] → [3,4,2,2,2,1] → [3,4,4,2,1] → [3,8,2,1].
If the given array is look like [1,1,3,1,1] it will be changed in the following way: [1,1,3,1,1] → [2,3,1,1] → [2,3,2] → [3,4].
The first line contains a single integer n (2≤n≤150000) — the number of elements in the array.
The second line contains a sequence from n elements a1,a2,…,an (1≤ai≤109) — the elements of the array.
In the first line print an integer k — the number of elements in the array after all the performed operations. In the second line print kintegers — the elements of the array after all the performed operations.
7 3 4 1 2 2 1 1
4 3 8 2 1
5 1 1 3 1 1
2 3 4
5 10 40 20 50 30
5 10 40 20 50 30
The first two examples were considered in the statement.
In the third example all integers in the given array are distinct, so it will not change.
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <map>
#include <set>
using namespace std;
#define INF 0x3f3f3f3f
#define N 200010
#define MP make_pair
#define ll long long
#define f first
#define s second
map < ll, set<ll> > mp;
set < pair<ll, ll> > ss;
int main()
{
int n;
ll a;
scanf("%d", &n);
for(int i = 1; i <= n; i++) {
scanf("%lld", &a);
mp[a].insert(i);
}
while(mp.size()) {
auto it = *mp.begin();
mp.erase(mp.begin());
while(it.s.size() >= 2) {
it.s.erase(it.s.begin());
mp[2*it.f].insert(*it.s.begin());
it.s.erase(it.s.begin());
}
if(it.s.size() == 1) //如果还有残留加入set里
ss.insert(MP(*it.s.begin(),it.f));
}
printf("%d\n", ss.size());
for(set< pair<ll, ll> > ::iterator it = ss.begin(); it != ss.end(); it++)
printf("%lld ", it->s);
return 0;
}