A
思路
经典问题。
dp[i]:第i个位置结尾的lis长度。暴力n^2转移。
/* Author : Rshs */
#include<iostream>
using namespace std;
const int MXN = 1e6+5;
int a[MXN];
int dp[MXN];
int main(){
int n;cin>>n;
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
int ans=0;
for(int i=1;i<=n;i++){
dp[i]=1;
for(int j=1;j<i;j++) if(a[i]>a[j]) dp[i]=max(dp[i],dp[j]+1);
}
for(int i=1;i<=n;i++)ans=max(ans,dp[i]);
cout<<ans;
return 0;
}
B
思路:
dp[i]:到第i个加油站的最快时间。暴力n^2转移。
/* Author : Rshs */
#include<bits/stdc++.h>
using namespace std;
#define FI first
#define SE second
#define LL long long
#define LDB long double
#define MP make_pair
#define PII pair<int,int>
#define SZ(a) (int)a.size()
const LDB pai = acos(-1.0L);
const LDB eps = 1e-10;
const LL mod = 1e9+7;
const int MXN = 1e6+5;
int p[MXN];
double dp[MXN];
int main(){
int L;
while(cin>>L){
int n,c,t;cin>>n>>c>>t;
int sa,sb,sc;cin>>sa>>sb>>sc;
double vr=sa,vt1=sb,vt2=sc;
for(int i=1;i<=n;i++) scanf("%d",&p[i]);
n++;p[n]=L;
double rabbit_time=1.0*L/(1.0*vr);
for(int i=1;i<=n;i++) dp[i]=1e10;
dp[0]=0;p[0]=0;
for(int i=1;i<=n;i++){
for(int j=0;j<i;j++){
double dis=p[i]-p[j];
dp[i]=min(dp[i],dp[j]+dis/vt2);
if(dis<=c){
dp[i]=min(dp[i],dp[j]+dis/vt1+t*(j==0?0.0:1.0));
}
else {
dp[i]=min(dp[i],dp[j]+1.0*c/vt1+t*(j==0?0.0:1.0)+(dis-1.0*c)/vt2);
}
}
}
if(dp[n]<rabbit_time){
puts("What a pity rabbit!");
}
else {
puts("Good job,rabbit!");
}
}
return 0;
}
C
思路:
简单数学。
/* Author : Rshs */
#include<bits/stdc++.h>
using namespace std;
#define FI first
#define SE second
#define LL long long
#define LDB long double
#define MP make_pair
#define PII pair<int,int>
#define SZ(a) (int)a.size()
const LDB pai = acos(-1.0L);
const LDB eps = 1e-10;
const LL mod = 1e9+7;
const int MXN = 1e6+5;
void Main(int avg){
LL a,b,k;cin>>a>>b>>k;
cout<<a*(k/2+(k%2==1))-b*(k/2)<<'\n';
}
int main(){
int cas;cin>>cas;for(int i=1;i<=cas;i++)Main(i);
return 0;
}
D
思路
贪心关右边亮着的灯。
/* Author : Rshs */
#include<bits/stdc++.h>
using namespace std;
#define FI first
#define SE second
#define LL long long
#define LDB long double
#define MP make_pair
#define PII pair<int,int>
#define SZ(a) (int)a.size()
const LDB pai = acos(-1.0L);
const LDB eps = 1e-10;
const LL mod = 1e9+7;
const int MXN = 1e6+5;
int a[MXN];
int main(){
int n;cin>>n;
for(int i=1;i<=n;i++)cin>>a[i];
int c=0;
for(int i=1;i<=n-2;i++){
if(a[i]==1&&a[i+1]==0&&a[i+2]==1) c++,a[i+2]=0;
}
cout<<c<<'\n';
return 0;
}
E
思路
遍历n个数。
n个数总和-最大的-当前的==最大的 即是满足条件的。
/* Author : Rshs */
#include<bits/stdc++.h>
using namespace std;
#define FI first
#define SE second
#define LL long long
#define LDB long double
#define MP make_pair
#define PII pair<int,int>
#define SZ(a) (int)a.size()
const LDB pai = acos(-1.0L);
const LDB eps = 1e-10;
const LL mod = 1e9+7;
const int MXN = 1e6+5;
PII a[MXN];
int main(){
int n;cin>>n;
for(int i=1;i<=n;i++)cin>>a[i].FI,a[i].SE=i;
sort(a+1,a+1+n);
LL s=0;
for(int i=1;i<=n;i++)s=s+(LL)a[i].FI;
vector<int>v;
if(s-a[n].FI-a[n-1].FI==a[n-1].FI) v.push_back(a[n].SE);
for(int i=1;i<n;i++){
if(s-a[i].FI-a[n].FI==a[n].FI) v.push_back(a[i].SE);
}
cout<<SZ(v)<<'\n';
for(auto i:v) cout<<i<<' ';
return 0;
}
F
思路
把每个数的个数放到vector,
二分最多的序列数,然后check能不能凑出来那么多序列数。
注意最后可能会多输出几个数,得pop一下。
/* Author : Rshs */
#include<bits/stdc++.h>
using namespace std;
#define FI first
#define SE second
#define LL long long
#define LDB long double
#define MP make_pair
#define PII pair<int,int>
#define SZ(a) (int)a.size()
const LDB pai = acos(-1.0L);
const LDB eps = 1e-10;
const LL mod = 1e9+7;
const int MXN = 1e6+5;
vector<PII>v;
map<int,int>ma;
int a[MXN];
int n,k;
bool ck(int m){
int cc=0;
for(auto i:v){
int can=i.FI/m;
cc=cc+can;
}
return cc>=k;
}
int main(){
cin>>n>>k;
for(int i=1;i<=n;i++)scanf("%d",&a[i]),ma[a[i]]++;
for(auto i:ma)v.push_back(MP(i.SE,i.FI));
sort(v.begin(),v.end());
reverse(v.begin(),v.end());
int l=1,r=n;
int ans=1;
while(l<=r){
int m=(l+r)/2;
if(ck(m)) ans=m,l=m+1;
else r=m-1;
}
vector<int>aa;
for(auto i:v){
int can=i.FI/ans;
while(can--)aa.push_back(i.SE);
}
while(SZ(aa)>k) aa.pop_back();
for(auto i:aa)cout<<i<<' ';
return 0;
}
G
思路
枚举第一个数是多少,然后二分后面的数。
复杂度是
O
(
n
∗
l
o
g
n
∗
l
o
g
n
)
O(n*logn*logn)
O(n∗logn∗logn)
/* Author : Rshs */
#include<bits/stdc++.h>
using namespace std;
#define FI first
#define SE second
#define LL long long
#define LDB long double
#define MP make_pair
#define PII pair<int,int>
#define SZ(a) (int)a.size()
const LDB pai = acos(-1.0L);
const LDB eps = 1e-10;
const LL mod = 1e9+7;
const int MXN = 1e6+5;
map<int,int>ma;
vector<int>v;
int a[MXN];
int main(){
int n;cin>>n;
for(int i=1;i<=n;i++)scanf("%d",&a[i]),ma[a[i]]++;
for(auto i:ma)v.push_back(i.SE);
sort(v.begin(),v.end());
int ans=0;
for(int i=1;i<=n;i++){
int pos=0,t=0;
for(int j=i;j<=n;j=j*2){
pos=lower_bound(v.begin()+pos,v.end(),j)-v.begin();
if(pos==SZ(v)) break;
t=t+j;
pos++;
}
ans=max(ans,t);
}
cout<<ans<<'\n';
return 0;
}
H
思路
定义
d
p
[
i
]
[
j
]
[
k
]
dp[i][j][k]
dp[i][j][k]:第i个位置,选了j个数,前面空了k个位置。
然后根据情况模拟dp。
/* Author : Rshs */
#include<bits/stdc++.h>
using namespace std;
#define FI first
#define SE second
#define LL long long
#define LDB long double
#define MP make_pair
#define PII pair<int,int>
#define SZ(a) (int)a.size()
const LDB pai = acos(-1.0L);
const LDB eps = 1e-10;
const LL mod = 1e9+7;
const int MXN = 1e6+5;
int a[MXN];
LL dp[205][205][205];
int main(){
int k,x,n;cin>>n>>k>>x;
for(int i=1;i<=n;i++) cin>>a[i];
memset(dp,-1,sizeof(dp));
dp[0][0][0]=0;
for(int i=1;i<=n;i++){
for(int s=0;s<=i;s++){
for(int d=0;d<k;d++){
if(d>=1&&dp[i-1][s][d-1]!=-1)dp[i][s][d]=max(dp[i-1][s][d-1],dp[i][s][d]);//不选
if(s>=1&&dp[i-1][s-1][d]!=-1)dp[i][s][0]=max(dp[i-1][s-1][d]+(LL)a[i],dp[i][s][0]);//选
}
}
}
LL ans=-1;
for(int i=0;i<k;i++) ans=max(ans,dp[n][x][i]);
cout<<ans<<'\n';
return 0;
}
I
定义
d
p
[
i
]
[
j
]
:
dp[i][j]:
dp[i][j]:选了第i个位置,并且是第j个数,的最大值。
注意交换dp顺序。
d
p
[
i
]
[
j
]
=
m
a
x
(
d
p
[
i
−
z
]
[
j
−
1
]
)
(
1
<
=
z
<
=
k
)
dp[i][j]=max(dp[i-z][j-1]) (1<=z<=k)
dp[i][j]=max(dp[i−z][j−1])(1<=z<=k)这一关系可以用数据结构。
线段树T了。
硬就要双端队列动态维护区间最大值(队列内降序,队首为最大值)。
/* Author : Rshs */
#include<bits/stdc++.h>
using namespace std;
#define FI first
#define SE second
#define LL long long
#define LDB long double
#define MP make_pair
#define PII pair<int,int>
#define SZ(a) (int)a.size()
const LDB pai = acos(-1.0L);
const LDB eps = 1e-10;
const LL mod = 1e9+7;
const int MXN = 1e6+5;
LL a[MXN];
LL dp[5005][5005];
int main(){
int k,x,n;cin>>n>>k>>x;
for(int i=1;i<=n;i++) cin>>a[i];
memset(dp,-1,sizeof(dp));
dp[0][0]=0;
for(int j=1;j<=x;j++){
deque<int>q;
if(j==1) q.push_back(0);
for(int i=1;i<=n;i++){
if(q.empty()) dp[i][j]=-1;
else {
dp[i][j]=a[i]+dp[q.front()][j-1];
}
while(!q.empty()&&dp[i][j-1]!=-1&&dp[q.back()][j-1]<=dp[i][j-1])q.pop_back();
if(dp[i][j-1]!=-1)q.push_back(i);
while(!q.empty()&&q.front()<i+1-k)
q.pop_front();
}
}
LL ans=-1;
for(int i=n-k+1;i<=n;i++) ans=max(ans,dp[i][x]);
cout<<ans<<'\n';
return 0;
}