题解:两者相除,向上取整。
题解:将所有 n n n的 a [ i ] a[i] a[i]相加,与 h h h比较即可。
#include
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
#define rush() int T;scanf("%d",&T);while(T--)
#define mm(a,b) memset(a,b,sizeof(a))
#define pii pair
#define mp make_pair
#define pb push_back
#define sc(a) scanf("%d",&a)
#define sc2(a,b) scanf("%d%d",&a,&b)
#define pf(a) printf("%d\n",a)
#define pf2(a,b) printf("%d %d\n",a,b)
#define p_f(a) printf("%d ",a)
#define fi first
#define se second
#define db double
#define ll long long
using namespace std;
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3f;
const db eps=1e-9;
int h,n,x;
int main()
{
cin>>h>>n;
int ans=0;
rep(i,1,n)cin>>x,ans+=x;
if(ans>=h)puts("Yes");
else puts("No");
return 0;
}
题解:排序,将大的移除为0,剩余的加起来就是答案了
#include
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
#define rush() int T;scanf("%d",&T);while(T--)
#define mm(a,b) memset(a,b,sizeof(a))
#define pii pair
#define mp make_pair
#define pb push_back
#define sc(a) scanf("%d",&a)
#define sc2(a,b) scanf("%d%d",&a,&b)
#define pf(a) printf("%d\n",a)
#define pf2(a,b) printf("%d %d\n",a,b)
#define p_f(a) printf("%d ",a)
#define fi first
#define se second
#define db double
#define ll long long
using namespace std;
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3f;
const db eps=1e-9;
ll n,k,h[200005];
int main()
{
cin>>n>>k;
rep(i,1,n)cin>>h[i];
sort(h+1,h+n+1);
ll ans=0;
int lim=max(0ll,n-k);
rep(i,1,lim)ans+=h[i];
cout<<ans<<endl;
return 0;
}
题解:可转化为若干个 2 2 2的幂次相加
#include
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
#define rush() int T;scanf("%d",&T);while(T--)
#define mm(a,b) memset(a,b,sizeof(a))
#define pii pair
#define mp make_pair
#define pb push_back
#define sc(a) scanf("%d",&a)
#define sc2(a,b) scanf("%d%d",&a,&b)
#define pf(a) printf("%d\n",a)
#define pf2(a,b) printf("%d %d\n",a,b)
#define p_f(a) printf("%d ",a)
#define fi first
#define se second
#define db double
#define ll long long
using namespace std;
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3f;
const db eps=1e-9;
ll h;
int main()
{
cin>>h;
ll ans=0;
int tot=1;
while(h>=1){
ans+=pow(2,tot-1);
h=h/2;
tot++;
}
cout<<ans<<endl;
return 0;
}
题解:可以转化为完全背包问题,将生命值看成体积,所要达到的花费最小值,可以将体积扩大上一倍
#include
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
#define rush() int T;scanf("%d",&T);while(T--)
#define mm(a,b) memset(a,b,sizeof(a))
#define pii pair
#define mp make_pair
#define pb push_back
#define sc(a) scanf("%d",&a)
#define sc2(a,b) scanf("%d%d",&a,&b)
#define pf(a) printf("%d\n",a)
#define pf2(a,b) printf("%d %d\n",a,b)
#define p_f(a) printf("%d ",a)
#define fi first
#define se second
#define db double
#define ll long long
using namespace std;
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3f;
const db eps=1e-9;
const int N=1e3+5;
int h,n,a[N],b[N],f[25000];
int main()
{
cin>>h>>n;
rep(i,1,n)cin>>a[i]>>b[i];
mm(f,inf);
f[0]=0;
rep(i,1,n)rep(j,a[i],h+10000){
f[j]=min(f[j],f[j-a[i]]+b[i]);
}
int ans=inf;
rep(i,h,h+10000)ans=min(ans,f[i]);
cout<<ans<<endl;
return 0;
}
题解:刚开始用的是链表, T T T在最后两组,最后想了下可以用队列进行维护,首先贪心从左往右扫描着做是必须的,但是得维护此时做的这个对于后面的影响,也可以将这些影响放入队列中,然后队列中加一个总标记,如果对于左标记已经达不到,那么出队,把总标记删除出队的。
#include
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
#define rush() int T;scanf("%d",&T);while(T--)
#define mm(a,b) memset(a,b,sizeof(a))
#define pii pair
#define mp make_pair
#define pb push_back
#define sc(a) scanf("%d",&a)
#define sc2(a,b) scanf("%d%d",&a,&b)
#define pf(a) printf("%d\n",a)
#define pf2(a,b) printf("%d %d\n",a,b)
#define p_f(a) printf("%d ",a)
#define fi first
#define se second
#define db double
#define ll long long
using namespace std;
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3f;
const db eps=1e-9;
const int N=2e5+5;
ll n,d,a;
pair<ll,ll>dots[N];
queue<pair<ll,ll> >q;
int main()
{
cin>>n>>d>>a;
rep(i,1,n)cin>>dots[i].fi>>dots[i].se;
sort(dots+1,dots+n+1);
ll sum=0,ans=0;
rep(i,1,n){
int lim=dots[i].fi-d;
while(!q.empty()){
pair<ll,ll> u=q.front();
if(u.fi<lim){
sum-=u.se;
q.pop();
}else break;
}
dots[i].se-=min(dots[i].se,sum*a);
if(dots[i].se){
q.push({dots[i].fi+d,ceil(dots[i].se*1.0/a)});
ans+=ceil(dots[i].se*1.0/a);
sum+=ceil(dots[i].se*1.0/a);
dots[i].se=0;
}
}
cout<<ans<<endl;
return 0;
}