8.18赛后总结
Chinese_Cant_Fly · · 个人记录
:::::info[C]
::::error[错误思路]
根据题目意思创建一个小根堆,将每一个数字放入堆中,当这个小根堆非空时,就从中每次都拿出两个当前的最小值,再按照题目的意思进行判断,如果1。
::::
::::error[错误代码]
#include<bits/stdc++.h>
#define int unsigned long long
using namespace std;
priority_queue<int,vector<int>,greater<int> >q;
int n,m;
signed main(){
cin>>n>>m;
for(int i=1;i<=n;i++){
int a;
cin>>a;
q.push(a);
}
int ans=0;
for(int i=1;i<n;i++){
int x=q.top();
q.pop();
int y=q.top();
q.pop();
if(m*x>=y&&m*y>=x) ans++;
q.push(x+y);
}
cout<<ans;
return 0;
}
:::: ::::success[正确思路] 注意到数据范围
2 \le m \le 10$,$0 \le a_i < 2^{64}
所以,我们需要开一个 unsigned long long 同时,在计算过程中,我们会发现在计算
值得注意的是,0,并且 0,并且 1。 |
|---|
| ::::success[正确代码] |
| ```cpp line-numbers lines=19-22 |
| #include<bits/stdc++.h> |
| #define int unsigned long long |
| using namespace std; |
| priority_queue<int,vector<int>,greater<int> >q; |
| int n,m; |
| signed main(){ |
| cin>>n>>m; |
| for(int i=1;i<=n;i++){ |
| int a; |
| cin>>a; |
| q.push(a); |
| } |
| int ans=0; |
| while(q.size()>1){ |
| int x=q.top(); |
| q.pop(); |
| int y=q.top(); |
| q.pop(); |
| int f1=0,f2=0;//主要修改部分,f1和f2表示是否成立 |
| if(y%m==0&&x>=y/m |
| if(x%m==0&&y>=x/m |
| if(f1==1&&f2==1) ans++;//如果mx>=y且my>=x成立,积分增加 |
| q.push(x+y); |
| } |
| cout<<ans; |
| return 0; |
| } |
::::
:::::
:::::info[D]
::::error[错误思路]
这道题一开始并没有什么思路,于是便直接将样例输出了,拿了0分。
::::
::::error[错误代码]
```cpp line-numbers
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,m;
int v[200005],a[200005],b[200005],c[200005],d[200005];
signed main(){
cin>>n>>m;
for(int i=1;i<=n;i++){
cin>>v[i];
}
for(int i=1;i<=m;i++){
cin>>a[i]>>b[i]>>c[i]>>d[i];
}
cout<<2<<"\n"<<-1<<"\n"<<-1<<"\n"<<1<<"\n"<<-1<<"\n"<<0;
return 0;
}
::::
::::success[正确思路]
考虑使用优先队列来实现这道题目,通过使用带结构体的优先队列,在结构体中存入三个数据,分别为 -1。
::::
::::success[正确代码]
#include<bits/stdc++.h>
#define int long long
using namespace std;
struct qwq{
int t,x,id;
bool operator < (const qwq& a) const
{
return t>a.t;
}
};
int n,m;
int v[200005],a[200005],b[200005],c[200005],d[200005];
priority_queue<qwq>q;
signed main(){
cin>>n>>m;
for(int i=1;i<=n;i++){
cin>>v[i];
}
for(int i=1;i<=m;i++){
cin>>a[i]>>b[i]>>c[i]>>d[i];
while(q.size()){
qwq now=q.top();
if(now.t>a[i])break;
q.pop();
v[now.id]+=now.x;
}
if(v[b[i]]>=d[i]){
q.push({a[i]+c[i],d[i],b[i]});
cout<<v[b[i]]-d[i]<<"\n";
v[b[i]]-=d[i];
}
else{
cout<<-1<<"\n";
}
}
return 0;
}
| :::: |
|---|