#尺取法# 尺取法例题

题目链接:POJ3061

题目链接:codeforces 660 C

题目链接:UVA11572

POJ3061

Description
A sequence of N positive integers (10 < N < 100 000), each of them less than or equal 10000, and a positive integer S (S < 100 000 000) are given. Write a program to find the minimal length of the subsequence of consecutive elements of the sequence, the sum of which is greater than or equal to S.
Input
The first line is the number of test cases. For each test case the program has to read the numbers N and S, separated by an interval, from the first line. The numbers of the sequence are given in the second line of the test case, separated by intervals. The input will finish with the end of file.
Output
For each the case the program has to print the result on separate line of the output file.if no answer, print 0.
Sample Input
2
10 15
5 1 3 5 10 7 4 9 2 8
5 11
1 2 3 4 5
Sample Output
2
3

题目大意:

给长度为n的数组和一个整数s,求总和不小于s的连续子序列的最小长度

解题思路:

尺取法

尺取法的思路:

反复地推进区间的开头和末尾,来求取满足条件的最小区间的方法被称为尺取法( O(n) )
1. 以 l = r = sum = 0 初始化
2. 只要依然有sum < s,就不断将sum增加ar,并将r增加1
3. 如果第2步无法满足sum >= s则终止,否则更新ans = min(ans, r - l);
4. 将sum减去al,l增加1然后回到第2步

样例1查找过程:

代码:

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <algorithm>
typedef long long LL;
using namespace std;
const int MaxN = 3e5 + 5;

int a[MaxN];

int main() {
    int t, n, s;
    scanf("%d", &t);
    while(t--) {
        scanf("%d %d",&n, &s);
        for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
        int l = 1, r = 1, sum = 0, ans = n + 1;
        while(1) {  //这部分就是尺取法的实现
            while(r <= n && sum < s) sum += a[r++];
            if(sum < s) break;  
            ans = min(ans, r - l);
            sum -= a[l++];
        }
        if(ans == n + 1) ans = 0;
        printf("%d\n", ans);
    }
    return 0;
}

codeforces 660 C

You are given an array a with n elements. Each element of a is either 0 or 1.
Let’s denote the length of the longest subsegment of consecutive elements in a, consisting of only numbers one, as f(a). You can change no more than k zeroes to ones to maximize f(a).
Input
The first line contains two integers n and k (1 ≤ n ≤ 3·105, 0 ≤ k ≤ n) — the number of elements in a and the parameter k.
The second line contains n integers ai (0 ≤ ai ≤ 1) — the elements of a.
Output
On the first line print a non-negative integer z — the maximal value of f(a) after no more than k changes of zeroes to ones.
On the second line print n integers aj — the elements of the array a after the changes.
If there are multiple answers, you can print any one of them.
Examples
Input
7 1
1 0 0 1 1 0 1
Output
4
1 0 0 1 1 1 1
Input
10 2
1 0 0 1 0 1 0 1 0 1
Output
5
1 0 0 1 1 1 1 1 0 1

题目大意:

最多把k个0变成1,求改变后连续的最长全为1的数字串是多长

解题思路:

尺取法

代码:

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <algorithm>
typedef long long LL;
using namespace std;
const int MaxN = 3e5 + 5;

int s[MaxN], a[MaxN];

int main() {
    int n, k;
    cin >> n >> k;
    for(int i = 1; i <= n; i++) {
        cin >> a[i];
        s[i] = s[i - 1] + a[i]; //实际上存的是1的个数
    }
    int l = 1, r = 1;
    int ans = 0, ansl = 0, ansr = 0; //所求的答案区间
    while(l <= n && r <= n) {
        while(r - l + 1 - (s[r] - s[l - 1]) <= k && r <= n) { //0的个数小于等于k时
            if(ans < r - l + 1) { //更新ans
                ans = r - l + 1;
                ansl = l, ansr = r;
            }
            r++;
        }
        l++;
    }
    for(int i = ansl; i <= ansr; i++) a[i] = 1;
    cout << ans << endl;
    for(int i = 1; i <= n; i++) cout << a[i] << (i == n ? "\n" : " ");
    return 0;
}

UVA11527

Emily the entrepreneur has a cool business idea: packaging and selling snowflakes. She has devised a machine that captures snowflakes as they fall, and serializes them into a stream of snowflakes that flow, one by one, into a package. Once the package is full, it is closed and shipped to be sold. The marketing motto for the company is “bags of uniqueness.” To live up to the motto, every snowflake in a package must be different from the others. Unfortunately, this is easier said than done, because in reality, many of the snowflakes flowing through the machine are identical. Emily would like to know the size of the largest possible package of unique snowflakes that can be created. The machine can start filling the package at any time, but once it starts, all snowflakes flowing from the machine must go into the package until the package is completed and sealed. The package can be completed and sealed before all of the snowflakes have flowed out of the machine.
Input
The first line of input contains one integer specifying the number of test cases to follow. Each test case begins with a line containing an integer n, the number of snowflakes processed by the machine. The following n lines each contain an integer (in the range 0 to 109, inclusive) uniquely identifying a snowflake. Two snowflakes are identified by the same integer if and only if they are identical. The input will contain no more than one million total snowflakes.
Output
For each test case output a line containing single integer, the maximum number of unique snowflakes that can be in a package.
Sample Input
1 5 1 2 3 2 1
Sample Output
3

题目大意:

求最长不重复子序列

解题思路:

set 和 尺取法的融合

代码:

#include<cstring>
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<queue>
#include<set>
#include<map>
using namespace std;
typedef long long LL;
const int MaxN = 1000000 + 5;

int a[MaxN];

int main() {
    int t, n;
    cin >> t;
    while(t--) {
        cin >> n;
        for(int i = 1; i <= n; i++) cin >> a[i];
        set<int> s;
        int l = 1, r = 1, ans = 0;
        while(l <= n && r <= n) {
            while(r <= n && !s.count(a[r])) s.insert(a[r++]);
            ans = max(ans, r - l);
            s.erase(a[l++]);
        }
        cout << ans << endl;
        }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/jasmineaha/article/details/79256060