好久没做cf,四道题读错2道你敢信- -
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.
题意是输入n,a,b,表示有n个字符,a个A,b个B,字符串中'.'表示空位,A B不能相连,问最多能放多少个A和B。
思路:暴力遍历,对于连续的偶数个空位无所谓,奇数个就贪心一下,把第一个放多的,也不用想着怎么直接算出来- -,直接暴力就好了啊,两种写法
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 2e5 + 10;
char str[maxn];
int main()
{
int n, a, b;
while(~scanf("%d%d%d", &n, &a, &b))
{
scanf(" %s", str);
int len = strlen(str);
int ans = 0, flag;
if(a > b) flag = 0;
else flag = 1;
for(int i = 0; i < len; i++)
{
if(str[i] == '.')
{
if(flag)
{
b--; flag = 0;
if(b >= 0) ans++;
}
else if(flag == 0)
{
a--; flag = 1;
if(a >= 0) ans++;
}
}
if(str[i] == '*')
{
if(a < b) flag = 1;
else flag = 0;
}
}
cout << ans << endl;
}
return 0;
}
这种就比较巧妙了
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
const int maxn = 2e5 + 5;
char str[maxn];
int main()
{
int n, a, b;
while(~scanf("%d%d%d", &n, &a, &b))
{
scanf(" %s", str);
int len = strlen(str);
int ans = 0, cnt = 0;
for(int i = 0; i <= len; i++)
{
if(str[i] == '.')
cnt++;
else
{
if(a > b) swap(a, b);
ans += min(a, cnt/2);
a -= min(a, cnt/2);
cnt -= cnt/2;
ans += min(b, cnt);
b -= min(b, cnt);
cnt = 0;
}
}
cout << ans << endl;
}
return 0;
}
题意:最少删掉几个数字可以让剩下的是一个平方数,并且删掉后的数字不能有前导0
思路:这题一眼bfs, 然而有前导0就比较复杂了, 最直接的思路就是bfs+优先队列一下,其实也可以暴力枚举y,让y*y <= n,然后暴力按顺序匹配y与n,看能不能通过删掉字符得出y,维护一个最小值。这样就不存在前导0的问题了
bfs+优先队列代码:
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>
#include <queue>
#include <cmath>
using namespace std;
long long po[20];
struct node
{
string s;
int step;
node(){}
node(string ss, int st)
{
s = ss;
step = st;
}
bool operator < (const node &a) const
{
return step > a.step;
}
};
int bfs(string s)
{
priority_queue<node> q;
q.push(node(s, 0));
while(!q.empty())
{
node p = q.top();
q.pop();
string tmp = p.s;
int num = 0;
// cout << tmp << endl;
int len = tmp.size();
for(int i = len-1; i >= 0; i--)
num += po[len-1-i]*(tmp[i]-'0');
// cout << num << endl;
int x = sqrt(num);
if(x * x == num) return p.step;
for(int i = 0; i < len; i++)
{
string t;
for(int j = 0; j < len; j++)
if(j != i)
t += tmp[j];
if(t == "") continue;
int cur = p.step+1;
int flag = 0;
for(int i = 0; i < t.size(); i++)
{
if(t[i] == '0')
{
if(i == t.size()-1) flag = 1;
cur++;
}
else break;
}
if(flag) continue;
q.push(node(t, cur));
}
}
return -1;
}
int main()
{
po[0] = 1;
for(int i = 1; i <= 12; i++)
po[i] = po[i-1] * 10;
string s;
while(cin >> s)
{
printf("%d\n",bfs(s));
}
return 0;
}
题意:给你一个数组,保证至少有一个数字重复至少2次,每次找出个数>=2最小的那个数字的最左面两个,把最左面的删掉,靠右的*2,问最后的数组剩下什么
思路:这题没读错- -,很快a了, 一个优先队列维护数组,一个vector存答案就好了
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 5;
int a[maxn];
struct node
{
ll val, index;
node(){}
node(ll ii, ll vv) : index(ii), val(vv){}
bool operator < (const node &a) const
{
if(val == a.val)
return index > a.index;
else
return val > a.val;
}
};
//struct node1
//{
// int val, index;
// node1(){}
// node1(int ii, int vv) : index(ii), val(vv){}
// bool operator < (const node1 &a) const
// {
// return index < a.index;
// }
//};
int cmp(node a, node b)
{
return a.index < b.index;
}
int main()
{
int n;
while(~scanf("%d", &n))
{
priority_queue<node> pq;
vector<node> ans;
for(int i = 1; i <= n; i++)
{
scanf("%d", &a[i]);
pq.push(node(i, a[i]));
}
while(pq.size())
{
node p = pq.top();
pq.pop();
if(pq.empty())
{
ans.push_back(p);
break;
}
if(p.val != pq.top().val)
ans.push_back(p);
else
{
p = pq.top();
pq.pop();
p.val *= 2;
pq.push(p);
}
}
printf("%d\n", ans.size());
sort(ans.begin(), ans.end(),cmp);
for(int i = 0; i < ans.size(); i++)
printf("%lld ", ans[i].val);
cout << endl;
}
return 0;
}