#include<bits/stdc++.h>
using namespace std;
#define int long long
void test(){
int n;cin >> n;
string s;cin >> s;
string ans = "";
ans = ans + s[0];
char no = s[0];
for(int i = 1;i < n;i++){
if(s[i] == no){
i++;
if(i < n)
ans = ans + s[i];
no = s[i];
}
}
cout << ans << endl;
}
#undef int
int main(){
int t = 1;cin >> t;
while(t--){
test();
}
}
B. Binary Cafe
题意:用
2
i
2^i
2i来配数,不能超过
n
n
n,
0
≤
i
≤
k
−
1
0\leq i\leq k-1
0≤i≤k−1
思路:如果没有n的限制,那么根据二进制可以配出
0
到
2
k
−
1
0到2^k-1
0到2k−1的所有数,所以只要比较
2
k
−
1
2^k-1
2k−1与
n
n
n的大小,取小的加1即可,由于
k
k
k可以到1000,因此不能直接用二进制,用循环即可
#include<bits/stdc++.h>
using namespace std;
#define int long long
void test(){
int n,k;cin >> n >> k;
int no = 1;
while(no - 1 <= n && k--) no *= 2;
if(no - 1 <= n) cout << no << endl;
else cout << n + 1 << endl;
}
#undef int
int main(){
int t = 1;cin >> t;
while(t--){
test();
}
}
C. Ski Resort
题意:给出一个序列,取至少连续的k个数,其中的最大值不超过q,问有几种取法
思路:那些超过q的数将连续的断开,对于完整的一段,长度如果为n,那么至少连续取k个数的方法数就为
(
n
−
k
+
1
)
+
(
n
−
k
)
+
(
n
−
k
−
1
)
+
.
.
.
+
1
(n-k+1)+(n-k)+(n-k-1)+...+1
(n−k+1)+(n−k)+(n−k−1)+...+1,也就是
[
(
n
−
k
+
2
)
∗
(
n
−
k
+
1
)
]
/
2
[(n-k+2)*(n-k+1)]/2
[(n−k+2)∗(n−k+1)]/2
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 2e5 + 10;
int a[N];
void test(){
int n,k,q;cin >> n >> k >> q;
int no = 0;
int ans = 0;
for(int i = 1;i <= n;i++){
cin >> a[i];
if(a[i] <= q)
no++;
else{
if(no - k + 1 > 0)
ans += max((int)0,(no - k + 2)*(no - k + 1)/2);
no = 0;
}
}
if(no - k + 1 > 0)
ans += max((int)0,(no - k + 2)*(no - k + 1)/2);
cout << ans << endl;
}
#undef int
int main(){
int t = 1;cin >> t;
while(t--){
test();
}
}
D. Wooden Toy Festival
题意:取三个数,让所有数取的
∣
x
−
y
∣
|x-y|
∣x−y∣的最大值最小
思路:取三个数也就是将所有数分成三分,每个数对应一个区域,这个区域肯定是连续的,因此先sort
然后枚举最后的区间,然后用二分找中间区间的最适合位置
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 2e5 + 10;
int a[N];
void test(){
int n;cin >> n;
for(int i = 1;i <= n;i++)
cin >> a[i];
sort(a + 1,a + 1 + n);
int mi = 1e9 + 10;
if(n <= 3)
cout << 0 << endl;
else{
int r = 2;
for(;r <= n - 1;r++){
int rr = (a[n] - a[r + 1] + 1) / 2;
int x = (a[r] - a[2] + 1) / 2;
int _l = 1,_r = r;
while(_l <= _r){
int _mid = (_l + _r) / 2;
int _ll = (a[_mid - 1] - a[1] + 1) / 2;
int _rr = (a[r] - a[_mid] + 1) / 2;
x = min(max(_ll,_rr),x);
if(_ll > _rr)
_r = _mid - 1;
else
_l = _mid + 1;
}
x = max(x,rr);
mi = min(x,mi);
}
cout << mi << endl;
}
}
#undef int
int main(){
int t = 1;cin >> t;
while(t--){
test();
}
}