[AtCoder-ARC073F]Many Moves

題目大意:
  有一排n個格子和2枚硬幣。
  現在有q次任務,每一次要你把其中一枚硬幣移到x的位置上,移動1格的代價是1。
  兩枚硬幣不能同時移動,任務必須按次序完成。
  現在告訴你兩枚硬幣初始狀態所在的位置a和b,問完成所有任務的最小代價。

思路:
  很容易想到一個O(qn)的DP。
  由于完成任務的次序確定,每個任務的位置也確定,我們可以用f[i][j]表示完成第i個任務后,一個硬幣在x[i],一個硬幣在j的最小代價。
  轉移方程為f[i][j]=min{f[i-1][j]+|x[i]-x[i-1]|},f[i][a[i-1]]=min{f[i-1][j]+|x[i]-j|}。
  然而這樣還是會TLE,在AtCoder上只過了14/34的測試數據。
  不難發現,在狀態轉移方程中,如果我們能去掉絕對值,里面的東西就能用線段樹維護。
  而絕對值的取值只與硬幣的左右位置關系有關。
  因此我們可以建2棵線段樹,一棵表示被轉移的狀態在目標狀態左邊,一棵表示在右邊。
  左線段樹中每個葉子結點x[i-1]維護f[i-1][j]-x[i-1]的值,右線段樹每個葉子結點x[i-1]維護f[i-1][j]+x[i-1]的值。
  看了一下榜,發現排在前面的基本上都是用樹狀數組做的。
  然而用樹狀數組維護區間最值難道不是O(log^2 n)的嗎?
  事實上我們可以發現線段樹上維護的東西只會越來越小,這樣我們可以直接在樹狀數組上修改,不用考慮原來的最小值沒了怎么辦。
  然后我又在樹狀數組里面加了一個剪枝。
  這樣隨隨便便就能拿Rank1。

  1 #include<cstdio>
  2 #include<cctype>
  3 #include<cstdlib>
  4 #include<algorithm>
  5 typedef signed long long int int64;
  6 inline unsigned getint() {
  7     register char ch;
  8     while(!isdigit(ch=getchar()));
  9     register unsigned x=ch^'0';
 10     while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
 11     return x;
 12 }
 13 inline int64 min(const int64 &a,const int64 &b) {
 14     return a<b?a:b;
 15 }
 16 const int64 inf=0x7ffffffffffffff;
 17 const int N=200001;
 18 int n;
 19 class FenwickTree {
 20     private:
 21         int64 val[N];
 22         int lowbit(const int &x) const {
 23             return x&-x;
 24         }
 25     public:
 26         FenwickTree() {
 27             std::fill(&val[0],&val[N],inf);
 28         }
 29         void modify(int p,const int64 &x) {
 30             while(p<=n) {
 31                 if(x<val[p]) {
 32                     val[p]=x;
 33                 } else {
 34                     return;
 35                 }
 36                 p+=lowbit(p);
 37             }
 38         }
 39         int64 query(int p) const {
 40             int64 ret=inf;
 41             while(p) {
 42                 ret=min(ret,val[p]);
 43                 p-=lowbit(p);
 44             }
 45             return ret;
 46         }
 47 };
 48 FenwickTree ta;
 49 class RevFenwickTree {
 50     private:
 51         int64 val[N];
 52         int lowbit(const int &x) const {
 53             return x&-x;
 54         }
 55     public:
 56         RevFenwickTree() {
 57             std::fill(&val[0],&val[N],inf);
 58         }
 59         void modify(int p,const int64 &x) {
 60             while(p) {
 61                 if(x<val[p]) {
 62                     val[p]=x;
 63                 } else {
 64                     return;
 65                 }
 66                 p-=lowbit(p);
 67             }
 68         }
 69         int64 query(int p) const {
 70             int64 ret=inf;
 71             while(p<=n) {
 72                 ret=min(ret,val[p]);
 73                 p+=lowbit(p);
 74             }
 75             return ret;
 76         }
 77 };
 78 RevFenwickTree tb;
 79 int64 f[N];
 80 inline void modify(const int &p,const int64 x) {
 81     if(x<f[p]) {
 82         f[p]=x;
 83         ta.modify(p,x-p);
 84         tb.modify(p,x+p);
 85     }
 86 }
 87 int main() {
 88     n=getint();
 89     int q=getint(),a=getint(),b=getint();
 90     std::fill(&f[0],&f[N],inf);
 91     modify(a,0);
 92     int64 sum=0;
 93     while(q--) {
 94         a=b;
 95         b=getint();
 96         sum+=abs(a-b);
 97         int64 t1=ta.query(b)+b,t2=tb.query(b)-b;
 98         modify(a,min(t1,t2)-abs(a-b));
 99     }
100     int64 tmp=inf;
101     for(register int i=1;i<=n;i++) {
102         tmp=min(tmp,f[i]);
103     }
104     printf("%lld\n",tmp+sum);
105     return 0;
106 }

原來的O(n^2)DP程序:

 1 #include<cstdio>
 2 #include<cctype>
 3 #include<cstring>
 4 #include<cstdlib>
 5 inline unsigned getint() {
 6     register char ch;
 7     while(!isdigit(ch=getchar()));
 8     register unsigned x=ch^'0';
 9     while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
10     return x;
11 }
12 inline unsigned min(const unsigned &a,const unsigned &b) {
13     return a<b?a:b;
14 }
15 const unsigned N=200000;
16 unsigned long long f[2][N];
17 unsigned a[2];
18 int main() {
19     unsigned n=getint(),q=getint();
20     memset(f[0],0xff,n<<3);
21     a[0]=getint()-1,f[0][getint()-1]=0;
22     for(register unsigned i=1;i<=q;i++) {
23         a[i&1]=getint()-1;
24         memset(f[i&1],0xff,n<<3);
25         for(register unsigned j=0;j<n;j++) {
26             if(!~f[~i&1][j]) continue;
27             f[i&1][j]=min(f[i&1][j],f[~i&1][j]+abs(a[i&1]-a[~i&1]));
28             f[i&1][a[~i&1]]=min(f[i&1][a[~i&1]],f[~i&1][j]+abs(a[i&1]-j));
29         }
30     }
31     unsigned long long ans=~0;
32     for(register unsigned i=0;i<n;i++) {
33         ans=min(ans,f[q&1][i]);
34     }
35     printf("%llu\n",ans);
36     return 0;
37 }
View Code

?

轉載于:https://www.cnblogs.com/skylee03/p/7609824.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/540556.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/540556.shtml
英文地址,請注明出處:http://en.pswp.cn/news/540556.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

ScalavsKotlin

Is Scala better that Kotlin? No..., Is Kotlin better than Scala? No... Scala比Kotlin更好嗎&#xff1f; 不...&#xff0c;Kotlin勝過Scala嗎&#xff1f; 沒有... Both programming languages have their own profits and are for a specific set of development. It…

工業智能相機與基于PC的機器視覺的區別比較

隨著科技的日漸成熟&#xff0c;機器視覺得到了飛速發展。由于嵌入式技術的發展,近幾年智能相機性能顯著提高&#xff0c;越來越多必須依賴于PC處理的應用開始向智能相機平臺傾斜。低成本、高可靠性及易于安裝維護等優勢&#xff0c;使得機器視覺在制造業上的規模性應用越來越普…

[轉載] python skimage在圖像處理中的用法

參考鏈接&#xff1a; 在Python中打印單變量和多變量 基于python腳本語言開發的數字圖片處理包&#xff0c;比如PIL,Pillow, opencv, scikit-image等。 PIL和Pillow只提供最基礎的數字圖像處理&#xff0c;功能有限&#xff1b;opencv實際上是一個c庫&#xff0c;只是提供了py…

scala元組 數組_Scala中的數組

scala元組 數組Scala中的數組 (Arrays in Scala) An array is a linear data structure with a fixed number of elements. It is a collection that stores a fixed number Arrays in Scalf elements of the same datatype. In Scala, an array is 0 indexed, i.e. the first …

OpenStack —— DevStack一鍵自動化安裝

一、DevStack介紹Devstack目前是支持Ubuntu16.04和CentOS 7&#xff0c;而且Devstack官方建議使用Ubuntu16.04&#xff0c;所以我們使用Ubuntu 16.04進行安裝。默認無論是Devstack和OpenStack&#xff0c;都是采用Master的代碼進行安裝&#xff0c;這樣經常會出現&#xff0c;今…

[轉載] Python學習筆記——運維和Shell

參考鏈接&#xff1a; 在C / C&#xff0c;Python&#xff0c;PHP和Java中交換兩個變量 目錄 什么是運維 運維第一工具-shell編程 shell歷史 執行腳本 基本語法 Shell腳本語法 條件測試&#xff1a;test [ if/then/elif/else/fi case/esac for/do/done …

