topcoder SRM712 Div1 LR

题目:

Problem Statement

     We have a cyclic array A of length n. For each valid i, element i-1 the left neighbor of element i. Additionally, element n-1 is the left neighbor of element 0.

You are given two vector<long long>s s and t, each with n elements. Currently, we have A[i] = s[i] for each valid i. Our goal is to have A[i] = t[i] for each valid i.

We can use two operations that modify the contents of A:

  • Operation L: Each element is increased by the value of its left neighbor.
  • Operation R: Each element is increased by the value of its right neighbor.

Note that all changes happen simultaneously. For example, if you use the operation L, the new value of A[7] is computed as the sum of the old value of A[7] and the old value of A[6].

If there is no way to reach the desired goal state, return "No solution". Otherwise return any valid way of doing so by using at most 100 operations. More precisely, return one valid sequence of operations encoded as a string of 'L's and 'R's.

If there are multiple valid solutions, you may return any of them. In particular, you are not required to find the shortest valid solution. Any valid solution will be accepted as long as its length does not exceed 100. We can prove that if there is an valid solution then there must exist one with length at most 100.

Definition

    
Class: LR
Method: construct
Parameters: vector<long long>, vector<long long>
Returns: string
Method signature: string construct(vector<long long> s, vector<long long> t)
(be sure your method is public)

Limits

    
Time limit (s): 2.000
Memory limit (MB): 256
Stack limit (MB): 256

Constraints

- s will contain between 2 and 50 elements, inclusive.
- s and t will contain the same number of elements.
- Each element in s will be between 0 and 1,000,000,000,000,000 (10^15) inclusive.
- Each element in t will be between 0 and 1,000,000,000,000,000 (10^15) inclusive.

Examples

0)  
    
{0,1,0,0,0}
{0,1,2,1,0}
Returns: "LL"
The first operation L will change A into {0,1,1,0,0} and then the second operation L will produce the array we wanted.
1)  
    
{0,0,0,1}
{0,1,0,0}
Returns: "No solution"
Even though A is cyclic, the precise indices matter. Here, s and t are two different configurations, and there is no valid way to change this s into this t.
2)  
    
{1,2,3,4,5,6,7,8,9,10}
{12,24,56,95,12,78,0,100,54,88}
Returns: "No solution"
Regardless of the type and order of operations all elements of A will always remain positive. However, t contains a zero. Therefore, t cannot be reached.
3)  
    
{1,0,0}
{11,11,10}
Returns: "RRRRR"
The sequence of five operations R will change the array A as follows: {1,0,0} -> {1,0,1} -> {1,1,2} -> {2,3,3} -> {5,6,5} -> {11,11,10}.
4)  
    
{1,1}
{562949953421312,562949953421312}
Returns: "RLLLRRRLLRRRLRLRRLLLLRLLRRLRRRLRRLRRLLRRRLLRRRLLL"
We start with A[0] = A[1] = 1, and we want A[0] = A[1] = 2^49. We can easily verify that in this case each operation changes A from {x, x} into {2x, 2x}. Therefore, any string of exactly 49 'L's and 'R's is a valid answer.
5)  
    
{0,0,0,0,0}
{0,0,0,1,0}
Returns: "No solution"
 
6)  
    
{123,456}
{123,456}
Returns: ""
 

This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.

思路:因为这是一个圆形数列,所以L和R所得的数列是差不多的,只是左移右移一次的区别。

  所以先判断进行sum次操作(通过数列值的和判断)。

  然后先进行sum次L操作,再判断把进行sum次操作后的数列k次右平移后能否得到t数列。

  能到得到的话则是进行了k次R,sum-k次L操作,LR的先后顺序没有关系。

 // BEGIN CUT HERE

 #include <conio.h>
#include <sstream>
/*
*/
#define debuging
#ifdef debuging
#define FIN {freopen("new.in" , "r" , stdin) ;}
#define FOUT {freopen("new.out" , "w" , stdout) ;}
#define OUT(x) {cout<< #x << " : " << x <<endl ;}
#define ERR(x) {cout<<"#error: "<< x ; while(1) ;}
#endif
// END CUT HERE
#include <bits/stdc++.h> using namespace std; #define MP make_pair
#define PB push_back
typedef long long LL;
typedef pair<int,int> PII;
const double eps=1e-;
const double pi=acos(-1.0);
const int K=1e6+;
const int mod=1e9+; class LR
{
public:
string construct(vector<long long> s, vector<long long> t)
{
int cnt=;
LL suma=,sumb=,sum=;
string ans;
for(int i=; i<s.size(); i++)
suma+=s[i],sumb+=t[i];
if(suma==sumb) sum=;
for(int i=; i<=&&sum; i++)
if((suma*=) == sumb)
{
sum=i;
break;
}
if(suma!=sumb)
return "No solution";
for(LL i=,n=s.size(); i<sum; i++)
for(LL j=,ls=s[n-],tmp; j<n; j++)
tmp=s[j],s[j]+=ls,ls=tmp;
for(int i=; i<=sum; i++)
{
if(s==t)
{
cnt=i;break;
}
s.insert(s.begin(),s[s.size()-]);
s.erase(--s.end());
}
if(cnt>sum)
return "No solution";
if(sum==)
return "";
for(int i=; i<cnt; i++)
ans+="R";
for(int i=cnt; i<sum; i++)
ans+="L";
return ans;
} // 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();
if ((Case == -) || (Case == )) test_case_6();
}
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 string &Expected, const string &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()
{
LL Arr0[] = {,,,,};
vector<long long> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[])));
LL Arr1[] = {,,,,};
vector<long long> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[])));
string Arg2 = "LL";
verify_case(, Arg2, construct(Arg0, Arg1));
}
void test_case_1()
{
LL Arr0[] = {,,,};
vector<long long> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[])));
LL Arr1[] = {,,,};
vector<long long> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[])));
string Arg2 = "No solution";
verify_case(, Arg2, construct(Arg0, Arg1));
}
void test_case_2()
{
LL Arr0[] = {,,,,,,,,,};
vector<long long> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[])));
LL Arr1[] = {,,,,,,,,,};
vector<long long> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[])));
string Arg2 = "No solution";
verify_case(, Arg2, construct(Arg0, Arg1));
}
void test_case_3()
{
LL Arr0[] = {,,};
vector<long long> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[])));
LL Arr1[] = {,,};
vector<long long> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[])));
string Arg2 = "RRRRR";
verify_case(, Arg2, construct(Arg0, Arg1));
}
void test_case_4()
{
LL Arr0[] = {,};
vector<long long> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[])));
LL Arr1[] = {,};
vector<long long> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[])));
string Arg2 = "RLLLRRRLLRRRLRLRRLLLLRLLRRLRRRLRRLRRLLRRRLLRRRLLL";
verify_case(, Arg2, construct(Arg0, Arg1));
}
void test_case_5()
{
LL Arr0[] = {,,,,};
vector<long long> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[])));
LL Arr1[] = {,,,,};
vector<long long> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[])));
string Arg2 = "No solution";
verify_case(, Arg2, construct(Arg0, Arg1));
}
void test_case_6()
{
LL Arr0[] = {,};
vector<long long> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[])));
LL Arr1[] = {,};
vector<long long> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[])));
string Arg2 = "";
verify_case(, Arg2, construct(Arg0, Arg1));
} // END CUT HERE };
// BEGIN CUT HERE
int main()
{
LR ___test;
___test.run_test();
getch() ;
return ;
}
// END CUT HERE
上一篇:springboot 集成mybatis plus3


下一篇:Dictionary导致CPU暴涨