给你无限个 1-n的硬币 问你怎么最少的组成m
那么我们贪心的去组成m即可 所以直接除以n 判断是否除的尽即可
/*
if you can't see the repay
Why not just work step by step
rubbish is relaxed
to ljq
*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
using namespace std;
#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;
const ll mod = (int)1e9+7;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
int main()
{
//ios::sync_with_stdio(false);
//freopen("a.txt","r",stdin);
//freopen("b.txt","w",stdout);
long long n,m;
scanf("%lld%lld",&n,&m);
printf("%lld\n",m/n+(m%n==0?0:1));
//fclose(stdin);
//fclose(stdout);
//cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
return 0;
}
给你一个由n个快组成的图形 有俯视图和侧视图 问你最多去几个小格子使得侧视图和俯视图不变
我们给块排个序 然后贪心的每个块都留高度为之前块加一的块 如果高度超过块本身高度就留块本身高度 最后不够的用最后一个高块去补全即可
/*
if you can't see the repay
Why not just work step by step
rubbish is relaxed
to ljq
*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
using namespace std;
#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;
const ll mod = (int)1e9+7;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
long long arr[1000025];
int main()
{
//ios::sync_with_stdio(false);
//freopen("a.txt","r",stdin);
//freopen("b.txt","w",stdout);
int n;
long long m;
scanf("%d%lld",&n,&m);
for(int i = 1;i<=n;++i) scanf("%lld",&arr[i]);
sort(arr+1,arr+1+n);
long long ans = 0,maxx = arr[n],tmp = 0;
for(int i = 1;i<n;++i)
{
ans+=arr[i] - 1;
tmp=min(arr[i],tmp+1);
}
if(maxx>tmp) ans+=arr[n]-(maxx-tmp);
else ans+=arr[n] - 1;
printf("%lld\n",ans);
//fclose(stdin);
//fclose(stdout);
//cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
return 0;
}
n是 1e5 n^2的做法过不了 一开始很难处理
用dp做 定义 dp[i] 表示 i 长度的合法方案数 那么我们知道对每一个数的所有因子 x 可以让 dp[x] += dp[x-1] 因为代表可以放x-1这个位置后面的x位置上 从大往小枚举因子使得这次放的方案 不受自己因子的影响
/*
if you can't see the repay
Why not just work step by step
rubbish is relaxed
to ljq
*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
using namespace std;
#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;
const ll mod = (int)1e9+7;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
#define MOD 1000000007
vector<int > vt[1000025];
const int MAX_N = 1000025;
void init()
{
for(int i = 1;i<=1000000;i++)
{
for(int j = i;j<=1000000;j+=i)
{
vt[j].push_back(i);
}
}
}
int arr[100025];
long long dp[MAX_N];
int main()
{
//ios::sync_with_stdio(false);
//freopen("a.txt","r",stdin);
//freopen("b.txt","w",stdout);
int n;
init();
scanf("%d",&n);
for(int i = 1;i<=n;++i) scanf("%d",&arr[i]);
dp[0] = 1;
for(int i = 1;i<=n;++i)
{
for(int j = vt[arr[i]].size()-1;j>=0;j--)
{
int x = vt[arr[i]][j];
dp[x] = (dp[x] + dp[x-1])%MOD;
}
}
long long ans = 0;
for(int i = 1;i<=1000000;i++) ans = (ans+dp[i])%MOD;
printf("%lld\n",ans);
//fclose(stdin);
//fclose(stdout);
//cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
return 0;
}
题意 给你n个电视节目 以及x y 开一个新电视的贡献是 x + 时间 *y 否则是 上一个电视延续到结束时间 * y
所以我们把电视时间二维排序 按照时间从小到大 然后用优先队列去判断哪个时间更优 取更优的贪心即可
/*
if you can't see the repay
Why not just work step by step
rubbish is relaxed
to ljq
*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
using namespace std;
#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;
const ll mod = (int)1e9+7;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
#define MOD 1000000007
const int MAX_N = 100025;
struct node
{
int l,r,id;
bool operator<(const node other) const
{
if(l==other.l) return id < other.id;
return l < other.l;
}
}arr[MAX_N<<1];
priority_queue<int > q;
int main()
{
//ios::sync_with_stdio(false);
//freopen("a.txt","r",stdin);
//freopen("b.txt","w",stdout);
int n,l,r;
long long x,y,ans = 0;
scanf("%d%lld%lld",&n,&x,&y);
for(int i = 1;i<=n;++i)
{
scanf("%d%d",&l,&r);
arr[i*2 - 1].l = l,arr[i*2-1].r = r,arr[i*2-1].id = 0;
arr[i*2].l = r,arr[i*2].id = 1;
}
n*=2;
sort(arr+1,arr+1+n);
for(int i = 1;i<=n;++i)
{
if(arr[i].id==1)
q.push(arr[i].l);
else
{
if(!q.empty())
{
int top = q.top();
ll tmp1 = (arr[i].r - top) * y;
ll tmp2 = (arr[i].r - arr[i].l) * y + x;
if(tmp1<tmp2)
{
ans+=tmp1;
ans%=MOD;
q.pop();
}
else ans+=tmp2,ans%=MOD;
}
else ans = ans+x+(arr[i].r - arr[i].l)*y,ans%=MOD;
}
}
printf("%lld\n",ans);
//fclose(stdin);
//fclose(stdout);
//cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
return 0;
}