Codeforces Round #639 (Div. 2) B.Card Constructions
A card pyramid of height 11 is constructed by resting two cards against each other. For h>1h>1, a card pyramid of height hh is constructed by placing a card pyramid of height h−1h−1 onto a base. A base consists of hh pyramids of height 11, and h−1h−1 cards on top. For example, card pyramids of heights 11, 22, and 33 look as follows:

You start with nn cards and build the tallest pyramid that you can. If there are some cards remaining, you build the tallest pyramid possible with the remaining cards. You repeat this process until it is impossible to build another pyramid. In the end, how many pyramids will you have constructed?
Each test consists of multiple test cases. The first line contains a single integer tt (1≤t≤10001≤t≤1000) — the number of test cases. Next tt lines contain descriptions of test cases.
Each test case contains a single integer nn (1≤n≤1091≤n≤109) — the number of cards.
It is guaranteed that the sum of nn over all test cases does not exceed 109109.
For each test case output a single integer — the number of pyramids you will have constructed in the end.
5 3 14 15 24 1
1 2 1 3 0
预处理出不同层数对应的金字塔所需要的边,然后二分查找第一个小于等于n的数,累加答案更新n继续进行直到n<2。
别看代码,直接从最大的数往下遍历就好(不用每次都二分
#include <bits/stdc++.h> using namespace std; int h[100005]; int main() { int i; h[1]=2; for(i=2;i<=100000;i++) { h[i]=2+(i-1)*5+(i-1)*(i-2)/2*3; } int t; cin>>t; while(t--) { int n,ans=0; cin>>n; while(n>=2) { int pos=lower_bound(h+1,h+100000+1,n)-h; if(h[pos]==n) { ans++; break; } else pos--; if(pos<1||pos>100000)break; ans++; n-=h[pos]; } cout<<ans<<endl; } return 0; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!