暑假训练三阶段day1

Similar Strings

Putting two similar strings together will create a very strong power that can quake the earth into parts and the person who did that can be called the Destroyer of the World. In order to stop the Destroyer of the World, WNJXYK decided to check every string in this world to make sure there is no pair of strings are similar. But some strings are too long that WNJXYK cannot judge whether they are similar to others, so he turns to you for help.
Now WNJXYK need you to determine the maximum s, for string A has a substring A’ and string B has a substring B’ whose length are both s and they differ on at most K positions. 

Input
The input starts with one line contains exactly one positive integer T which is the number of test cases.
Each test case contains three lines. The first line contains an integer K and each of the following two lines contains a string indicates stringA or stringB
Output
For each test case, output one line containing “y” where y is the maximum s.
Sample Input

3
3
qwertypoi
qwetyrio
0
qqqqq
qqqaqqqq
10
qwertyuiop
asdfghjklzxcvbnm

Sample Output

6
4
10

Hint

1<=T<=5,1<=len(A),len(B)<=4000,0<=K<=min{len(A),len(B)}
Strings only contain lowercase English letters.

题意:给两个字符串 A 和 B,要求求出长度都为 S 的子串 A’ 和 B’ 中最多有 K 个位置不同的最长长度 S

题解:按要求尺取

#include<iostream>

using namespace std;

int ans;
int k;


void fun(string str, string strr) {
    int l = min(str.size(),strr.size());
    int cnt, rr;
    rr = -1;
    cnt = 0;
    for (int ll = 0; ll < l; ++ll) {
        while (rr + 1 < l) {
            if (str[rr + 1] != strr[rr + 1]) {
                if (cnt + 1 > k)
                    break;
                cnt++;
            }
            rr++;
        }
        ans = max(ans, rr - ll + 1);
        if (str[ll] != strr[ll])
            --cnt;
    }
}

int main() {
    int t;
    cin >> t;
    while (t--) {
        string s1, s2;
        ans = 0;
        cin >> k;
        cin >> s1 >> s2;
        int l = s1.size();
        for (int i = 0; i < l; i++) {
            string a(s1, i);
            fun(a, s2);
        }
        l = s2.size();
        for (int i = 0; i < l; i++) {
            string a(s2, i);
            fun(a, s1);
        }
        cout << ans << endl;
    }
    return 0;
}

card card card

Loading [MathJax]/jax/output/HTML-CSS/jax.js
As a fan of Doudizhu, WYJ likes collecting playing cards very much.
One day, MJF takes a stack of cards and talks to him: let’s play a game and if you win, you can get all these cards. MJF randomly assigns these cards into n heaps, arranges in a row, and sets a value on each heap, which is called “penalty value”.
Before the game starts, WYJ can move the foremost heap to the end any times.
After that, WYJ takes the heap of cards one by one, each time he needs to move all cards of the current heap to his hands and face them up, then he turns over some cards and the number of cards he turned is equal to the penaltyvalue.
If at one moment, the number of cards he holds which are face-up is less than the penaltyvalue, then the game ends. And WYJ can get all the cards in his hands (both face-up and face-down).
Your task is to help WYJ maximize the number of cards he can get in the end.So he needs to decide how many heaps that he should move to the end before the game starts. Can you help him find the answer?
MJF also guarantees that the sum of all “penalty value” is exactly equal to the number of all cards.
Input
There are about 10 test cases ending up with EOF.
For each test case:
the first line is an integer n (1≤n≤106), denoting n heaps of cards;
next line contains n integers, the ith integer ai (0≤ai≤1000) denoting there are ai cards in ith heap;
then the third line also contains n integers, the ith integer bi (1≤bi≤1000) denoting the “penalty value” of ith heap is bi.
Output
For each test case, print only an integer, denoting the number of piles WYJ needs to move before the game starts. If there are multiple solutions, print the smallest one.
Sample Input

5
4 6 2 8 4
1 5 7 9 2

Sample Output

4

Hint

[pre]
For the sample input:

+ If WYJ doesn't move the cards pile, when the game starts the state of cards is:
	4 6 2 8 4
	1 5 7 9 2
WYJ can take the first three piles of cards, and during the process, the number of face-up cards is 4-1+6-5+2-7. Then he can't pay the the "penalty value" of the third pile, the game ends. WYJ will get 12 cards.
+ If WYJ move the first four piles of cards to the end, when the game starts the state of cards is:
	4 4 6 2 8
	2 1 5 7 9
WYJ can take all the five piles of cards, and during the process, the number of face-up cards is 4-2+4-1+6-5+2-7+8-9. Then he takes all cards, the game ends. WYJ will get 24 cards.

It can be improved that the answer is 4.

**huge input, please use fastIO.**
[/pre]

题意和题解

String

Loading [MathJax]/jax/output/HTML-CSS/jax.js
There is a string S.S only contain lower case English character.(10≤length(S)≤1,000,000)
How many substrings there are that contain at least k(1≤k≤26) distinct characters?
Input
There are multiple test cases. The first line of input contains an integer T(1≤T≤10) indicating the number of test cases. For each test case:

