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

  1 // BEGIN CUT HERE
  2 /*
  3  * Author:  plum rain
  4  * score :
  5  */
  6 /*
  7 
  8  */
  9 // END CUT HERE
 10 #line 11 "AvoidingProduct.cpp"
 11 #include <sstream>
 12 #include <stdexcept>
 13 #include <functional>
 14 #include <iomanip>
 15 #include <numeric>
 16 #include <fstream>
 17 #include <cctype>
 18 #include <iostream>
 19 #include <cstdio>
 20 #include <vector>
 21 #include <cstring>
 22 #include <cmath>
 23 #include <algorithm>
 24 #include <cstdlib>
 25 #include <set>
 26 #include <queue>
 27 #include <bitset>
 28 #include <list>
 29 #include <string>
 30 #include <utility>
 31 #include <map>
 32 #include <ctime>
 33 #include <stack>
 34 
 35 using namespace std;
 36 
 37 #define CLR(x) memset(x, 0, sizeof(x))
 38 #define CLR1(x) memset(x, -1, sizeof(x))
 39 #define PB push_back
 40 #define SZ(v) ((int)(v).size())
 41 #define ALL(t) t.begin(),t.end()
 42 #define zero(x) (((x)>0?(x):-(x))<eps)
 43 #define out(x) cout<<#x<<":"<<(x)<<endl
 44 #define tst(a) cout<<#a<<endl
 45 #define CINBEQUICKER std::ios::sync_with_stdio(false)
 46 
 47 typedef vector<int> VI;
 48 typedef vector<string> VS;
 49 typedef vector<double> VD;
 50 typedef pair<int, int> pii;
 51 typedef long long int64;
 52 
 53 const double eps = 1e-8;
 54 const double PI = atan(1.0)*4;
 55 const int maxint = 2139062143;
 56 const int N = 200000;
 57 
 58 bool v[1100];
 59 
 60 class AvoidingProduct
 61 {
 62     public:
 63         vector <int> getTriple(vector <int> a, int n){
 64             int sz = a.size();
 65             CLR (v);
 66             for (int i = 0; i < sz; ++ i) v[a[i]] = 1;
 67             
 68             int num = maxint;
 69             VI cur, tmp;
 70             for (int i = 1; i <= 1010; ++ i) if (!v[i]){
 71                 if (i*i*i > N) break;
 72                 for (int j = i; j <= 1010; ++ j) if (!v[j]){
 73                     if (i*j*j > N) break;
 74                     for (int k = j; k <= 1010; ++ k) if (!v[k]){
 75                         int mul = i * j * k;
 76                         int t_num = abs(n - mul);
 77                         if (num == t_num){
 78                             tmp.clear();
 79                             tmp.PB (i); tmp.PB (j); tmp.PB (k);
 80                         }
 81                         if (t_num < num || (t_num == num && tmp < cur)){
 82                             num = t_num;
 83                             cur.clear();
 84                             cur.PB (i); cur.PB (j); cur.PB (k);
 85                         }
 86                         if (mul > N) break;
 87                     }
 88                 }
 89             }
 90             return cur;
 91         }
 92         
 93 // BEGIN CUT HERE
 94     public:
 95     void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); if ((Case == -1) || (Case == 4)) test_case_4(); if ((Case == -1) || (Case == 5)) test_case_5(); }
 96     private:
 97     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(); }
 98     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; } }
 99     void test_case_0() { int Arr0[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 1000; int Arr2[] = {51, 51, 51 }; vector <int> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); verify_case(0, Arg2, getTriple(Arg0, Arg1)); }
100     void test_case_1() { int Arr0[] = {1}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 10; int Arr2[] = {2, 2, 2 }; vector <int> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); verify_case(1, Arg2, getTriple(Arg0, Arg1)); }
101     void test_case_2() { int Arr0[] = {1,2}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 10; int Arr2[] = {3, 3, 3 }; vector <int> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); verify_case(2, Arg2, getTriple(Arg0, Arg1)); }
102     void test_case_3() { int Arr0[] = {1,3}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 12; int Arr2[] = {2, 2, 2 }; vector <int> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); verify_case(3, Arg2, getTriple(Arg0, Arg1)); }
103     void test_case_4() { int Arr0[] = {1,3}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 13; int Arr2[] = {2, 2, 4 }; vector <int> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); verify_case(4, Arg2, getTriple(Arg0, Arg1)); }
104     void test_case_5() { int Arr0[] = {1,15}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 90; int Arr2[] = {2, 5, 9 }; vector <int> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); verify_case(5, Arg2, getTriple(Arg0, Arg1)); }
105 
106 // END CUT HERE
107 
108 };
109 
110 // BEGIN CUT HERE
111 int main()
112 {
113 //    freopen( "a.out" , "w" , stdout );    
114     AvoidingProduct ___test;
115     ___test.run_test(-1);
116        return 0;
117 }
118 // END CUT HERE
View Code

 

转载于:https://www.cnblogs.com/plumrain/p/srm_399.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值