#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define inf 0x3f3f3f3f
#define IOS ios_base::sync_with_stdio(0); cin.tie(0);
#define rep(i, a, n) for(register int i = a; i <= n; ++ i)
#define per(i, a, n) for(register int i = n; i >= a; -- i)
#define ONLINE_JUDGE
using namespace std;
typedef long long ll;
const int mod=1e9+7;
template<typename T>void write(T x)
{
if(x<0)
{
putchar('-');
x=-x;
}
if(x>9)
{
write(x/10);
}
putchar(x%10+'0');
}
template<typename T> void read(T &x)
{
x = 0;char ch = getchar();ll f = 1;
while(!isdigit(ch)){if(ch == '-')f*=-1;ch=getchar();}
while(isdigit(ch)){x = x*10+ch-48;ch=getchar();}x*=f;
}
ll gcd(ll a,ll b){return b==0?a:gcd(b,a%b);}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;};
ll ksm(ll a,ll n){//看是否要mod
ll ans=1;
while(n){
if(n&1) ans=(ans*a)%mod;
a=a*a%mod;
n>>=1;
}
return ans%mod;
}
//==============================================================
ll n,m,k,t;
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
clock_t c1 = clock();
//===========================================================
read(t);
while(t--){
read(n),read(m),read(k);
ll p=n/k;
ll j=min(p,m);
ll j2=(m-j)/(k-1);
if((m-j)%(k-1)) j2++;
write(j-j2);
cerr<<j<<" "<<j2<<endl;
putchar('\n');
}
//===========================================================
std::cerr << "Time:" << clock() - c1 << "ms" << std::endl;
return 0;
}
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define inf 0x3f3f3f3f
#define IOS ios_base::sync_with_stdio(0); cin.tie(0);
#define rep(i, a, n) for(register int i = a; i <= n; ++ i)
#define per(i, a, n) for(register int i = n; i >= a; -- i)
#define ONLINE_JUDGE
using namespace std;
typedef long long ll;
const int mod=1e9+7;
template<typename T>void write(T x)
{
if(x<0)
{
putchar('-');
x=-x;
}
if(x>9)
{
write(x/10);
}
putchar(x%10+'0');
}
template<typename T> void read(T &x)
{
x = 0;char ch = getchar();ll f = 1;
while(!isdigit(ch)){if(ch == '-')f*=-1;ch=getchar();}
while(isdigit(ch)){x = x*10+ch-48;ch=getchar();}x*=f;
}
ll gcd(ll a,ll b){return b==0?a:gcd(b,a%b);}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;};
ll ksm(ll a,ll n){//看是否要mod
ll ans=1;
while(n){
if(n&1) ans=(ans*a)%mod;
a=a*a%mod;
n>>=1;
}
return ans%mod;
}
//==============================================================
char mp[105][1005];
int x,y;
int t,n,m;
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
clock_t c1 = clock();
//===========================================================
read(t);
while(t--){
read(n),read(m),read(x),read(y);
rep(i,1,n){
rep(j,1,m){
cin>>mp[i][j];
}
}
if(2*x<=y){
int cnt=0;
rep(i,1,n){
rep(j,1,m){
if(mp[i][j]=='.'){
cnt++;
}
}
}
write(cnt*x);
}
else{
int cnt1=0,cnt2=0;
rep(i,1,n){
rep(j,1,m){
if(mp[i][j]=='.'){
if(j+1<=m&&mp[i][j+1]=='.'){
cnt2++;
mp[i][j]=mp[i][j+1]='*';
}
else{
cnt1++;
mp[i][j]='*';
}
}
}
}
write(cnt1*x+cnt2*y);
}
putchar('\n');
}
//===========================================================
std::cerr << "Time:" << clock() - c1 << "ms" << std::endl;
return 0;
}
首先,分情况:
若加入偶数杯,通过列式可知,温度和加入的杯数无关,所以,我们把偶数的情况单独拎出来比较。
对于奇数杯,我们可以写出通式: t n o w = ( ( c + h ) ∗ x − c ) / ( 2 ∗ x − 1 ) t_{now}=((c+h)*x-c)/(2*x-1) tnow=((c+h)∗x−c)/(2∗x−1),我们求导之后,发现这个函数是单调减的,所以,这里我们可以选择二分。但是,我们也可以再推一下。假设 t n o w = ( ( c + h ) ∗ x − c ) / ( 2 ∗ x − 1 ) = = t t_{now}=((c+h)*x-c)/(2*x-1)==t tnow=((c+h)∗x−c)/(2∗x−1)==t,我们可以算出: x = ( c − t ) / ( c + h − 2 ∗ t ) x=(c-t)/(c+h-2*t) x=(c−t)/(c+h−2∗t),此时,加入杯数为 a n s = ( c − h ) / ( c + h − 2 ∗ t ) ans=(c-h)/(c+h-2*t) ans=(c−h)/(c+h−2∗t),但是,因为这不是精确值,所以,我们需要左右枚举一番。
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define inf 0x3f3f3f3f
#define IOS ios_base::sync_with_stdio(0); cin.tie(0);
#define rep(i, a, n) for(register int i = a; i <= n; ++ i)
#define per(i, a, n) for(register int i = n; i >= a; -- i)
#define ONLINE_JUDGE
using namespace std;
typedef long long ll;
const int mod=1e9+7;
template<typename T>void write(T x)
{
if(x<0)
{
putchar('-');
x=-x;
}
if(x>9)
{
write(x/10);
}
putchar(x%10+'0');
}
template<typename T> void read(T &x)
{
x = 0;char ch = getchar();ll f = 1;
while(!isdigit(ch)){if(ch == '-')f*=-1;ch=getchar();}
while(isdigit(ch)){x = x*10+ch-48;ch=getchar();}x*=f;
}
ll gcd(ll a,ll b){return b==0?a:gcd(b,a%b);}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;};
ll ksm(ll a,ll n){//看是否要mod
ll ans=1;
while(n){
if(n&1) ans=(ans*a)%mod;
a=a*a%mod;
n>>=1;
}
return ans%mod;
}
//==============================================================
int T;
ll c,h,t;
double cal(ll x){
int t1=x/2,t2=x-t1;
return (1.0*t2*h+1.0*t1*c)/x;
}
#define eps 1e-9
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
clock_t c1 = clock();
//===========================================================
read(T);
while(T--){
read(h),read(c),read(t);
if(h==t) write(1);
else{
int ans=(double)((1.0*c-h)/(1.0*c+h-2*t));
double xx=fabs(cal(ans)-t);
if(ans<=0){
write(2);
putchar('\n');
continue;
}
int l=ans-5,r=ans+5;
rep(i,l,r){
if(i<=0) continue;
if(fabs(cal(i)-t)<xx){
ans=i;
xx=fabs(cal(i)-t);
}
}
if(fabs(cal(2)-t)<fabs(cal(ans)-t)) ans=2;
write(ans);
}
putchar('\n');
}
//===========================================================
std::cerr << "Time:" << clock() - c1 << "ms" << std::endl;
return 0;
}
好像正解是dp。。但是,看到有大佬写了个st表+单调栈的方法,感觉更好理解。因为它只抽掉中间的一个数,所以,我们可以枚举这个数。很容易可以想到,我们抽调的这个数,是为了让结果尽量小,那么,就应该是这个区间内最大的数,所以,我们可以利用单调栈求出这个数左右两侧的沿着这个数到两端方向的最大单调递减区间,并记录下区间的边界,就可以得到每个数对应的区间。
之后,我们再用st表,去维护一个前缀和。
在查询的时候,我们就查询st表中,在右侧递减区间内的最大前缀和和左侧递减区间中的最小前缀和,两个值的差就是删除这个数的对应区间内能得到的最大子段和,减掉要删去的数,就是所得值。我们只要枚举中间值就行了。
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define inf 0x3f3f3f3f
#define IOS ios_base::sync_with_stdio(0); cin.tie(0);
#define rep(i, a, n) for(register ll i = a; i <= n; ++ i)
#define per(i, a, n) for(register ll i = n; i >= a; -- i)
#define ONLINE_JUDGE
using namespace std;
typedef long long ll;
const int mod=1e9+7;
template<typename T>void write(T x)
{
if(x<0)
{
putchar('-');
x=-x;
}
if(x>9)
{
write(x/10);
}
putchar(x%10+'0');
}
template<typename T> void read(T &x)
{
x = 0;char ch = getchar();ll f = 1;
while(!isdigit(ch)){if(ch == '-')f*=-1;ch=getchar();}
while(isdigit(ch)){x = x*10+ch-48;ch=getchar();}x*=f;
}
ll gcd(ll a,ll b){return b==0?a:gcd(b,a%b);}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;};
ll ksm(ll a,ll n){//看是否要mod
ll ans=1;
while(n){
if(n&1) ans=(ans*a)%mod;
a=a*a%mod;
n>>=1;
}
return ans%mod;
}
//==============================================================
#define re register
const int maxn=1e5+10;
ll a[maxn];
ll s[maxn];
ll st_min[maxn][20];
ll st_max[maxn][20];
ll l[maxn],r[maxn];
ll lg[maxn];
ll n;
void build(){//st表维护前缀和,记录在每个区间内的最大/最小前缀和
rep(i,1,n){
lg[i]=lg[i/2]+1;
}
rep(i,1,n) st_min[i][0]=s[i],st_max[i][0]=s[i];
for(re int j=1;j<=25;j++){
for(re ll i=0;i+(1<<j)-1<=n;i++){
st_max[i][j]=max(st_max[i][j-1],st_max[i+(1<<(j-1))][j-1]);
st_min[i][j]=min(st_min[i][j-1],st_min[i+(1<<(j-1))][j-1]);
}
}
}
//查询操作
ll query_max(ll l,ll r){
ll k =lg[r-l+1]-1;
return max(st_max[l][k],st_max[r-(1<<k)+1][k]);
}
ll query_min(ll l,ll r){
ll k=lg[r-l+1]-1;
return min(st_min[l][k],st_min[r-(1<<k)+1][k]);
}
void get_edge(){
ll st[maxn]={0};//手写栈
ll top=0;
rep(i,1,n){
while(top&&a[st[top]]<=a[i]){//目前栈顶对应的值比他小,说明还能拓展,继续pop
top--;
}
l[i]=st[top];
st[++top]=i;
}
top=0;
st[0]=n+1;
per(i,1,n){
while(top&&a[i]>=a[st[top]]){//同上
top--;
}
r[i]=st[top];
st[++top]=i;
}
rep(i,1,n) l[i]++,r[i]--;
//因为上面边界分别是从0和n+1开始的,而且每次记录的是比他小一或大一单位的数,所以,需要缩小一下范围。
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
clock_t c1 = clock();
//===========================================================
read(n);
rep(i,1,n) read(a[i]);
rep(i,1,n) s[i]=s[i-1]+a[i];//求前缀和
build();
get_edge();
ll ans=-inf;
rep(i,1,n){//暴力查询
ll tmp=query_max(i,r[i])-query_min(l[i]-1,i-1);
ans=max(ans,tmp-a[i]);
}
write(ans);
//===========================================================
std::cerr << "Time:" << clock() - c1 << "ms" << std::endl;
return 0;
}
想不明白什么时候才满足题目给的式子,大佬给了我答案:当每个除数中间存在倍数关系的时候。
证明:还是交给大佬吧。。
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define inf 0x3f3f3f3f
#define IOS ios_base::sync_with_stdio(0); cin.tie(0);
#define rep(i, a, n) for(register ll i = a; i <= n; ++ i)
#define per(i, a, n) for(register ll i = n; i >= a; -- i)
#define ONLINE_JUDGE
using namespace std;
typedef long long ll;
const ll mod=998244353;
template<typename T>void write(T x)
{
if(x<0)
{
putchar('-');
x=-x;
}
if(x>9)
{
write(x/10);
}
putchar(x%10+'0');
}
template<typename T> void read(T &x)
{
x = 0;char ch = getchar();ll f = 1;
while(!isdigit(ch)){if(ch == '-')f*=-1;ch=getchar();}
while(isdigit(ch)){x = x*10+ch-48;ch=getchar();}x*=f;
}
ll gcd(ll a,ll b){return b==0?a:gcd(b,a%b);}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;};
ll ksm(ll a,ll n){//看是否要mod
ll ans=1;
while(n){
if(n&1) ans=(ans*a)%mod;
a=a*a%mod;
n>>=1;
}
return ans%mod;
}
//==============================================================
const int maxn=5e5+10;
ll fac[maxn];
ll inv[maxn];
ll n,k;
void init(){
fac[0]=1;
rep(i,1,maxn+1){
fac[i]=(fac[i-1]*i)%mod;
}
inv[(int)(5e5)]=ksm((int)(fac[(int)5e5]),mod-2)%mod;
for(int i=5e5-1;i>=0;--i){
inv[i]=inv[i+1]*(i+1)%mod;
}
}
ll C(ll n,ll m){
if(n<m) return 0;
return ((fac[n]*inv[n-m])%mod*inv[m])%mod;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
clock_t c1 = clock();
//===========================================================
init();
read(n),read(k);
ll ans=0;
rep(i,1,n){
ans=(ans+C(n/i-1,k-1)%mod)%mod;
}
write(ans);
//===========================================================
std::cerr << "Time:" << clock() - c1 << "ms" << std::endl;
return 0;
}