题目:
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:
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 |
|||||||||||||
|
|||||||||||||
Limits |
|||||||||||||
|
|||||||||||||
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) | |||||||||||||
|
|||||||||||||
1) | |||||||||||||
|
|||||||||||||
2) | |||||||||||||
|
|||||||||||||
3) | |||||||||||||
|
|||||||||||||
4) | |||||||||||||
|
|||||||||||||
5) | |||||||||||||
|
|||||||||||||
6) | |||||||||||||
|
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<=&∑ 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