SRM 399(1-250pt)
DIV1 250pt
题意:给出一个size不超过50的数组a和整数n,求x,y,z使得|n - x*y*z|最小,且x,y,z均不再数组a中。若有多组xyz使得|n-x*y*z|最小,输出字典序最小的。a中的元素和n <= 1000。
解法:我直接写了一个暴力扫的方法,x*y*z上限设成了10000....发现如果a中含有1,2,3....50,就会WA,因为这样的话x*y*z的需要达到51*51*51。后来又写了一个方法也错了.....没做出来看了题解,发现它是div2的1000突然就感觉到安慰了.......
要观察到一个东西,就是x,y,z都不可能超过n + 51。然后暴力就可以了,加一个break语句,当x*y*z > 200000时。
想了一下,也就是说,其实可以不用观察到那个结论,只需要对xyz写3个for语句从1到1010,然后在x*y*z > 200000时break,这道题就能过了....
tag:think
// BEGIN CUT HERE
/*
* Author: plum rain
* score :
*/
/* */
// END CUT HERE
#line 11 "AvoidingProduct.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 = ;
const int N = ; bool v[]; class AvoidingProduct
{
public:
vector <int> getTriple(vector <int> a, int n){
int sz = a.size();
CLR (v);
for (int i = ; i < sz; ++ i) v[a[i]] = ; int num = maxint;
VI cur, tmp;
for (int i = ; i <= ; ++ i) if (!v[i]){
if (i*i*i > N) break;
for (int j = i; j <= ; ++ j) if (!v[j]){
if (i*j*j > N) break;
for (int k = j; k <= ; ++ k) if (!v[k]){
int mul = i * j * k;
int t_num = abs(n - mul);
if (num == t_num){
tmp.clear();
tmp.PB (i); tmp.PB (j); tmp.PB (k);
}
if (t_num < num || (t_num == num && tmp < cur)){
num = t_num;
cur.clear();
cur.PB (i); cur.PB (j); cur.PB (k);
}
if (mul > N) break;
}
}
}
return cur;
} // 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(); if ((Case == -) || (Case == )) test_case_4(); if ((Case == -) || (Case == )) test_case_5(); }
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 Arr0[] = {, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , }; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arg1 = ; int Arr2[] = {, , }; vector <int> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[]))); verify_case(, Arg2, getTriple(Arg0, Arg1)); }
void test_case_1() { int Arr0[] = {}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arg1 = ; int Arr2[] = {, , }; vector <int> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[]))); verify_case(, Arg2, getTriple(Arg0, Arg1)); }
void test_case_2() { int Arr0[] = {,}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arg1 = ; int Arr2[] = {, , }; vector <int> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[]))); verify_case(, Arg2, getTriple(Arg0, Arg1)); }
void test_case_3() { int Arr0[] = {,}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arg1 = ; int Arr2[] = {, , }; vector <int> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[]))); verify_case(, Arg2, getTriple(Arg0, Arg1)); }
void test_case_4() { int Arr0[] = {,}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arg1 = ; int Arr2[] = {, , }; vector <int> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[]))); verify_case(, Arg2, getTriple(Arg0, Arg1)); }
void test_case_5() { int Arr0[] = {,}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arg1 = ; int Arr2[] = {, , }; vector <int> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[]))); verify_case(, Arg2, getTriple(Arg0, Arg1)); } // END CUT HERE }; // BEGIN CUT HERE
int main()
{
// freopen( "a.out" , "w" , stdout );
AvoidingProduct ___test;
___test.run_test(-);
return ;
}
// END CUT HERE
SRM 399(1-250pt)的更多相关文章
- SRM593(1-250pt,500pt)
SRM 593 DIV1 250pt 题意:有如下图所示的平面,每个六边形有坐标.将其中一些六边形染色,要求有边相邻的两个六边形不能染同一种颜色.给定哪些六边形需要染色,问最少需要多少种颜色. 解法: ...
- 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和 ...
- SRM 609(1-250pt, 1-500pt)
嗯....还是应该坚持写题解的好习惯啊... DIV1 250pt 这难度是回到srm 300+的250了嘛...略 // BEGIN CUT HERE /* * Author: plum rain ...
- SRM 442(1-250pt, 1-500pt)
DIV1 250pt 题意:将一个数表示成质因子相乘的形式,若乘式所含数字的个数为质数,则称A为underprime.比如12 = 2*2*3,则含3个数字,是underprime.求A, B之间un ...
- 记第一次TopCoder, 练习SRM 583 div2 250
今天第一次做topcoder,没有比赛,所以找的最新一期的SRM练习,做了第一道题. 题目大意是说 给一个数字字符串,任意交换两位,使数字变为最小,不能有前导0. 看到题目以后,先想到的找规律,发现要 ...
- SRM 513 2 1000CutTheNumbers(状态压缩)
SRM 513 2 1000CutTheNumbers Problem Statement Manao has a board filled with digits represented as St ...
- SRM 510 2 250TheAlmostLuckyNumbersDivTwo(数位dp)
SRM 510 2 250TheAlmostLuckyNumbersDivTwo Problem Statement John and Brus believe that the digits 4 a ...
随机推荐
- 美好头标ToolBar
ActionBar我相信是每一位合格的程序员都用过的组件,也是每一个程序员都会抱怨的组件,因为他不能实现复杂的自定义.为此Google推出了比ActionBar更为美好的组件ToolBar. 本文重点 ...
- xml中报错,验证是否是xml报错
1.xml中写入sql有时报错,例如有大于号小于号,要用<![CDATA[ ]]>扩起来 2.验证xml有错的方式,以浏览器方式打开,如果正常打开,无错. ...
- java.lang.ClassCastException: java.math.BigDecimal cannot be cast to java.lang.String
http://blog.csdn.net/agileclipse/article/details/17161225 详情请点击链接查看
- (whh仅供自己参考)进行ip网络请求的步骤
这个过程大致是这个样子: 1 添加通知 2 发送网络请求 里边有一个发送通知的操作 3 执行发送通知的具体操作 代码如下: 1 在VC添加通知 [[NSNotificationCenter defau ...
- C# 多线程、结构体
struct IpAndPort { public string Ip; public int Port; } private void Form1_Load(object sender, Event ...
- java_设计模式_模板方法模式_Template Method Pattern(2016-08-11)
定义: 定义一个操作中算法的骨架,而将一些步骤延迟到子类中,使得子类可以不改变算法的结构即可重定义该算法中的某些特定步骤.这里的算法的结构,可以理解为你根据需求设计出来的业务流程.特定的步骤就是指那些 ...
- WHU 1568 Product (DP、逆元)
题意: 定义f(x) 为数x的所有数字的乘积. 求满足f(k)=f(x)的不同的不含数字1的k的个数. x的长度小于50. 不超过1000组数据. Solution: 由于函数是乘积的形式,可以由质因 ...
- Struts2中的链接标签 <s:url>和<s:a>---在action中获取jsp表单提交的参数(转)
转自:http://sgl124764903.iteye.com/blog/444183 1.普通链接 Web程序中最普通的应用是链接到其他页面,下面看Welcome.jsp. <%@ page ...
- 生成四位随机数的PHP代码
纯数字的四位随机数 rand(1000,9999) 数字和字符混搭的四位随机字符串: function GetRandStr($len) { $chars = array( "a" ...
- windows7任务栏上的图标修复
Technorati 标记: 疑难杂症 今天,我在使用Windows 7的时候,因为操作一些系统文件,发现桌面下角的个别正在运行的图标不见了,但是,我们如果再打开一个新程序,又会提醒你已经在运行了 ...