The first line contains string S.
The second line contains a integer k(1≤k≤26).

Output
For each test case, output the number of substrings that contain at least k dictinct characters.
Sample Input

2
abcabcabca
4
abcabcabcabc
3

Sample Output

0
55

题意:给定一个只有小写字母字符串 S ,求存在多少个子串至少包含了 k 种子母。
题解:尺取

#include<iostream>

using namespace std;

int main() {
    ios::sync_with_stdio(false);
    int t, k;
    cin >> t;
    while (t--) {
        string s;
        int vis[30] = {0};
        cin >> s >> k;
        int l = s.size(), sum = 0, r = -1;
        long long ans = 0;
        for (int i = 0; i < l; i++) {
            while (r + 2 <= l) {
                if (sum == k)
                    break;
                r++;
                vis[s[r] - 'a']++;
                if (vis[s[r] - 'a'] == 1)
                    sum++;
            }
            if (sum == k)
                ans += l - r;
            vis[s[i] - 'a']--;
            if (vis[s[i] - 'a'] == 0)
                sum--;
        }
        cout << ans << endl;
    }
}

Complete the Sequence

You probably know those quizzes in Sunday magazines: given the sequence 1, 2, 3, 4, 5, what is the next number? Sometimes it is very easy to answer, sometimes it could be pretty hard. Because these "sequence problems" are very popular, ACM wants to implement them into the "Free Time" section of their new WAP portal.
ACM programmers have noticed that some of the quizzes can be solved by describing the sequence by polynomials. For example, the sequence 1, 2, 3, 4, 5 can be easily understood as a trivial polynomial. The next number is 6. But even more complex sequences, like 1, 2, 4, 7, 11, can be described by a polynomial. In this case, 1/2.n^2-1/2.n+1 can be used. Note that even if the members of the sequence are integers, polynomial coefficients may be any real numbers.

Polynomial is an expression in the following form:


P(n) = aD.n^D+aD-1.n^D-1+...+a1.n+a0


. If aD <> 0, the number D is called a degree of the polynomial. Note that constant function P(n) = C can be considered as polynomial of degree 0, and the zero function P(n) = 0 is usually defined to have degree -1.

Input
There is a single positive integer T on the first line of input. It stands for the number of test cases to follow. Each test case consists of two lines. First line of each test case contains two integer numbers S and C separated by a single space, 1 <= S < 100, 1 <= C < 100, (S+C) <= 100. The first number, S, stands for the length of the given sequence, the second number, C is the amount of numbers you are to find to complete the sequence.

The second line of each test case contains S integer numbers X1, X2, ... XS separated by a space. These numbers form the given sequence. The sequence can always be described by a polynomial P(n) such that for every i, Xi = P(i). Among these polynomials, we can find the polynomial Pmin with the lowest possible degree. This polynomial should be used for completing the sequence.

Output
For every test case, your program must print a single line containing C integer numbers, separated by a space. These numbers are the values completing the sequence according to the polynomial of the lowest possible degree. In other words, you are to print values Pmin(S+1), Pmin(S+2), … Pmin(S+C).

It is guaranteed that the results Pmin(S+i) will be non-negative and will fit into the standard integer type.

Sample Input

4
6 3
1 2 3 4 5 6
8 2
1 2 4 7 11 16 22 29
10 2
1 1 1 1 1 1 1 1 1 2
1 10
3

Sample Output

7 8 9
37 46
11 56
3 3 3 3 3 3 3 3 3 3

按题意二维差分

include <iostream>

using namespace std;
const int N = 210;
int a[N][N];

int main(void) {
    int t;
    cin >> t;
    while (t--) {
        int s, c;
        cin >> s >> c;
        for (int i = 0; i < s; i++)
            cin >> a[0][i];
        for (int i = 1; i < s; i++)
            for (int j = 0; i + j < s; j++)
                a[i][j] = a[i - 1][j + 1] - a[i - 1][j];
        for (int i = 1; i <= c; i++)
            a[s - 1][i] = a[s - 1][0];
        for (int i = s - 2; i >= 0; i--)
            for (int j = 0; j < c; j++)
                a[i][s + j - i] = a[i + 1][s + j - i - 1] + a[i][s + j - i - 1];
        for (int i = s; i < s + c; i++) {
            cout << a[0][i];
            if (i != s + c - 1)
                cout << " ";
            else
                cout << endl;
        }
    }
    return 0;
}

Maximum Subrectangle

You are given two arrays [Math Processing Error] and [Math Processing Error] of positive integers, with length [Math Processing Error] and [Math Processing Error]

respectively.

Let [Math Processing Error]
be an [Math Processing Error] matrix, where [Math Processing Error]

.

You need to find a subrectangle of the matrix [Math Processing Error]
such that the sum of its elements is at most [Math Processing Error]

, and its area (the total number of elements) is the largest possible.

