2023杭电多校(二)

1002 Binary Number

Binary Number

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 276    Accepted Submission(s): 59


 

Problem Description
Markyyz is learning binary numbers. There is an easy problem in his homework.

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?
 

Input
The first line of the input contains a single integer T (1≤T≤6×104), indicating the number of test cases.

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
 

Output
You need to print a string of length n in one line, representing the biggest binary number after the k operations.
 

Sample Input

2 8 2 10100101 5 233333333333333333 11101
 

Sample Output

11111101
11111
 

题解:

(一)当 s=1,k为偶数时 ,输出0;k为奇数时,输出1;

(二)当 s全为1,k=1时,末尾字符变成0输出即可;

  (三)   记录全为0的块数 ct;

                当ct<=k  全1

                当ct

        因为10序列有特殊性变为全一可有以下情况:

一次:10——>11

两次:10——>01——>11

#include 
#include 
#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;
}

1004 Card Game

Card Game

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 46    Accepted Submission(s): 36


 

Problem Description
Recently, playing card games has become popular. SPY and Markyyz are also playing a game. In this game, the cards must be placed in piles. Any number of cards can be stacked in the same pile. Any card can be placed at the bottom of an empty pile. The stacked cards go from the bottom to the top, with decreasing and consecutive values. For example, piles (5,4,3,2,1), (8,7,6,5,4,3), (9), and () (an empty pile) are all valid, while piles (4,2,1) (not consecutive), (1,2,3) (not decreasing), and (9,8,7,5,6,4,3,2) (neither consecutive nor decreasing) are not valid. (The description of the piles mentioned above is in the order from bottom to top).

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.
 

Input
There are multiple test cases in this problem.

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.
 

Output
For each test case, output a single line containing an integer, representing the maximum value of k for the number of cards. Take the modulus of 998244353.
 

Sample Input

3 2 3 114514
 

Sample Output

1 3 766171354
题解:推出n=4是,k最大为7,然后队友猜了个 2^(n-1)-1 。。。。
#include
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;
}

1009 String Problem

String Problem

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 64    Accepted Submission(s): 43


 

Problem Description
Little L raised a question:

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.
 

Input
A positive integer T in the first line represents the number of test groups, for each group of test data:

The only line contains a string of length n which containing only lowercase letters.

T≤20,∑n≤106
 

Output
For each test data, print a number representing the maximum score.
 

Sample Input

2 etxabaxtezwkdwokdbbb aaaaa
 

Sample Output

2 4

题解:统计连续相同字符序列的个数m,用长度n减去m即可

#include
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;
}

你可能感兴趣的:(算法,c++,数据结构)