跳到正文

个人记录

4.10总结

liuxiuqi_0203约 10 分钟
原文
文章信息与版本

文章信息

发布
2026年4月15日
收录
2026年4月18日
更新
2026年11月8日
篇幅
约 10 分钟

版本

  1. v2当前
  2. v1

4.10总结

T750436 订单结算

错误代码:

C++
#include<bits/stdc++.h>
#define int double
using namespace std;
map<string,int> mp;
int n,ke;
signed main(){
	cin>>n;
	for(int i=1;i<=n;i++){
	  string str;
	  int h;
	  cin>>str>>h;
	  if(mp.count(str)){
	  	mp[str]+=h;
	  }else{
	  	if(i!=1){
	  	  mp[str]=h;
		}else{
		  mp[str]=h*0.75;
		}
	  }
	}
	for(auto i:mp){
	  cout<<i.first<<' ';
	  cout<<i.second<<'\n';
	}
	return 0;
}

错误点

本题因为本蒟蒻忘了保留小数,加上精度误差,就导致错误。

C++
小数:
cout<<fixed << setprecision(1);

另一个不会用也不常用

还有,本题优惠的是每一个商品第一单每一个商品第一单,不是第一个订单第一个订单。

正确思路:

每一种商品是一个由名称名称和价格价格组成的pair<string,double>pair<string,double>(坑人小数)

因为商品有多个,所以我们需要至多nn个商品组,那么明显mapmap会十分合适

然后我们依次计算每个商品的钱,最后加起来

复杂度:遍历:O(n)O(n),mapmap使用:O(log⁡n)O(\log{n}) 综合:O(nlog⁡n)O(n \log{n})

正确代码:

C++
#include<bits/stdc++.h>
using namespace std;
map<string,double> mp;
int n;
signed main(){
	cin>>n;
	for(int i=1;i<=n;i++){
	  string str;
	  double h;
	  cin>>str>>h;
	  if(mp.count(str)==1){
	  	mp[str]+=h;
	  }else{
	  	  mp[str]=h*0.75;
	  }
	}
	for(auto i:mp){
	  cout<<i.first<<' ';
      cout<<fixed << setprecision(1);
	  cout<<i.second<<'\n';
	}
	return 0;
}

T739897 位运算进阶1

错误代码:

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,q,a[5005];
signed main(){
	cin>>n>>q;
	for(int i=1;i<=n;i++){
	  cin>>a[i];
	}
	for(int i=1;i<=q;i++){
	  int op,x,y;
	  cin>>op>>x>>y;
	  if(op-1){
	  	int z=0;
	  	for(int i=x;i<=y;i++){
	  	  int woof=a[i];
	  	  z=z^woof;
			for(int j=i+1;j<=y;j++){
			  woof=woof^a[j];
			  z=z^woof;
			}
		}
		cout<<z<<'\n';
	  }else{
	  	a[x]=y;
	  }
    }
	return 0;
}

错误原因:

复杂度:O(q×n2)O(q \times n^2), 面对n,q≤5×103n,q\le5 \times 10^3

很明显,打死都过不了

正确思路

如果我们观察样例,会发现出现的次数是一个有规律的变化,经过老师的权威发言,我们得到了一个可爱的公式:(i−x+1)∗(y−i+1)(i-x+1)*(y-i+1)

可复制
C++
int g=(i-x+1)*(y-i+1);

然后因为异或性质(a⊕a=0)(a⊕a=0)所以我们只要判断所得数的奇偶性,然后得到答案

正确代码:

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,q,a[5005];
signed main(){
	cin>>n>>q;
	for(int i=1;i<=n;i++){
	  cin>>a[i];
	}
	for(int i=1;i<=q;i++){
	  int op,x,y;
	  cin>>op>>x>>y;
	  if(op-1){
	  	int z=0;
	  	for(int i=x;i<=y;i++){
          int g=(i-x+1)*(y-i+1);
          if(g%2){
            z^=a[i];
          }
        }
		cout<<z<<'\n';
	  }else{
	  	a[x]=y;
	  }
    }
	return 0;
}

P2058 [NOIP 2016 普及组] 海港

错误代码:

C++
#include<bits/stdc++.h>
using namespace std;
map<int,int> mp;
map<int,vector<int> > m;
queue<int> q;
int n;
int main(){
	cin>>n;
	for(int i=1;i<=n;i++){
		int t,k;
		cin>>t>>k;
		q.push(t);
		for(int j=0;j<q.size();){
		    if(t-q.front()>=86400){
		    	for(int l=0;l<m[q.front()].size();l++){
		    	  mp[m[q.front()][l]]--;
		    	  if(mp[m[q.front()][l]]==0){
		    	  	mp.erase(m[q.front()][l]);
				  }
				}
		  		q.pop();
		    }else{
		  		break;
		    }
		}
		for(int j=1;j<=k;j++){
		  int c;
		  cin>>c;
		  m[i].push_back(c);
		  if(mp.count(c)){
		  	mp[c]++;
		  }else{
		  	mp[c]=1;
		  }
		}
		cout<<mp.size()<<'\n';
	}
	return 0;
}

正确思路:

这道题实际上用queuequeue来储存船的所有信息会最好,因为queuequeue是先进先出先进先出的容器,所以我们可以完全使用queuequeue来存储船的信息。

接着我们可以用数组来算国籍,这样在操作上会有很大帮助

正确代码:

C++
#include <bits/stdc++.h>
using namespace std;
int n,t,ans;
int k,c[10000005];
struct stu{
    int c,t;
}pr;
queue<stu> p;
int main(){
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>t>>k;
        while(!p.empty()){
            pr = p.front();
            if(pr.t+86400<=t){
                c[pr.c]--;
                if(!c[pr.c]){
                    ans--;
                }
                p.pop();
            }
            else break;
        }
        for(int j=1;j<=k;j++){
            int x;
            cin>>x;
            stu tmp={x,t};
            p.push(tmp);
            c[x]++;
            if(c[x]==1){
                ans++;
            }
        }
        cout<<ans<<'\n';
    }
    return 0;
}

T750439 中位数

错误代码:

C++
#include<bits/stdc++.h>
using namespace std;
int a[100005],n;
int main(){
	ios::sync_with_stdio(false);
	cin>>n;
	for(int i=1;i<=n;i++){
	  cin>>a[i];
	}
	for(int i=1;i<=n;i+=2){
	  sort(a+1,a+1+i);
	  cout<<a[(i+1)/2]<<'\n';
	}
	return 0;
}

错误原因:

O(n2log⁡n)O(n^2 \log{n})的复杂度你不超时谁超时??

正确思路:

我们可以维护一个大根堆和小根堆,同时满足大根堆的取出值取出值小于小根堆的取出值取出值,这样我们就可以用O(nlog⁡n)O(n \log{n})的复杂度做出本题

正确代码:

C++
#include<bits/stdc++.h>
using namespace std;
int n,x;
priority_queue<int,vector<int> > q1;
priority_queue<int,vector<int>,greater<int> > q2;
int get_num(int i){
    while(q1.size()>i) q2.push(q1.top()), q1.pop();
    while(q1.size()<i) q1.push(q2.top()), q2.pop();
    return q1.top();
}
int main(){
    cin>>n>>x;
    q1.push(x);
    cout<<q1.top()<<endl;
    for(int i=2;i<=n;i++){
        cin>>x;
        if(x>q1.top()){
          q2.push(x);
        }else{
          q1.push(x);
        }
        if(i%2){
            cout<<get_num(i/2+1)<<'\n';
        }
    }
    return 0;
}
原文