Poj3061 subsequence & poj3320 Jessica's reading problem (ruler acquisition)

Source: Internet
Author: User

Both questions are based on the ruler. The ruler acquisition method is a common technique in the challenge Programming Competition.

It is O (n) scanning the array, and the answer will come out after scanning, this process requires the problem to have this nature: After the head pointer goes forward (s ++, the tail pointer (t) either does not move or moves forward. If this feature is met, the ruler acquisition method can be considered.

Poj3061 is relatively simple. It can also be done in binary mode. time complexity O (N * logn ). You can use the ruler acquisition method O (n) to solve the problem.

# Include <iostream> # include <cstdio> # include <cstdlib> # include <cstring> # include <cmath> # include <map> # include <set> # include <vector> # include <algorithm> # include <stack> # include <queue> # include <cctype> # include <sstream> using namespace STD; # define INF 1000000000 # define EPS 1e-8 # define PII pair <int, int> # define ll long intint T, N, S, A [100010]; int main () {// freopen ("in7.txt", "r", stdin); // freopen ("out.txt", "W", stdout); scanf ("% d ", & T); While (t --) {scanf ("% d", & N, & S); int ans = inf; For (INT I = 0; I <n; I ++) {scanf ("% d", & A [I]);} int S = 0, T = 0, sum = 0; while (1) {While (T <n & sum <s) {sum + = A [T]; t ++;} If (sum <s) break; ans = min (ANS, t-s); // note that the distance here is not (t-s + 1), because the last t ++ in my previous while, so // The range from S to T is left closed right open sum-= A [s]; s ++;} If (ANS <inf) cout <ans <Endl; else cout <'0' <Endl;} // fclose (stdin); // fclose (stdout); Return 0 ;}
View code

Poj3320 is very convenient to use set and map for data processing, and the core part is the ruler acquisition method.

# Include <iostream> # include <cstdio> # include <cstdlib> # include <cstring> # include <cmath> # include <map> # include <set> # include <vector> # include <algorithm> # include <stack> # include <queue> # include <cctype> # include <sstream> using namespace STD; # define INF 1000000000 # define EPS 1e-8 # define PII pair <int, int> # define ll long intint P, A [1000010]; set <int> st; Map <int, int> MP; int main () {// freopen ("in8.txt", "r", stdin); // freopen ("out.txt", "W", stdout ); scanf ("% d", & P); For (INT I = 0; I <p; I ++) {scanf ("% d ", & A [I]); ST. insert (a [I]);} int Tol = ST. size (); int S = 0, T = 0; int ans = inf; For (;) {While (T <P & MP. size () <TOL) {If (MP. count (A [T]) MP [A [t ++] ++;/* use the count function in map, and 1 is returned, 0 */else MP [A [t ++] = 1;} If (MP. size () <TOL) break; ans = min (ANS, t-s); MP [A [s] --; if (MP [A [s] = 0) MP. erase (A [s]); s ++;} cout <ans <Endl; // fclose (stdin); // fclose (stdout); Return 0 ;}
View code

 

Poj3061 subsequence & poj3320 Jessica's reading problem (ruler acquisition)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.