目录
Array Splitting(差分+贪心)
题意:
给你a数组,有n个数,已经按由小到大的顺序排列。将数组分成k段,使最小,输出该值。
思路:
1 2 3 4 5 6
a[i]: 4 8 15 16 23 42 //原数组
c[i]: 4 7 1 7 19 //差分数组
1.分成k段,每一段的(max(i)-min(i))都等于该区间的差分和。
例:将a[3],a[4],a[5]划为一段,则23-15=8等价于c[3]+c[4]=1+7=8。
2.求最小,则保留差值尽量小的,将差值大的让其自己一组(自己一组的最大值-最小值==0)。所以对C数组排序。
3. 最大也就是分成一段,a[n]-a[1];
现在要分成k段,将C数组中最大的k-1个减去,让它们一个元素一段。
sum=a[n]-a[1];
for(int i=n-1;i>n-k;i--)
sum-=c[i];
AC代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxx=3e5+10;
int a[maxx];
int c[maxx];
int main()
{
int n,k;
cin>>n>>k;
for(int i=1; i<=n; i++)
cin>>a[i];
if(k==1)
{
cout<<a[n]-a[1]<<endl;
}
else
{
for(int i=2; i<=n; i++)
{
c[i-1]=a[i]-a[i-1];
}
sort(c+1,c+n);
int sum=a[n]-a[1];
for(int i=n-1;i>n-k;i--)
sum-=c[i];
cout<<sum<<endl;
}
}
Selfish Grazing(贪心)
题意:
给出n个线段的左右端点(l和r),输出n个线段里面最多的不相交的线段数量。
思路:
对每条线段的右端点排序,小的在前,大的在后。遍历数组如果当前线段的左端点大于等于前一个线段的右端点即可。最后多条线段左右端点形成的序列是一个上升的序列。
AC代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxx=50005;
struct node
{
int l,r;
} a[50005];
bool cmp(node x,node y)
{
return x.r<y.r;
}
int main()
{
int n;
cin>>n;
for(int i=0; i<n; i++)
{
cin>>a[i].l>>a[i].r;
}
sort(a,a+n,cmp);
int t=a[0].r;
int ans=1;
for(int i=1;i<n;i++)
{
if(a[i].l>=t)
{
t=a[i].r;
ans++;
}
}
cout<<ans<<endl;
}
导弹拦截(贪心)
题意:
样例输入:
0 0 10 0
2
-3 3
10 0
样例输出:
18
样例解释:
思路:
先将每个导弹与第一个系统的距离的平方算出来 ,再对距离的平方进行从大到小排序 ,从1开始枚举每个导弹由二系统拦截的情况,每次更新二系统的最大半径和答案的最小值。
AC代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxx=100005;
int a[maxx];
struct node
{
int x,y,dis;
}p[maxx];
bool cmp(node aa,node bb)
{
return aa.dis>bb.dis;
}
int main()
{
int x1,y1,x2,y2;
int n;
cin>>x1>>y1>>x2>>y2;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>p[i].x>>p[i].y;
p[i].dis=(x1-p[i].x)*(x1-p[i].x)+(y1-p[i].y)*(y1-p[i].y);
}
sort(p,p+n,cmp);
ll r1=p[0].dis,r2=0;
ll ans=r1+r2;
ll tt;
for(int i=1;i<n;i++)
{
tt=(x2-p[i-1].x)*(x2-p[i-1].x)+(y2-p[i-1].y)*(y2-p[i-1].y);
r2=max(r2,tt);
r1=p[i].dis;
ans=min(ans,r1+r2);
}
cout<<ans<<endl;
}
操作系统(优先队列+贪心)
题意:
思路:
1.优先队列priority_queue<node>q;按照题意,优先级优先,一样则按照到达时间排序。
bool operator < (const node x,const node y)
{
if(x.f!=y.f)
return x.f<y.f;
else
return x.da>y.da;
}
2.用time记录当前时间,一直更新time。
a,b,c,d是新进入的节点信息,当前节点now
if(b>=time+now.run)//新进入的节点到达晚,当前节点可以执行完
{
time+=now.run;
cout<<now.id<<" "<<time<<endl;
}
else//当前节点没有执行完,重新放入队列,如果它的优先级高,下一次开始执行时还是它
{
now.run=now.run-(b-time);
q.push(now);
break;
}
3.不断入队列出队列直至队列为空。
AC代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
struct node
{
ll id,da,run,f;
};
bool operator < (const node x,const node y)
{
if(x.f!=y.f)
return x.f<y.f;
else
return x.da>y.da;
}
int main()
{
priority_queue<node>q;
ll a,b,c,d,time=0;
while(cin>>a>>b>>c>>d)
{
while(!q.empty())
{
node now=q.top();
q.pop();
if(b>=time+now.run)
{
time+=now.run;
cout<<now.id<<" "<<time<<endl;
}
else
{
now.run=now.run-(b-time);
q.push(now);
break;
}
}
node tmp;
tmp.id=a;tmp.da=b;tmp.run=c;tmp.f=d;
q.push(tmp);
time=b;
}
while(!q.empty())
{
node now=q.top();
q.pop();
time+=now.run;
cout<<now.id<<" "<<time<<endl;
}
}
Brownie Slicing(二分答案+矩阵前缀和)
题意:
n<=500 * m<=500的方阵,先沿横坐标切A-1刀,再把每一块切B-1刀,得到A*B块,求这A*B块的数字之和的最小值的最大值。
思路:
1.最小值最大化=====二分答案
2.check函数:横坐标切A-1刀,纵坐标切B-1刀互不影响。
(1)先考虑把第一行分成 ≥ x 的 b 块。如果不可以,那么就加上第二行再分,一直叠加直到可以分出这样的 b 块为止,假设叠加到了第 p 行,那么[1,p] 就作为横向切的第一刀(即切在 p 行处),然后再对 p+1 行进行同样的操作。最后判断能否切到 a 刀即可。
(2)把若干行分成 ≥ x 的 b块,利用矩阵前缀和。
s=sum[i][j]-sum[fa-1][j]-sum[i][fb-1]+sum[fa-1][fb-1];
AC代码:
#include<bits/stdc++.h>
using namespace std;
const int maxx=15100;
typedef long long ll;
int a[505][505];
int sum[505][505];
int R,C,A,B;
bool check(int x)
{
int fa=1,fb=1;
int cnta=0,cntb=0;
int s;
for(int i=1;i<=R;i++)
{
fb=1;
cntb=0;
for(int j=1;j<=C;j++)
{
s=sum[i][j]-sum[fa-1][j]-sum[i][fb-1]+sum[fa-1][fb-1];
if(s>=x)
{
fb=j+1;
cntb++;
}
}
if(cntb>=B)
{
fa=i+1;
cnta++;
}
}
if(cnta>=A)
return true;
return false;
}
int main()
{
int l=4005,r=0,mid;
cin>>R>>C>>A>>B;
memset(sum,0,sizeof(sum));
int tt;
for(int i=1;i<=R;i++)
{
tt=0;
for(int j=1;j<=C;j++)
{
cin>>a[i][j];
r+=a[i][j];
tt+=a[i][j];
sum[i][j]=sum[i-1][j]+tt;
l=min(l,a[i][j]);
}
}
int ans=0;
while(l<=r)
{
mid=(l+r)/2;
if(check(mid))
{
ans=mid;
l=mid+1;
}
else
{
r=mid-1;
}
}
cout<<ans<<endl;
}