kotori和糖果(OEIS)

链接:https://ac.nowcoder.com/acm/contest/940/A

kotori共有n块糖果,每块糖果的初始状态是分散的,她想把这些糖果聚在一堆。但她每次只能把两堆糖果合并成一堆。

已知把两堆数量为a和b的糖果聚在一堆的代价是|a-b|。

kotori想知道,她把这n块糖果聚在一堆的最小代价是多少?

 

解:OEIS搜索序列前12个数,出来唯一一的结果,该序列介绍中有如下这一条:

the minimum cost of connecting n+1 nodes when the cost of joining two connected components is the absolute difference of their sizes. In particular, connecting two equal sized components has zero cost. For example, in the case of n=4 there are 5 nodes. Connecting nodes 1 and 2 costs zero, connecting nodes 3 and 4 costs zero, then connecting {5} to {3,4} costs 1 and finally connecting {1,2} to {3,4,5} costs 1 giving a total cost of 2. Because this solution is optimal a(4) = 2.

正符合题意!

序列的计算式 formula :a(0) = a(1) = 0; a(2*n) = a(n) + a(n-1) + 1; a(2*n+1) = 2*a(n)

写出这个递归就好啦。

数比较大,用map存。

然后递归写挂了(不

 

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
map<ll,ll> mp;
ll getans(ll n){
    if(n==0) return 0;
    if(mp.count(n)) return mp[n];
    if(n&1){
        mp[n]=2*getans(n/2);
    }else{
        mp[n]=getans(n/2)+getans(n/2-1)+1;
    }
    return mp[n];
}
int main(){
    ll t;
    scanf("%lld",&t);
    while(t--){
        ll n;
        scanf("%lld",&n);
        if(n==0)puts("0");
        else printf("%lld\n",getans(n-1));
    }
}

 

 

 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以使用GridView和Adapter来展示相册中的图片。首先,您需要获取相册中的图片路径,然后将其加载到GridView中。以下是一个简单的示例代码: 1. 获取相册中的图片路径 ```java private ArrayList<String> getGalleryPhotos() { ArrayList<String> galleryList = new ArrayList<String>(); final String[] projection = { MediaStore.Images.Media.DATA }; final String orderBy = MediaStore.Images.Media.DATE_TAKEN; Cursor imageCursor = getContentResolver().query( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, null, null, orderBy + " DESC"); if (imageCursor != null && imageCursor.moveToFirst()) { do { String path = imageCursor.getString(imageCursor .getColumnIndex(MediaStore.Images.Media.DATA)); galleryList.add(path); } while (imageCursor.moveToNext()); } imageCursor.close(); return galleryList; } ``` 2. 加载图片到GridView中 ```java GridView gridView = (GridView) findViewById(R.id.gridview); ArrayList<String> galleryList = getGalleryPhotos(); gridView.setAdapter(new ImageAdapter(this, galleryList)); ``` 3. 实现ImageAdapter ```java public class ImageAdapter extends BaseAdapter { private Context context; private ArrayList<String> galleryList; public ImageAdapter(Context context, ArrayList<String> galleryList) { this.context = context; this.galleryList = galleryList; } public int getCount() { return galleryList.size(); } public Object getItem(int position) { return galleryList.get(position); } public long getItemId(int position) { return position; } public View getView(int position, View convertView, ViewGroup parent) { ImageView imageView; if (convertView == null) { imageView = new ImageView(context); imageView.setLayoutParams(new GridView.LayoutParams(200, 200)); imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); } else { imageView = (ImageView) convertView; } // 加载图片 Glide.with(context).load(galleryList.get(position)).into(imageView); return imageView; } } ``` 这样就可以在GridView中展示相册中的图片了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值