题目:

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的先后顺序没有关系。

  1 // BEGIN CUT HERE
  2
  3 #include <conio.h>
  4 #include <sstream>
  5 /*
  6 */
  7 #define debuging
  8 #ifdef debuging
  9 #define FIN  {freopen("new.in" , "r" , stdin) ;}
 10 #define FOUT {freopen("new.out" , "w" , stdout) ;}
 11 #define OUT(x)  {cout<< #x << "  : " << x <<endl ;}
 12 #define ERR(x)  {cout<<"#error: "<< x ; while(1) ;}
 13 #endif
 14 // END CUT HERE
 15 #include <bits/stdc++.h>
 16
 17 using namespace std;
 18
 19 #define MP make_pair
 20 #define PB push_back
 21 typedef long long LL;
 22 typedef pair<int,int> PII;
 23 const double eps=1e-8;
 24 const double pi=acos(-1.0);
 25 const int K=1e6+7;
 26 const int mod=1e9+7;
 27
 28
 29 class LR
 30 {
 31 public:
 32     string construct(vector<long long> s, vector<long long> t)
 33     {
 34         int cnt=101;
 35         LL suma=0,sumb=0,sum=1;
 36         string ans;
 37         for(int i=0; i<s.size(); i++)
 38             suma+=s[i],sumb+=t[i];
 39         if(suma==sumb)  sum=0;
 40         for(int i=1; i<=100&&sum; i++)
 41             if((suma*=2) == sumb)
 42             {
 43                 sum=i;
 44                 break;
 45             }
 46         if(suma!=sumb)
 47             return "No solution";
 48         for(LL i=0,n=s.size(); i<sum; i++)
 49             for(LL j=0,ls=s[n-1],tmp; j<n; j++)
 50                 tmp=s[j],s[j]+=ls,ls=tmp;
 51         for(int i=0; i<=sum; i++)
 52         {
 53             if(s==t)
 54             {
 55                 cnt=i;break;
 56             }
 57             s.insert(s.begin(),s[s.size()-1]);
 58             s.erase(--s.end());
 59         }
 60         if(cnt>sum)
 61             return "No solution";
 62         if(sum==0)
 63             return "";
 64         for(int i=0; i<cnt; i++)
 65             ans+="R";
 66         for(int i=cnt; i<sum; i++)
 67             ans+="L";
 68         return ans;
 69     }
 70
 71
 72 // BEGIN CUT HERE
 73 public:
 74     void run_test(int Case)
 75     {
 76         if ((Case == -1) || (Case == 0)) test_case_0();
 77         if ((Case == -1) || (Case == 1)) test_case_1();
 78         if ((Case == -1) || (Case == 2)) test_case_2();
 79         if ((Case == -1) || (Case == 3)) test_case_3();
 80         if ((Case == -1) || (Case == 4)) test_case_4();
 81         if ((Case == -1) || (Case == 5)) test_case_5();
 82         if ((Case == -1) || (Case == 6)) test_case_6();
 83     }
 84 private:
 85     template <typename T> string print_array(const vector<T> &V)
 86     {
 87         ostringstream os;
 88         os << "{ ";
 89         for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\",";
 90         os << " }";
 91         return os.str();
 92     }
 93     void verify_case(int Case, const string &Expected, const string &Received)
 94     {
 95         cerr << "Test Case #" << Case << "...";
 96         if (Expected == Received) cerr << "PASSED" << endl;
 97         else
 98         {
 99             cerr << "FAILED" << endl;
100             cerr << "\tExpected: \"" << Expected << '\"' << endl;
101             cerr << "\tReceived: \"" << Received << '\"' << endl;
102         }
103     }
104     void test_case_0()
105     {
106         LL Arr0[] = {0,1,0,0,0};
107         vector<long long> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0])));
108         LL Arr1[] = {0,1,2,1,0};
109         vector<long long> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0])));
110         string Arg2 = "LL";
111         verify_case(0, Arg2, construct(Arg0, Arg1));
112     }
113     void test_case_1()
114     {
115         LL Arr0[] = {0,0,0,1};
116         vector<long long> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0])));
117         LL Arr1[] = {0,1,0,0};
118         vector<long long> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0])));
119         string Arg2 = "No solution";
120         verify_case(1, Arg2, construct(Arg0, Arg1));
121     }
122     void test_case_2()
123     {
124         LL Arr0[] = {1,2,3,4,5,6,7,8,9,10};
125         vector<long long> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0])));
126         LL Arr1[] = {12,24,56,95,12,78,0,100,54,88};
127         vector<long long> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0])));
128         string Arg2 = "No solution";
129         verify_case(2, Arg2, construct(Arg0, Arg1));
130     }
131     void test_case_3()
132     {
133         LL Arr0[] = {1,0,0};
134         vector<long long> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0])));
135         LL Arr1[] = {11,11,10};
136         vector<long long> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0])));
137         string Arg2 = "RRRRR";
138         verify_case(3, Arg2, construct(Arg0, Arg1));
139     }
140     void test_case_4()
141     {
142         LL Arr0[] = {1,1};
143         vector<long long> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0])));
144         LL Arr1[] = {562949953421312,562949953421312};
145         vector<long long> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0])));
146         string Arg2 = "RLLLRRRLLRRRLRLRRLLLLRLLRRLRRRLRRLRRLLRRRLLRRRLLL";
147         verify_case(4, Arg2, construct(Arg0, Arg1));
148     }
149     void test_case_5()
150     {
151         LL Arr0[] = {0,0,0,0,0};
152         vector<long long> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0])));
153         LL Arr1[] = {0,0,0,1,0};
154         vector<long long> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0])));
155         string Arg2 = "No solution";
156         verify_case(5, Arg2, construct(Arg0, Arg1));
157     }
158     void test_case_6()
159     {
160         LL Arr0[] = {123,456};
161         vector<long long> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0])));
162         LL Arr1[] = {123,456};
163         vector<long long> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0])));
164         string Arg2 = "";
165         verify_case(6, Arg2, construct(Arg0, Arg1));
166     }
167
168 // END CUT HERE
169
170 };
171 // BEGIN CUT HERE
172 int main()
173 {
174     LR ___test;
175     ___test.run_test(3);
176     getch() ;
177     return 0;
178 }
179 // END CUT HERE

转载于:https://www.cnblogs.com/weeping/p/6739263.html

topcoder SRM712 Div1 LR相关推荐

  1. TopCoder SRM502 Div1 1000 动态规划

    原文链接https://www.cnblogs.com/zhouzhendong/p/SRM502-1000.html SRM502 Div1 1000 题意 从 [0,n-1] 中选择 k 个不同的 ...

  2. noip2017考前整理(未完)

    快考试了,把我以前写过的题回顾一下. Noip2007 树网的核:floyd,推出性质,暴力. Noip2008 笨小猴:模拟 Noip2008 火柴棒等式:枚举 Noip2008 传纸条:棋盘dp ...

  3. 最完整的面试经历 - FaceBook PHD面试经历

    最完整的面试经历 2014-01-16 MITBBS 待字闺中 昨天收到FB的电话,我的OFFER已经批下来了,这也意味着我的JOB HUNTING结束了,下 面是我这两个月来申请结果汇总: Appl ...

  4. TOPCODER SAM 686 div1 300

    // TOPCODER SAM 686 div1 300 Problem Statement 带有小中括号的括号序列,问可以去掉多少子串,使得剩下的非空串是合法的. Constraints 字符串长度 ...

  5. topcoder srm 714 div1

    problem1 link 倒着想.每次添加一个右括号再添加一个左括号,直到还原.那么每次的右括号的选择范围为当前左括号后面的右括号减去后面已经使用的右括号. problem2 link 令$h(x) ...

  6. topcoder srm 691 div1 -3

    1.给定一个$n$个顶点$n$个边的图,边是$(i,a_{i})$,顶点编号$[0,n-1]$.增加一个顶点$n$,现在选出一个顶点集$M$,对于任意的在$M$中 的顶点$x$,去掉边$(x,a_{x ...

  7. topcoder srm 706 div1

    1.给定一个迷宫,点号表示不可行,井号表示可行.现在可以改变其中的一些井号的位置.问最少改变多少个井号可以使得从左上角到右下角存在路径. 思路:设高为$n$,宽为$m$,若井号的个数$S$小于$n+m ...

  8. topcoder srm 694 div1 -3

    1.给出$n$个数字,将其分成三个非空的组,每组的权值为该组所有数字的抑或.选择一种分法使得三组的权值和最大? 思路:记录前两组的权值且三组有没有数字时第三组的值.(当前两组的值知道时第三组的权值是确 ...

  9. topcoder srm 330 div1

    problem1 link 直接模拟. import java.util.*; import java.math.*; import static java.lang.Math.*;public cl ...

最新文章

  1. CSS教你玩转背景background-position(1)
  2. STL中用erase()方法遍历删除元素
  3. #华为云·寻找黑马程序员#【代码重构之路】我是如何将8行代码缩减成1行的
  4. 马斯克的星链计划对互联网有哪些影响?
  5. win10改计算机用户名,win10如何改成自己想要的文件夹用户名?
  6. 博文视点大讲堂35期《Google Android创赢路线与产品开发实战》读者见面会
  7. ArrayList源码剖析
  8. Linux 多线程编程 (典藏、含代码)
  9. 精心整理的NGINX面试题
  10. [Java聊天室服务器]实战之六 去除死链接
  11. PWM控制的基本原理
  12. 云计算如何运用在政府行业--解决方案
  13. Android 沉浸式(透明)状态栏细研-超级细还附 Demo
  14. 什么是集体户口,优势、劣势
  15. Excel的使用心得与技巧
  16. mac m1使用picGo + gitee搭建免费图床
  17. VLC初始加载优化:avformat_find_stream_info接口延迟降低
  18. 汇编——32位汇编基础框架
  19. java实现生日相同概率
  20. android H5开发出现广告,常见的移动端H5页面开发遇到的坑和解决办法

热门文章

  1. VC中设置头文件的搜索路径~~
  2. [bash]删除文件中含特定字符串的行
  3. 循环队列及C语言实现三
  4. [html] 如何在页面上显示Emoji表情?
  5. [vue] 有在vue中使用过echarts吗?踩过哪些坑?如何解决的?
  6. [vue] 说说你对vue的mixin的理解,有什么应用场景?
  7. [vue] EventBus注册在全局上时,路由切换时会重复触发事件,如何解决呢?
  8. 工作237:vuex取值
  9. 工作226:for循环逻辑
  10. 前端学习(1687):前端系列javascript基础面试前言