<<6.9-401-apple题>>总结
_starry_night · · 个人记录
<<6.9-401-apple题>>总结
| 题目 | 正整数 | 划船计划 | 数组操作 | 经典多米诺 | 热量计算 |
|---|---|---|---|---|---|
| 预计分数 | 50 | 50 | 100 | 10 | 0 |
| 实际分数 | 65 | 60 | 100 | 0 | 4 |
第一题:正整数
思路:
1.输入
2.令m=n/c
3.特判(1):m=0 ->无解
4.特判(2):m=1 ->n=c 只有一个答案->c*c
5.运用推出a 与(a-1 )这一条件得到结果->c*c*m*(m-1)
时间复杂度:O(1)
代码:
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
const int N=5e6+25;
int c,n,o=1;
int maxx=-1;
int vis[N];
bool f[N];
signed main()
{
// freopen("t1.in","r",stdin);
// freopen("t1.out","w",stdout);
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>c>>n;
n/=c;
if(n==0)
{
cout<<-1;
return 0;
}
if(n==1)
{
cout<<c*c;
return 0;
}
cout<<c*c*n*(n-1);
return 0;
}
第二题:划船计划
思路:
1.运用桶记录每种体重出现的次数(vis[a_i]+1 )
2.枚举总质量(s )与其中一个个体质量(w )
3.进行判断(x>y or x<y or x=y )
4.计数输出
时间复杂度:O(200*100*T)
代码:
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
const int N=1e5+25;
int vis[N];
int t,n;
int a[N];
signed main()
{
// freopen("t2.in","r",stdin);
// freopen("t2.out","w",stdout);
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>t;
while(t--)
{
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i];
vis[a[i]]++;
}
int maxx=0;
for(int w=2;w<=200;w++)
{
int cnt=0;
for(int i=1;i<=100;i++)
{
if(w-i>=1)
{
int x=i;
int y=w-i;
if(x>y)
{
continue;
}
if(x==y)
{
cnt+=vis[x]/2;
}
else
{
cnt+=min(vis[x],vis[y]);
}
}
}
maxx=max(maxx,cnt);
}
cout<<maxx<<endl;
for(int i=1;i<=100;i++)
{
vis[i]=0;
}
}
return 0;
}
第三题:数组操作
思路:直接套退化版dij
时间复杂度:O(log(n*n))
代码:
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
const int N=1e5+25;
int t,n,dp[N],minn;
void dfs(int u,int sum,int pre){
if(sum-1>minn)return ;
if(u==1){
minn=min(sum-1,minn);
return ;
}
for(int i=pre;i<=87&&dp[i]<=u;i++){
if(u%dp[i]==0){
dfs(u/dp[i],sum+i+1,i);
}
}
}
signed main()
{
// freopen("t4.in","r",stdin);
// freopen("t4.out","w",stdout);
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>t;
dp[1]=1;
dp[2]=2;
for(int i=3;i<=87;i++)
{
dp[i]=dp[i-1]+dp[i-2];
}
while(t--)
{
minn=1e16;
cin>>n;
if(n==1)
{
cout<<1<<endl;
continue;
}
dfs(n,0,2);
if(minn==1e16)
{
cout<<0<<endl;
}
else
{
cout<<minn<<endl;
}
}
return 0;
}
第四题:经典多米诺
思路:
经过一段推理(枚举一些方案如横摆障碍,斜摆障碍和竖摆障碍)可得,方案数=1区块方案数*2区块方案数*3区块方案数*......*k区块方案数 ,而区块方案数经枚举可发现是肥波纳期数(斐波那契数),因此仅需枚举+达表+深搜+剪枝即可得到答案
时间复杂度:O(T*log(n))
代码:
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
struct node{
int x,cost;
};
int n,p,x,qp,y,sum,minn=1e18;
map<int,int>mp;
queue<node>q;
signed main()
{
// freopen("t3.in","r",stdin);
// freopen("t3.out","w",stdout);
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>p>>x>>qp>>y;
int dx[15]={x,-y};
int dy[15]={p,qp};
for(int i=1;i<=n;i++)
{
int sss;
cin>>sss;
sum+=sss;
sum%=n;
}
q.push({sum,0});
mp[sum]=-10;
while(q.size()){
node f=q.front();
q.pop();
if(f.cost>=minn)continue;
if(f.x==0){
minn=min(minn,f.cost);
continue;
}
for(int i=0;i<2;i++){
int xx=f.x+dx[i];
int cc=f.cost+dy[i];
if(xx<0)xx=n+xx;
if(xx>=n)xx=xx-n;
if(xx==0){
minn=min(minn,cc);
continue;
}
if((mp[xx]==0||mp[xx]>cc)&&cc<=minn){
mp[xx]=cc;
q.push({xx,cc});
}
}
}
if(minn==1e18)
{
cout<<-1;
}
else
{
cout<<minn;
}
return 0;
}