SRM 480 DIV 2

1 水题。。。非常松的限制条件,如果都去掉的话,或许会成为challenge的一道好题

2 非常恶心的问题,一个trick把我给绕进去了。。不能线序的遍历一遍,需要不断设置,不断扩充未完成的集合,并附设标记位

3 又是一道恶心的题目。。。这种1000分的题目,一定不要放弃,不断思考,一定会有结果的。。

This problem can be solved greedily with the following powerful observation.

Let X1, X2, X3, ..., XN be the numbers. We must allocate them in the encrypted string in some order. Suppose that Xi is the last number allocated number in this order. Then, it is optimal to allocate the rest of the numbers starting from the smallest one and in increasing order. That is, if Xi is the last number allocated, then the smallest possible length of the encrypted string is partialsolution(sortascending(X1,X2,...,X(i-1),X(i+1),...,XN) + Xi).

We'll see that this is true by contradiction. Suppose there exist j and k (both are not equal to i), j < k such that Xj > Xk and the representative of Xj is to the left of the representative of Xk. The proof will be complete if we show that we can swap the locations of the representatives of these two numbers, since we can then apply this until all numbers except Xi are sorted. We suppose that Xj is allocated at position 2^Pj and Xk is allocated at position 2^Pk. Since Xk < Xj, it follows that Xk can be placed in the former position of Xj. Now, since the length of Xj must be less than 2^Pk (since otherwise Xk cannot be originally allocated at 2^Pk), we can place Xj in the former position of Xk without affecting the position 2^(Pk+1). Hence, the proof is complete.

解答,代码写的真的是非常的漂亮!!

// END CUT HERE
#line 5 "SignalIntelligence.cpp"

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

using namespace std;

// BEGIN CUT HERE

//Begin Sosi TopCoder

//const double EPS=1e-11;

//const double PI=acos(-1.0);

//const short INF=32767,NF= -32768;

//const int INF=2147483647,NF= -2147483648;

//const long long INF=9223372036854775807,NF=-9223372036854775808;

//const long double INF=99999999.99999999;

//Numberic Functions

//template T gcd(T a,T b){ if(a<0) return gcd(-a,b);if(b<0)return gcd(a,-b);return (b==0)?a:gcd(b,a%b);}

//template T lcm(T a,T b){ return a*(b/gcd(a,b));}

//template inline vector > factorize(T n)

//   {vector > R;for (T i=2;n>1;){if (n%i==0){int C=0;for (;n%i==0;C++,n/=i);R.push_back(make_pair(i,C));}

//   i++;if (i>n/i) i=n;}if (n>1) R.push_back(make_pair(n,1));return R;}

//template inline bool isPrimeNumber(T n)

//   {if(n<=1)return false;for (T i=2;i*i<=n;i++) if (n%i==0) return false;return true;}

//template inline T eularFunction(T n)

//   {vector > R=factorize(n);T r=n;for (int i=0;i

//Translator

//template string toString(T n){ostringstream ost;ost<

//int toInt(string s){int r=0;istringstream sin(s);sin>>r;return r;}//NOTES:toInt(

//long long toInt64(string s){long long r=0;istringstream sin(s);sin>>r;return r;}

//double toDouble(string s){double r=0;istringstream sin(s);sin>>r;return r;}//NOTES:toDouble(

//template void stoa(string s,int &n,T A[]){n=0;istringstream sin(s);for(T v;sin>>v;A[n++]=v);}

//template void atos(int n,T A[],string &s){ostringstream sout;for(int i=0;i 0)sout<<' ';sout<

//template void atov(int n,T A[],vector &vi){vi.clear();for (int i=0;i

//template void vtoa(vector vi,int &n,T A[]){n=vi.size();for (int i=0;i

//template void stov(string s,vector &vi){vi.clear();istringstream sin(s);for(T v;sin>>v;vi.push_bakc(v));}

//template void vtos(vector vi,string &s){ostringstream sout;for (int i=0;i 0)sout<<' ';sout<

//Fraction

//template struct Fraction{T a,b;Fraction(T a=0,T b=1);string toString();};

//template Fraction ::Fraction(T a,T b){T d=gcd(a,b);a/=d;b/=d;if (b<0) a=-a,b=-b;this->a=a;this->b=b;}

//template string Fraction ::toString(){ostringstream sout;sout< <<"/"<

//template Fraction operator+(Fraction p,Fraction q){return Fraction (p.a*q.b+q.a*p.b,p.b*q.b);}

//template Fraction operator-(Fraction p,Fraction q){return Fraction (p.a*q.b-q.a*p.b,p.b*q.b);}

//template Fraction operator*(Fraction p,Fraction q){return Fraction (p.a*q.a,p.b*q.b);}

//template Fraction operator/(Fraction p,Fraction q){return Fraction (p.a*q.b,p.b*q.a);}

//STL

//bool comp(T A,T B){return A

//do{ } while(next_permutation(T.begin(), T.end()));

//int dirx[]={1,0,-1,0};

//int diry[]={0,-1,0,1}; //clockwise

//int dirx[]={1, 1, 0,-1,-1,-1,0,1};

//int diry[]={0,-1,-1,-1, 0, 1,1,1}; //clockwise

//End Sosi TopCoder

// END CUT HERE

class SignalIntelligence
{
public:
    long long encrypt(vector numbers)
    {
        int n = numbers.size();
        long long best = -1;
        for (int i=0;i
        { // every possible last number
            //printf("try %d as last one/n", numbers[i]);
            vector numbers2;
            for (int j=0;j =i)?1:0)]);
            sort(numbers2.begin(),numbers2.end());
            //printf("numbers left:");
            //for (int j=0;j             //printf("/n");
            long long pos = 1, npos = pos;
            for (int j=0;j
            {
                //printf("set %d at pos %d/n", numbers2[j], pos);
                npos = pos;
                while (pos + numbers2[j] >= npos) npos *= 2;
                pos = npos;
            }
            //printf("would be %d long/n/n",pos + numbers[i]-1);
            if (best == -1 || npos + numbers[i]-1 < best) best = npos + numbers[i]-1;
        }
        return best;
    }

    //$CARETPOSITION$

    // BEGIN CUT HERE
public:
    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(); }
private:
    template string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector ::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '/"' << *iter << "/","; os << " }"; return os.str(); }
    void verify_case(int Case, const long long &Expected, const long long &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "/tExpected: /"" << Expected << '/"' << endl; cerr << "/tReceived: /"" << Received << '/"' << endl; } }
    void test_case_0() { int Arr0[] = {1,2,3}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); long long Arg1 = 8LL; verify_case(0, Arg1, encrypt(Arg0)); }
    void test_case_1() { int Arr0[] = {4,4,2,2}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); long long Arg1 = 19LL; verify_case(1, Arg1, encrypt(Arg0)); }
    void test_case_2() { int Arr0[] = {1000000000,1000000000,1000000000,1000000000,1000000000,
        1000000000,1000000000,1000000000,1000000000,1000000000,
        1000000000,1000000000,1000000000,1000000000,1000000000,
        1000000000,1000000000,1000000000,1000000000,1000000000}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); long long Arg1 = 281475976710655LL; verify_case(2, Arg1, encrypt(Arg0)); }

    // END CUT HERE

};

// BEGIN CUT HERE
int main()
{
    SignalIntelligence ___test;
    ___test.run_test(-1);
    return 0;
}

做题的时候,summer在旁边晃来晃去。。。心烦。。。不过看到最后的代码的实现,的确是非常优美啊!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值