T1:
详见题解。
代码:
#include <bits/stdc++.h>
using namespace std;
const int Max=2010;
int n,m;
int a[Max],f[Max];
inline int get_int()
{
int x=0,f=1;
char c;
for(c=getchar();(!isdigit(c))&&(c!='-');c=getchar());
if(c=='-') f=-1,c=getchar();
for(;isdigit(c);c=getchar()) x=(x<<3)+(x<<1)+c-'0';
return x*f;
}
inline int mn(int a,int b){return a>b?a:b;}
inline void init()
{
n=get_int(),m=get_int();
for(int i=0;i<n;i++) a[i]=get_int();
}
inline bool check(int mid)
{
for(int i=n-1;~i;i--)
{
f[i]=n-i-1;
for(int j=i+1;j<n;j++)
if(abs(a[j]-a[i])<=(j-i)*mid) f[i]=min(f[i],f[j]+j-i-1);
if(f[i]+i<=m) return true;
}
return false;
}
inline void solve()
{
int l=0,r=2e9,mid;
while(l<r)
{
mid=(l+r)>>1;
if(check(mid)) r=mid;
else l=mid+1;
}
cout<<r;
}
int main()
{
init();
solve();
return 0;
}
T2:
详见题解。
代码:
#include <bits/stdc++.h>
#include <tr1/unordered_map>
using namespace std;
const int Max=500005;
int n,q;
int a[Max],nxt[Max];
tr1::unordered_map<int,long long>sum;
inline int get_int()
{
int x=0;char c;
for(c=getchar();!isdigit(c);c=getchar());
for(;isdigit(c);c=getchar()) x=(x<<3)+(x<<1)+c-'0';
return x;
}
inline void print(long long x)
{
if(x>9) print(x/10);
putchar('0'+x%10);
}
int main()
{
n=get_int(),q=get_int();
for(int i=1;i<=n;i++) a[i]=get_int(),nxt[i]=i+1;
for(int i=n;i;i--)
{
int pre=i;
for(int j=i;j<=n;j=nxt[j])
{
a[j]=__gcd(a[pre],a[j]);
if(a[j]==a[pre]) nxt[pre]=nxt[j];
sum[a[j]]+=(nxt[j]-j);
pre=j;
}
}
while(q--) print(sum[get_int()]),putchar(' ');
return 0;
}
T3:
详见题解
//created by dzyo
#include <bits/stdc++.h>
using namespace std;
const int RLEN=1<<18|1;
inline char nc() {
static char ibuf[RLEN],*ib,*ob;
(ib==ob) && (ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
return (ib==ob) ? -1 : *ib++;
}
inline int rd() {
char ch=nc(); int i=0,f=1;
while(!isdigit(ch)) {if(ch=='-')f=-1; ch=nc();}
while(isdigit(ch)) {i=(i<<1)+(i<<3)+ch-'0'; ch=nc();}
return i*f;
}
inline void W(int x) {
static int buf[50];
if(!x) {putchar('0'); return;}
if(x<0) {putchar('-'); x=-x;}
while(x) {buf[++buf[0]]=x%10; x/=10;}
while(buf[0]) putchar(buf[buf[0]--]+'0');
}
const int N=1e5+50;
int n,k,h[N],mn[N],ans[N];
struct fenwick_tree {
vector <int> all,bit;
inline void init() {
sort(all.begin(),all.end());
all.erase(unique(all.begin(),all.end()),all.end());
bit.assign(all.size()+1,0);
}
inline void inc(int x) {
x=upper_bound(all.begin(),all.end(),x)-all.begin();
for(;x;x-=x&(-x)) bit[x]++;
}
inline int ask(int x,int rs=0) {
x=lower_bound(all.begin(),all.end(),x)-all.begin()+1;
for(;x<=all.size();x+=x&(-x)) rs+=bit[x];
return rs;
}
} bit;
struct node {
int pos,val;
node(int pos,int val) : pos(pos), val(val) {}
friend inline bool operator <(const node &a,const node &b) {return a.val<b.val;}
};
inline void solve(int l,int r) {
if(l==r) {++ans[l]; return;}
int mid=(l+r)>>1;
mn[mid]=h[mid];
for(int i=mid-1;i>=l;i--) mn[i]=min(mn[i+1],h[i]);
mn[mid+1]=h[mid+1];
for(int i=mid+2;i<=r;i++) mn[i]=min(mn[i-1],h[i]);
vector <node> vec;
for(int i=l;i<=r;i++) vec.push_back(node(i,mn[i]));
sort(vec.begin(),vec.end());
bit.all.clear();
for(int i=l;i<=mid;i++) bit.all.push_back(k-(h[i]-i-2*mn[i]));
bit.init();
for(int i=0;i<vec.size();i++) {
node &t=vec[i];
if(t.pos<=mid) bit.inc(k-(h[t.pos]-t.pos-2*mn[t.pos]));
else ans[t.pos]+=bit.ask(h[t.pos]+t.pos);
} //h[j] + j <= k - (h[i] - i - 2 * mn[i])
bit.all.clear();
for(int i=l;i<=mid;i++) bit.all.push_back(k-(h[i]-i));
bit.init();
for(int i=vec.size()-1;~i;i--) {
node &t=vec[i];
if(t.pos<=mid) bit.inc(k-(h[t.pos]-t.pos));
else ans[t.pos]+=bit.ask(h[t.pos]+t.pos-2*mn[t.pos]);
} //h[j] + j - 2 * mn[j] <= k - (h[i] - i)
bit.all.clear();
for(int i=mid+1;i<=r;i++) bit.all.push_back(k-(h[i]+i-2*mn[i]));
bit.init();
for(int i=0;i<vec.size();i++) {
node &t=vec[i];
if(t.pos>mid) bit.inc(k-(h[t.pos]+t.pos-2*mn[t.pos]));
else ans[t.pos]+=bit.ask(h[t.pos]-t.pos);
} //h[i] - i <= k - (h[j] + j - 2 * mn[j])
bit.all.clear();
for(int i=mid+1;i<=r;i++) bit.all.push_back(k-(h[i]+i));
bit.init();
for(int i=vec.size()-1;~i;i--) {
node &t=vec[i];
if(t.pos>mid) bit.inc(k-(h[t.pos]+t.pos));
else ans[t.pos]+=bit.ask(h[t.pos]-t.pos-2*mn[t.pos]);
} //h[i] - i - 2 * mn[i] <= k - (h[j] + j)
solve(l,mid); solve(mid+1,r);
}
int main() {
n=rd(), k=rd();
for(int i=1;i<=n;i++) h[i]=rd();
solve(1,n);
for(int i=1;i<=n;i++) W(ans[i]), putchar(' ');
}