JZ初中OJ 1999.[2015.8.6普及組模擬賽] Wexley接蘋果


題目描述

         Wexley最近發現了一個古老的屏幕游戲。游戲的屏幕被划分成n列。在屏幕的底端,有一個寬為m列的籃子(m<n)。在游戲過程中,Wexley能左右移動這個籃子,            Wexley的操作很犀利,移動是瞬間完成的,但是籃子必須始終都在屏幕中。 蘋果從屏幕的頂端落下,每個蘋果從n列中的某一列頂端掉落,垂直掉落到屏幕的底端。每個蘋果總是在上一個蘋果掉落到底端的時候開始掉落。Wexley想要通過移動籃子來接住所有的蘋果。起先,籃子在屏幕的最左端。
         求出Wexley要接住所有的蘋果所需移動的最短距離。 
 

輸入

第一行,兩個整數n、m,如題所述
第二行,一個整數k,表示掉落的蘋果總數
接下來k行,每行一個整數Ai,表示每個蘋果掉落的位置

輸出

一行一個整數,表示所需移動最短距離
 

樣例輸入

Sample Input1:
5 1
3
1
5
3

Sample Input2:
5 2
3
1
5
3

 

樣例輸出

Sample Output1:
6

Sample Output2:
4

 
 

數據范圍限制

【數據范圍】
對於30%的數據,m<n<=5,k<=10
對於100%的數據,1<=m<n<=10,1<=k<=20
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int n,m,k,a[21];
 4 int b[21],head,ans,tmp;
 5 int main()
 6 {
 7     freopen("apple.in","r",stdin);
 8     freopen("apple.out","w",stdout);
 9     cin>>n>>m>>k;
10     b[0]=1;
11     for(int i=1;i<=k;i++)
12     {
13         cin>>a[i];
14     }
15     head=m;
16     for(int i=1;i<=k;i++)
17     {
18         if(head>=a[i] && (head-m+1)<=a[i])
19         continue;
20         if(head<a[i])
21         {
22             ans+=a[i]-head;
23             head+=a[i]-head;
24             continue;
25         }
26         if((head-m+1)>a[i])
27         {
28             int tmp=head;
29             head=a[i]+m-1;
30             ans+=tmp-head;
31             continue;
32         }
33     }
34     cout<<ans;
35 }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM