题目以下:ios
Counting Subsequencesgit
Time Limit: 5000 MSMemory Limit: 65536 K算法
Description |
"47 is the quintessential random number," states the 47 society. And there might be a grain of truth in that.dom For example, the first ten digits of the Euler's constant are:ide 2 7 1 8 2 8 1 8 2 8优化 And what's their sum? Of course, it is 47.ui You are given a sequence S of integers we saw somewhere in the nature. Your task will be to compute how strongly does this sequence support the above claims.this We will call a continuous subsequence of S interesting if the sum of its terms is equal to 47.spa E.g., consider the sequence S = (24, 17, 23, 24, 5, 47). Here we have two interesting continuous subsequences: the sequence (23, 24) and the sequence (47).设计 Given a sequence S, find the count of its interesting subsequences. |
Input |
The first line of the input file contains an integer T(T <= 10) specifying the number of test cases. Each test case is preceded by a blank line. The first line of each test case contains the length of a sequence N(N <= 500000). The second line contains N space-separated integers – the elements of the sequence. Sum of any continuous subsequences will fit in 32 bit signed integers. |
Output |
For each test case output a single line containing a single integer – the count of interesting subsequences of the given sentence. |
Sample Input |
2
13
7 |
Sample Output |
3 4 |
这道题的意思就是给你一个整形的序列,让你计算知足和是47的子序列的个数。序列中的值可能有负数。
因此最直观的方法就是计算全部子序列的和,而后判断是否与47相等。算法的时间复杂度为O(N³),代码以下:
#include<iostream> #include<vector> #include<map> #define N 47 using namespace std; template<class type> int countSubseq(const vector<type> &data) { int count = 0; for(size_t i = 0; i < data.size(); ++i) { int sum = 0; for(int j = 0; j <= i; ++j) { int sum = 0; for(int k = j; k <= i; ++k) { sum += data[k]; } if(sum == N) { ++count; } } } return count; } int main() { int nCase; cin >> nCase; for(int i = 0; i < nCase; ++i) { int n, d; cin >> n; vector<int> data(n, 0); for(int j = 0; j < n; ++j) { cin >> d; data[j] = d; } cout << countSubseq(data) << endl; } return 0; }
经过分析能够看出程序存在许多重复计算,上一个sum能够进行一次加法运算获得下一个sum。通过再次优化使其时间复杂度变为O(N²),代码以下:
template<class type> int countSubseq(const vector<type> &data) { int count = 0; for(size_t i = 0; i < data.size(); ++i) { int sum = 0; for(int j = i; j < data.size(); ++j) { sum += data[j]; if(sum == N) { ++count; } } } return count; }
可是程序依然超时,因此要设计出复杂度更低的算法才行。
设SUM[i,j] = A[i] + A[i+1] + ... + A[j] (0 <= i <= j < N),因此SUM[i, j] = SUM[0, j] - SUM[,0, i-1],也就是说只要求出全部的SUM[0, K] (K∈[0,N))就能计算出任意SUM[i, j].那么咱们每次计算K的一种取值获得的SUM时,查找以前计算的SUM是否有与当前SUM差是47的K的个数,能够使用map来下降查找的复杂度,这样时间复杂度降为O(N),代码以下:
template<class type> int countSubseq(const vector<type> &data) { int count = 0, sum = 0; map<int, int> sumToSeqCnt;//存储和是sum的序列K的个数 sumToSeqCnt[0] = 1; for(size_t i = 0; i < data.size(); ++i) { sum += data[i]; sumToSeqCnt[sum]++;//计算和是sum的序列K的个数 count += sumToSeqCnt[sum - N]; } return count; }