Binary Number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 276 Accepted Submission(s): 59
You are given a binary number s1∼n (s1 is the highest bit. sn is the lowest bit.). You need to do an operation exactly k times: select an interval [l,r] (1≤l≤r≤n) arbitrarily and flip sl,sl+1,...,sr, in other word, for all i∈[l,r], si becomes 1 if si is 0, si becomes 0 if si is 1. What is the biggest result binary number after the k operations.
Markyyz found useless algorithms useless on the problem, so he asked SPY for help. SPY looked down on the problem but finally got WA (wrong answer). Can you help them to find the right solution?
In each test case:
The first line contains two integers n,k. (1≤n≤105,0≤k≤1018)
The second line contains a binary number s1∼n. (s1=1, ∀i∈[2,n]:si∈{0,1})
It's guarenteed that in all test cases, ∑n≤2.5×106
题解:
(一)当 s=1,k为偶数时 ,输出0;k为奇数时,输出1;
(二)当 s全为1,k=1时,末尾字符变成0输出即可;
(三) 记录全为0的块数 ct;
当ct<=k 全1
当ct<k 前k个0块置1
因为10序列有特殊性变为全一可有以下情况:
一次:10——>11
两次:10——>01——>11
#include <iostream>
#include <algorithm>
#define ll long long
using namespace std;
int main()
{
int t;
cin >> t;
while (t--)
{
ll n, k;
cin >> n >> k;
string s;
cin >> s;
if (k == 0)
{
cout << s << "\n";
continue;
}
if (n == 1)
{
if (k % 2 == 1)
{
cout << (s[0] == '0' ? '1' : '0') << "\n";
}
else
{
cout << s[0] << "\n";
}
continue;
}
int len = s.length();
int cnt1 = 0, cnt2 = 0; // 0个数 0块数
for (int i = 0; i < len;)
{
if (s[i] == '0')
{
cnt2++;
while (s[i] == '0' && i < len)
{
cnt1++;
i++;
}
}
else
{
i++;
}
}
// cout << cnt1 << " " << cnt2 << "\n";
if (cnt2 == 0 && k == 1)
{
for (int i = 0; i < len - 1; i++)
{
cout << 1;
}
cout << 0 << "\n";
}
else
{
for (int i = 0; i < len;)
{
if (s[i] == '0')
{
k--;
while (s[i] == '0' && i < len)
{
s[i] = '1';
i++;
}
if (k == 0)
{
break;
}
}
else
{
i++;
}
}
cout << s << "\n";
}
}
return 0;
}
Card Game
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 46 Accepted Submission(s): 36
In one move, a player can choose a card from the top of a non-empty pile and move it to the top of another pile. Throughout the player's moves, the stacking rules of the cards must be followed, otherwise it is considered a foul.
SPY is now playing the card game on a table with n piles, where one pile contains k cards (k,k−1,k−2,...,2,1), called pile 1, and the rest of the piles are empty piles. All the free slots are empty. SPY wants to move all the cards from pile 1 to another pile (pile 2). At this point, clever Markyyz comes up with a question:
Given the number of piles n, under the condition of not fouling, what is the maximum value of k that allows the movement of k cards as described above?
Since the answer could be large, take the modulus of 998244353.
The first line of input contains a positive integer t (1≤t≤105), indicating the number of test cases.
Afterwards, there are t test cases. Each test case consists of a single line containing an integer n (2≤n≤109), representing the number of piles.
#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int N = 1e5 + 10;
const ll mod = 998244353;
ll qmi(ll m, ll k, ll p) {
ll res = 1 % p, t = m;
while (k) {
if (k & 1) res = res * t % p;
t = t * t % p;
k >>= 1;
}
return res;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int T;
cin >> T;
while (T--) {
ll n;
cin >> n;
ll ans = qmi(2, n - 1, mod) - 1;
cout << ans << '\n';
}
return 0;
}
String Problem
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 64 Accepted Submission(s): 43
Given a string S of length n containing only lowercase letters.
You need to select several non-empty substrings of S so that they are disjoint pairwise, and each substring is a palindrome.
Assuming you have selected K substrings(s1,s2...sk) that satisfy the above conditions, your score is the sum of the lengths of all substrings minus K. It is ∑Ki=1len(si)−K
But Little L is a dedicated person, and to increase difficulty, Little L requires that each palindrome string contain at most one kind of letter
Little L wants you to find the maximum score.
The only line contains a string of length n which containing only lowercase letters.
T≤20,∑n≤106
题解:统计连续相同字符序列的个数m,用长度n减去m即可
#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int N = 1e5 + 10;
const ll mod = 212370440130137957ll;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int T;
cin >> T;
while (T--) {
string s;
cin >> s;
int ans = 0, n = s.size();
for (int i = 0; i < n; i++) {
int cnt = 0;
int j = i + 1;
while (s[i] == s[j]) {
cnt++;
j++;
}
ans += cnt;
i = j - 1;
}
cout << ans << '\n';
}
return 0;
}