SRM 404(1-250pt, 1-500pt)
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
SRM 404(1-250pt, 1-500pt)的更多相关文章
- SRM475 - SRM479(1-250pt,500pt)
SRM 475 DIV1 300pt 题意:玩游戏.给一个棋盘,它有1×n(1行n列,每列标号分别为0,1,2..n-1)的格子,每个格子里面可以放一个棋子,并且给定一个只含三个字母WBR,长度为n的 ...
- SRM468 - SRM469(1-250pt, 500pt)
SRM 468 DIV1 250pt 题意:给出字典,按照一定要求进行查找. 解法:模拟题,暴力即可. tag:water score: 0.... 这是第一次AC的代码: /* * Author: ...
- SRM470 - SRM474(1-250pt,500pt)(471-500pt为最短路,474-500pt未做)
SRM 470 DIV1 250pt 题意:有n个房间排成一排,相邻两个房间之间有一扇关闭着的门(共n-1扇),每个门上都标有‘A’-‘P’的大写字母.给定一个数n,表示第n个房间.有两个人John和 ...
- SRM593(1-250pt,500pt)
SRM 593 DIV1 250pt 题意:有如下图所示的平面,每个六边形有坐标.将其中一些六边形染色,要求有边相邻的两个六边形不能染同一种颜色.给定哪些六边形需要染色,问最少需要多少种颜色. 解法: ...
- topcoder srm 553
div1 250pt: 题意:... 解法:先假设空出来的位置是0,然后模拟一次看看是不是满足,如果不行的话,我们只需要关心最后栈顶的元素取值是不是受空白处的影响,于是还是模拟一下. // BEGIN ...
- topcoder srm 552
div1 250pt: 题意:用RGB三种颜色的球摆N层的三角形,要求相邻的不同色,给出RGB的数量,问最多能摆几个 解法:三种颜色的数量要么是全一样,要么是两个一样,另外一个比他们多一个,于是可以分 ...
- topcoder srm 551
div1 250pt 题意:一个长度最多50的字符串,每次操作可以交换相邻的两个字符,问,经过最多MaxSwaps次交换之后,最多能让多少个相同的字符连起来 解法:对于每种字符,枚举一个“集结点”,让 ...
- topcoder srm 550
div1 250pt: 题意:有个机器人,从某一点出发,他只有碰到地形边缘或者碰到走过的点时才会改变运动方向,然后接着走,现在给出他的运动轨迹,判断他的运动是否合法,如果合法的话,那么整个地形的最小面 ...
- topcoder srm 610
div1 250pt: 题意:100*100的01矩阵,找出来面积最大的“类似国际象棋棋盘”的子矩阵. 解法:枚举矩阵宽(水平方向)的起点和终点,然后利用尺取法来找到每个固定宽度下的最大矩阵,不断更新 ...
随机推荐
- 删除我的电脑中360随身WiFi云U盘的图标
可通过删除注册表的方法 运行-regedit 找到这个项 HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\My ...
- 关于UIScrollView属性和方法的总结
iOS中UIScollView的总结 在iOS开发中可以说UIScollView是所有滑动类视图的基础,包括UITableView,UIWebView,UICollectionView等等,UIScr ...
- Swift 中 Selector 方法的访问权限控制问题
今天用Swift写了个视图,在视图上加个手势,如下所示: panGestureRecognizer = UIPanGestureRecognizer(target: self, action: &qu ...
- 哈哈,CSDN又支持Windows Live Writer了
从10年开始写CSDN博客,后面不支持WLW了,就不怎么写了,话说自带的编辑器确实不怎么样,不过又支持了,那就哈哈,重新开工了. 关于如何配置的,跟以前一样,详情如下所示: http://blog.c ...
- OpenCV中Mat的详解
每次碰到Mat都得反复查具体的用法,网上的基础讲解不多,难得看到一篇,赶快转来收藏~ 原文地址:http://www.opencvchina.com/thread-1039-1-1.html 目标 我 ...
- Java发邮件:Java Mail与Apache Mail
作者:Vinkn 来自http://www.cnblogs.com/Vinkn/ 一.邮件简介 一封邮件由很多信息构成,主要的信息如下,其他的暂时不考虑,例如抄送等: 1.收件人:收件人的邮箱地址,例 ...
- js prototype __proto__ instanceof constructor
JS中有两个特殊的对象:Object与Function,它们都是构造函数,用于生成对象. Object.prototype是所有对象的祖先,Function.prototype是所有函数的原型,包括构 ...
- 【Linux】常用命令
一.文件结构 /: 根目录 /bin: 系统所需要的那些命令位于此目录. /boot: Linux的内核及引导系统程序所需要的文件目录,GRUB或LILO系统引导管理器也 ...
- 使用appium做自动化时如何切换activity
在使用appium过程中遇到了执行一个用例时有多个不同的acitivity的情况,以下为app内部切换acitivity的方法: 如果仅需要切换一次activity,可以通过设置desired_cap ...
- 编译内核出错:invalid option `abi=aapcs-linux' 解决办法
出现此问题的原因是由于kernel feature中选中了Use the ARM EABIto compile the kernel引起的,有两各解决办法: 1)换编译器为arm-linux-gcc ...