4.10总结
liuxiuqi_0203 · · 个人记录
4.10总结
T750436 订单结算
错误代码:
#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;
}
错误点
本题因为本蒟蒻忘了保留小数,加上精度误差,就导致错误。
小数:
cout<<fixed << setprecision(1);
另一个不会用也不常用
还有,本题优惠的是
正确思路:
每一种商品是一个由(坑人小数)
因为商品有多个,所以我们需要至多
然后我们依次计算每个商品的钱,最后加起来
复杂度:遍历:
正确代码:
#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
错误代码:
#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;
}
错误原因:
复杂度:
很明显,打死都过不了
正确思路
如果我们观察样例,会发现出现的次数是一个有规律的变化,经过老师的权威发言,我们得到了一个可爱的公式:
可复制
int g=(i-x+1)*(y-i+1);
然后因为异或性质
正确代码:
#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 普及组] 海港
错误代码:
#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;
}
正确思路:
这道题实际上用
接着我们可以用数组来算国籍,这样在操作上会有很大帮助
正确代码:
#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 中位数
错误代码:
#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;
}
错误原因:
正确思路:
我们可以维护一个大根堆和小根堆,同时满足大根堆的
正确代码:
#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;
}