DIV1 250pt
题意:对于1-9数字三角形如下图,设其为a[i][j],则a[i][j] = (a[i-1][j] + a[i-1][j+1]) % 10。现在对于某个数字三角形, 每行告诉你某一个位置的数字,求整个数字三角形。
n <= 50。
解法:首先,由于最后一行和倒数第二行共3个数,已知2个,所以就能求出剩下一个,然后一个往上,一行一行将每个数求出来。时间复杂度小于O(10*n^2)。
tag:think
// BEGIN CUT HERE
/*
* Author: plum rain
* score :
*/
/* */
// END CUT HERE
#line 11 "RevealTriangle.cpp"
#include <sstream>
#include <stdexcept>
#include <functional>
#include <iomanip>
#include <numeric>
#include <fstream>
#include <cctype>
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <set>
#include <queue>
#include <bitset>
#include <list>
#include <string>
#include <utility>
#include <map>
#include <ctime>
#include <stack> using namespace std; #define CLR(x) memset(x, 0, sizeof(x))
#define CLR1(x) memset(x, -1, sizeof(x))
#define PB push_back
#define SZ(v) ((int)(v).size())
#define ALL(t) t.begin(),t.end()
#define zero(x) (((x)>0?(x):-(x))<eps)
#define out(x) cout<<#x<<":"<<(x)<<endl
#define tst(a) cout<<#a<<endl
#define CINBEQUICKER std::ios::sync_with_stdio(false) typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<double> VD;
typedef pair<int, int> pii;
typedef long long int64; const double eps = 1e-;
const double PI = atan(1.0)*;
const int maxint = ; class RevealTriangle
{
public:
vector <string> calcTriangle(vector <string> q){
int sz = q.size();
string s;
for (int i = sz-; i >= ; -- i){
s = q[i];
int pos = ;
for (int j = ; j < s.size(); ++ j)
if (s[j] >= '' && s[j] <= ''){
pos = j; break;
} for (int k = pos-; k >= ; -- k)
for (int t = ; t < ; ++ t)
if ((t+s[k+]-'') % == q[i+][k] - '')
s[k] = t + '';
for (int k = pos+; k < s.size(); ++ k)
for (int t = ; t < ; ++ t)
if ((t+s[k-]-'') % == q[i+][k-] - '')
s[k] = t + ''; q[i] = s;
}
return q;
} // BEGIN CUT HERE
public:
void run_test(int Case) { if ((Case == -) || (Case == )) test_case_0(); if ((Case == -) || (Case == )) test_case_1(); if ((Case == -) || (Case == )) test_case_2(); if ((Case == -) || (Case == )) test_case_3(); }
private:
template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }
void verify_case(int Case, const vector <string> &Expected, const vector <string> &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: " << print_array(Expected) << endl; cerr << "\tReceived: " << print_array(Received) << endl; } }
void test_case_0() { string Arr0[] = {"4??",
"?2",
""}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); string Arr1[] = {"", "", "" }; vector <string> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[]))); verify_case(, Arg1, calcTriangle(Arg0)); }
void test_case_1() { string Arr0[] = {""}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); string Arr1[] = {"" }; vector <string> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[]))); verify_case(, Arg1, calcTriangle(Arg0)); }
void test_case_2() { string Arr0[] = {"???2", "??2", "?2", ""}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); string Arr1[] = {"", "", "", "" }; vector <string> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[]))); verify_case(, Arg1, calcTriangle(Arg0)); }
void test_case_3() { string Arr0[] = {"??5?", "??9", "?4", ""}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); string Arr1[] = {"", "", "", "" }; vector <string> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[]))); verify_case(, Arg1, calcTriangle(Arg0)); } // END CUT HERE }; // BEGIN CUT HERE
int main()
{
// freopen( "a.out" , "w" , stdout );
RevealTriangle ___test;
___test.run_test(-);
return ;
}
// END CUT HERE
DIV1 500pt
题意:通过O(n)的方法生成一个长度为n的数列a[]。定义s(i,k) = a[i] + a[i+1] + ... + a[i+k-1]。求最小的|s(i,k) - s(j,k)|,其中i+k-1 < j,并返回k和|s(i,k) - s(j,k)|。如果有多组解,返回k值较大的。
解法:枚举k,求出k固定时最小的满足题意的i, j, k。下面考虑k已经固定的情况。
法一:维护一个set,然后用二分查找绝对值差最小的一组。这样的方法时间复杂度O(n*n*logn),但是常数很大,不一定能过。
法二:考虑一个结论,对于任意一组i, j, k,如果i+k-1 >= j,则一定存在另一组i', j', k'满足i' + k' - 1 < j',且|s(i, k) - s(j, k)| = |s(i', k') - s(j', k')|。
所以得到的方法是,首先记录答案返回值为k_idx 和 val,对于所有的s(i, j)做一排序,第一影响因素是s(i, j)从小到大,第二影响因素是i从小到大,将排序结果存在num[]中。对于num[]中的相邻元素t1和t2(t1在t2之前),有两种可能:
1、如果t1和t2的s(i, k)之差不为0,则if(|t1.s - t2.s| <= val && |t1.i - t2.i| >= k) 更新答案val和k_idx,否则不更新。
2、如果t1和t2的s(i, k)相等,则考虑找出所有s(i, k)与他们相同的项,然后取i之差最大的两项来比较。当然,这个要提前预处理好,不然会很慢。具体见代码。
tag:think, good
// BEGIN CUT HERE
/* */
// END CUT HERE
#line 7 "KSubstring.cpp"
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <iostream>
#include <sstream>
#include <set>
#include <queue>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <list>
#include <stdexcept>
#include <functional>
#include <string>
#include <utility>
#include <map>
#include <ctime>
#include <stack> using namespace std; #define CLR(x) memset(x, 0, sizeof(x))
#define PB push_back
#define SZ(v) ((int)(v).size())
#define ALL(x) (x).begin(), (x).end()
#define out(x) cout<<#x<<":"<<(x)<<endl
#define tst(a) cout<<#a<<endl
#define CINBEQUICKER std::ios::sync_with_stdio(false) typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<double> VD;
typedef long long int64; const double eps = 1e-;
const double PI = atan(1.0)*;
const int maxint = ; inline int MyMod( int a , int b ) { return (a%b+b)%b;} struct nod{
int64 num, s;
}; int64 a[];
nod num[];
int pos[]; bool cmp(nod a, nod b)
{
return b.num < a.num || (a.num == b.num && a.s < b.s);
} class KSubstring
{
public:
vector <int> maxSubstring(int a0, int x, int y, int mod, int n){
a[] = a0;
for (int i = ; i < n; ++ i) a[i] = (a[i-]*x + y) % mod; int64 val = 1LL<<;
int k_idx = ;
for (int k = ; k <= n/; ++ k){
int64 sum = , idx = ;
for (int i = ; i < k; ++ i) sum += a[i];
for (int i = ; i+k <= n; ++ i){
num[idx].num = sum;
num[idx++].s = i;
sum = sum - a[i] + a[i+k];
} sort(num, num+idx, cmp);
int p = idx-;
int64 tnum = num[idx-].num;
for (int i = idx-; i >= ; -- i){
if (num[i].num != tnum){
pos[i] = i+; p = i; tnum = num[i].num;
}
else pos[i] = p;
}
for (int i = ; i < idx; ++ i){
int64 tmp = abs(num[i].num - num[pos[i]].num);
int64 pdif = abs(num[pos[i]].s - num[i].s);
if (pdif >= k && val >= tmp){
k_idx = k; val = tmp;
}
}
}
VI ans; ans.PB (k_idx); ans.PB ((int)val);
return ans;
} // BEGIN CUT HERE
public:
//void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_2();}
void run_test(int Case) { if ((Case == -) || (Case == )) test_case_0(); if ((Case == -) || (Case == )) test_case_1(); if ((Case == -) || (Case == )) test_case_2(); if ((Case == -) || (Case == )) test_case_3(); }
private:
template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }
void verify_case(int Case, const vector <int> &Expected, const vector <int> &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: " << print_array(Expected) << endl; cerr << "\tReceived: " << print_array(Received) << endl; } }
void test_case_0() { int Arg0 = ; int Arg1 = ; int Arg2 = ; int Arg3 = ; int Arg4 = ; int Arr5[] = {, }; vector <int> Arg5(Arr5, Arr5 + (sizeof(Arr5) / sizeof(Arr5[]))); verify_case(, Arg5, maxSubstring(Arg0, Arg1, Arg2, Arg3, Arg4)); }
void test_case_1() { int Arg0 = ; int Arg1 = ; int Arg2 = ; int Arg3 = ; int Arg4 = ; int Arr5[] = {, }; vector <int> Arg5(Arr5, Arr5 + (sizeof(Arr5) / sizeof(Arr5[]))); verify_case(, Arg5, maxSubstring(Arg0, Arg1, Arg2, Arg3, Arg4)); }
void test_case_2() { int Arg0 = ; int Arg1 = ; int Arg2 = ; int Arg3 = ; int Arg4 = ; int Arr5[] = {, }; vector <int> Arg5(Arr5, Arr5 + (sizeof(Arr5) / sizeof(Arr5[]))); verify_case(, Arg5, maxSubstring(Arg0, Arg1, Arg2, Arg3, Arg4)); }
void test_case_3() { int Arg0 = ; int Arg1 = ; int Arg2 = ; int Arg3 = ; int Arg4 = ; int Arr5[] = {, }; vector <int> Arg5(Arr5, Arr5 + (sizeof(Arr5) / sizeof(Arr5[]))); verify_case(, Arg5, maxSubstring(Arg0, Arg1, Arg2, Arg3, Arg4)); } // END CUT HERE };
//by plum rain
// BEGIN CUT HERE
int main()
{
//freopen( "a.out" , "w" , stdout );
KSubstring ___test;
___test.run_test(-);
return ;
}
// END CUT HERE