Formally, you need to find the largest number [Math Processing Error]
such that it is possible to choose integers [Math Processing Error] subject to [Math Processing Error], [Math Processing Error], [Math Processing Error], and [Math Processing Error]

Input

The first line contains two integers [Math Processing Error]

and [Math Processing Error] ([Math Processing Error]

).

The second line contains [Math Processing Error]
integers [Math Processing Error] ([Math Processing Error]

).

The third line contains [Math Processing Error]
integers [Math Processing Error] ([Math Processing Error]

).

The fourth line contains a single integer [Math Processing Error]
([Math Processing Error]

).

Output

If it is possible to choose four integers [Math Processing Error]

such that [Math Processing Error], [Math Processing Error], and [Math Processing Error], output the largest value of [Math Processing Error] among all such quadruplets, otherwise output [Math Processing Error]

.

Examples
Input

3 3
1 2 3
1 2 3
9

Output

4

Input

5 1
5 4 2 4 5
2
5

Output

1

Note

Matrix from the first sample and the chosen subrectangle (of blue color):

Matrix from the second sample and the chosen subrectangle (of blue color):

题意:有一个长度为 n 的 a 数组和长度为 m 的 b 数组,让这个一维矩阵相乘得到二维的矩阵,求最大子矩阵的和小于或等于 x。

题解:前缀和处理 来计算每个矩阵的右下角都是他们的最小值

#include<iostream>
#include<algorithm>

#define ll long long
using namespace std;
const ll inf = 1e18 + 10;
ll a[2005], b[2005], mi[2005], mj[2005];

int main() {
    int n, m;
    ll x;
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        cin >> x;
        a[i] = a[i - 1] + x;
        mi[i] = inf;
    }
    for (int i = 1; i <= m; i++) {
        cin >> x;
        b[i] = b[i - 1] + x;
        mj[i] = inf;
    }
    cin >> x;
    for (int i = 1; i <= n; i++) {
        for (int j = 0; j < i; j++) {
            mi[i - j] = min(mi[i - j], a[i] - a[j]);
        }
    }
    for (int i = 1; i <= m; i++) {
        for (int j = 0; j < i; j++) {
            mj[i - j] = min(mj[i - j], b[i] - b[j]);
        }
    }
    long long ans = 0;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            if (mi[i] * mj[j] <= x) {
                ans = max(ans, 1ll * i * j);
            }
        }
    }
    cout << ans << endl;
    return 0;
}

Producing Snow

Alice likes snow a lot! Unfortunately, this year's winter is already over, and she can't expect to have any more of it. Bob has thus bought her a gift — a large snow maker. He plans to make some amount of snow every day. On day i he will make a pile of snow of volume Vi and put it in her garden.

Each day, every pile will shrink a little due to melting. More precisely, when the temperature on a given day is Ti, each pile will reduce its volume by Ti. If this would reduce the volume of a pile to or below zero, it disappears forever. All snow piles are independent of each other.

Note that the pile made on day i already loses part of its volume on the same day. In an extreme case, this may mean that there are no piles left at the end of a particular day.

You are given the initial pile sizes and the temperature on each day. Determine the total volume of snow melted on each day. 

Input

The first line contains a single integer N (1 ≤ N ≤ 105) — the number of days.

The second line contains N integers V1, V2, ..., VN (0 ≤ Vi ≤ 109), where Vi is the initial size of a snow pile made on the day i.

The third line contains N integers T1, T2, ..., TN (0 ≤ Ti ≤ 109), where Ti is the temperature on the day i.

Output

Output a single line with N integers, where the i-th integer represents the total volume of snow melted on day i.

Examples
Input

3
10 10 5
5 7 2

Output

5 12 4

Input

5
30 25 20 15 10
9 10 12 4 13

Output

9 20 35 11 25

Note

In the first sample, Bob first makes a snow pile of volume 10, which melts to the size of 5 on the same day. On the second day, he makes another pile of size 10. Since it is a bit warmer than the day before, the first pile disappears completely while the second pile shrinks to 3. At the end of the second day, he has only a single pile of size 3. On the third day he makes a smaller pile than usual, but as the temperature dropped too, both piles survive till the end of the day.

题解:注意下雪融化的时间

#include <iostream>
#include<algorithm>
#include<string.h>
#include<queue>
#include<stdio.h>
using namespace std;
typedef long long ll;
const int N=1e5+10;
ll t[N],sum[N],v[N];
int main(void)
{
    priority_queue<ll,vector<ll>,greater<ll> >q;
    int n;
    cin >> n;
    for(int i=1;i<=n;i++)
    {
        cin >> v[i];
    }
    for(int i=1;i<=n;i++)
    {
        cin >> t[i];
        sum[i]=t[i]+sum[i-1];
    }
    for(int i=1;i<=n;i++)
    {
        q.push(v[i]+sum[i-1]);
        ll ans=t[i]*q.size();
        while(!q.empty()&&q.top()<=sum[i])
        {
            ans+=q.top()-sum[i];
            q.pop();
        }
        cout << ans << (i == n ? "\n":" ");
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值