【贪心】Codeforces Round #407 (Div. 2) A. Anastasia and pebbles

贪心地一个一个尽可能往口袋里放,容易发现和顺序无关。

#include<cstdio>
#include<iostream>
using namespace std;
typedef long long ll;
int n,m,a[100100];
ll ans;
int main(){
// freopen("a.in","r",stdin);
scanf("%d%d",&n,&m);
for(int i=1;i<=n;++i){
scanf("%d",&a[i]);
}
int i=1;
while(i<=n){
if(a[i]<=m && a[i+1]<=m){
i+=2;
++ans;
}
else if(a[i]<=m && a[i+1]>m){
a[i+1]-=m;
++i;
++ans;
}
else if(a[i]<2*m){
++i;
++ans;
}
else if(a[i]>=2*m){
ans+=(ll)(a[i]/(2*m));
a[i]%=(2*m);
if(a[i]==0){
++i;
}
}
}
cout<<ans<<endl;
return 0;
}
上一篇:【分类讨论】Codeforces Round #407 (Div. 2) D. Weird journey


下一篇:【分类讨论】【set】Codeforces Round #407 (Div. 2) B. Masha and geometric depression