scala java混合_Scala特性混合

scala java混合Scala | 特性混合 (Scala | Trait Mixins ) In Scala, the number of traits can be extended using a class or an abstract class. This is known as Trait Mixins. For extending, only traits, the blend of traits, class or abstract class are valid. If …

Scala鑄造

Scala中的類型 (Types in Scala) Type also know as data type tells the compiler about the type of data that is used by the programmer. For example, if we initialize a value or variable as an integer the compiler will free up 4 bytes of memory space and it wi…

/ 卡路里_最大卡路里

/ 卡路里Problem statement: 問題陳述&#xff1a; Shivang is very foodie but he has a diet plan. He has an array of elements indicating the calorie of food he can consume on that day. In his diet plan, he can’t eat on for three consecutive days. But since …

[轉載] Python類中的私有變量和公有變量

參考鏈接&#xff1a; Python中的私有變量 我們這里就直奔主題&#xff0c;不做基礎鋪墊&#xff0c;默認你有一些Python類的基礎&#xff0c;大家在看這篇博客的時候&#xff0c;如果基礎知識忘了&#xff0c;可以去菜鳥教程 從一個簡單的類開始 class A(): #定義一…

OpenCV探索之路(二十五):制作簡易的圖像標注小工具

搞圖像深度學習的童鞋一定碰過圖像數據標注的東西&#xff0c;當我們訓練網絡時需要訓練集數據&#xff0c;但在網上又沒有找到自己想要的數據集&#xff0c;這時候就考慮自己制作自己的數據集了&#xff0c;這時就需要對圖像進行標注。圖像標注是件很枯燥又很費人力物力的一件…

固件的完整形式是什么?

FW&#xff1a;前進 (FW: Forward) FW is an abbreviation of "Forward". FW是“ Forward”的縮寫 。 It is an expression, which is commonly used in Gmail or messaging platform. It is also written as FWD or Fwd or Fw. It shows that the email has been s…

[轉載] python __slots__ 詳解(上篇)

參考鏈接&#xff1a; Python的__name __(特殊變量) python中的new-style class要求繼承Python中的一個內建類型&#xff0c; 一般繼承object&#xff0c;也可以繼承list或者dict等其他的內建類型。 在python新式類中&#xff0c;可以定義一個變量__slots__&#xff0c;它的作…

委托BegionInvoke和窗體BegionInvoke

委托BegionInvoke是指通過委托方法執行多線程任務&#xff0c;例如&#xff1a; //定義委托成員變量 delegate void dg_DeleAirport(); //指定委托函數 dg_DeleAirport dga AirportBLL.DeleteHistoryTransAirport; //通過BeginInvoke以異步線程方式執行委托函數&#xff0c;可…

圖論 弦_混亂的弦

圖論 弦Problem statement: 問題陳述&#xff1a; You are provided an input string S and the string "includehelp". You need to figure out all possible subsequences "includehelp" in the string S? Find out the number of ways in which the s…

[轉載] Python列表操作

參考鏈接&#xff1a; Python中的基本運算符 Python列表&#xff1a; 序列是Python中最基本的數據結構。序列中的每個元素都分配一個數字 - 它的位置&#xff0c;或索引&#xff0c;第一個索引是0&#xff0c;第二個索引是1&#xff0c;依此類推&#xff1b; Python有6個序列的…

「原創」從馬云、馬化騰、李彥宏的對話,看出三人智慧差在哪里?

在今年中國IT領袖峰會上&#xff0c;馬云、馬化騰、李彥宏第一次單獨合影&#xff0c;同框畫面可以說很難得了。BAT關心的走勢一直是同行們競相捕捉的熱點&#xff0c;所以三位大Boss在這次大會上關于人工智能的見解&#xff0c;也受到廣泛關注與多方解讀。馬云認為機器比人聰明…

python 注釋含注釋_Python注釋

python 注釋含注釋Python注釋 (Python comments) Comments in Python are used to improve the readability of the code. It is useful information given by the programmer in source code for a better understanding of code and logic that they have used to solve the …

C2的完整形式是什么?

C2&#xff1a;核心2 (C2: Core 2) C2 is an abbreviation of "Core 2" or "Intel Core 2". C2是“ Core 2”或“ Intel Core 2”的縮寫 。 It is a family of Intels processor which was launched on the 27th of July, 2006. It comprises a series of…