1. 程式人生 > >Increasing Sequence CodeForces - 11A

Increasing Sequence CodeForces - 11A

一個個 href 過去 note main 貪心 ast 錯誤 scanf

Increasing Sequence CodeForces - 11A

很簡單的貪心。由於不能減少元素,只能增加,過程只能是從左到右一個個看過去,看到一個小於等於左邊的數的數就把它加到比左邊大,並記錄加的次數。

錯誤記錄:

但是很容易錯...以前錯了4次..過幾個月來再做還是不能1A...

比如下面這個有很明顯錯誤的程序

 1 #include<cstdio>
 2 int n,d,last,now,ans;
 3 int main()
 4 {
 5     int i;
 6     scanf("%d%d",&n,&d);
 7
scanf("%d",&last); 8 for(i=2;i<=n;i++) 9 { 10 scanf("%d",&now); 11 if(now<=last) 12 { 13 ans+=(last-now)/d+1; 14 now+=d*ans; 15 } 16 last=now; 17 } 18 printf("%d",ans); 19 return 0; 20 }
2333333
 1 #include<cstdio>
 2 int n,d,last,now,ans;
 3 int main()
 4 {
 5     int i,tans;
 6     scanf("%d%d",&n,&d);
 7     scanf("%d",&last);
 8     for(i=2;i<=n;i++)
 9     {
10         scanf("%d",&now);
11         if(now<=last)
12         {
13             tans=(last-now)/d+1
; 14 ans+=tans; 15 now+=d*tans; 16 } 17 last=now; 18 } 19 printf("%d",ans); 20 return 0; 21 }

Increasing Sequence CodeForces - 11A