Codeforces Round #413 (Div.1 + Div. 2) B. T-shirt buying
来源:互联网 发布:淘宝网账号注册官网 编辑:程序博客网 时间:2024/06/04 18:57
是一个模拟水题,但是写的时候WA了无数次最后改了还是TLE,而且很麻烦。。需要锻炼啊。
先给上我TLE的版本。留着以后思考为什么TLE
我原来的大体思路是:开三个数组分别代表1 、2 、3的三种衣服,排序。顾客来了从左往右扫。然后用t1 t2 t3分别代表之前顾客已经扫到的位置。是否卖给顾客这条衣服时,用一个used数组记录之前是否卖过,没有则可以,卖了继续往下扫直到没有。下一个顾客来了从上一次更新的t1 t2 t3扫。
-1的情况是 衣服卖完了或者没有这种衣服。 用sum记录是否衣服卖完。
#include <cstdio>#include <iostream>#include <string>#include <algorithm>#include <map>#include <cstring>using namespace std;const int maxn = 200005;map <long long ,int> maps;bool used[maxn];long long cost[maxn];long long a[maxn];long long b[maxn];long long c[maxn];int t1 = 0, t2 = 0, t3 = 0;int cnt1 = 0, cnt2 = 0, cnt3 = 0;int sum = 0;void solve(long long *x, int t,int ccc){ if(t <= ccc && ccc) { if(!used[maps[x[t]]]) { used[maps[x[t]]] = true; cout << x[t]; t++; sum++; } else { bool flag = false; while(used[maps[x[t]]]) { t++; if(t>=ccc) { cout << "-1"; flag = true; break; } } if(!flag) { used[maps[x[t]]] = true; cout << x[t]; t++; sum++; } } } else cout << "-1";}int main(){ int n; while(cin >> n && n) { memset(used,false,sizeof(used)); maps.clear(); for(int i=0; i<n; i++) { long long p; cin >> p; cost[i] = p; maps[p] = i; } cnt1 = 0; cnt2 = 0; cnt3 = 0; for(int i=0; i<n; i++) { int num; cin >> num; if(num == 1) a[cnt1++] = cost[i]; else if(num == 2) b[cnt2++] = cost[i]; else c[cnt3++] = cost[i]; } for(int i=0; i<n; i++) { int num; cin >> num; if(num == 1) a[cnt1++] = cost[i]; else if(num == 2) b[cnt2++] = cost[i]; else c[cnt3++] = cost[i]; } sort(a,a+cnt1); sort(b,b+cnt2); sort(c,c+cnt3); int tot; sum = 0; cin >> tot; t1 = 0, t2 = 0, t3 = 0; for(int i=0; i<tot; i++) { int num; cin >> num; if(i) cout << " "; if(sum > n) cout << "-1" ; else { if(num == 1) solve(a,t1,cnt1); else if(num == 2) solve(b,t2,cnt2); else solve(c,t3,cnt3); } } cout << endl; } return 0;}
然后是正确的做法。
用3个set集合存储三种衣服。因为set是可以自动排序的。然后从头开始扫,卖完了或者当前可以卖。扫完后便删除。
-1的情况是全部卖完。
(其实我觉得思路差不多的,只是用set更加的方便)
#include <cstdio>#include <iostream>#include <string>#include <algorithm>#include <map>#include <cstring>#include <set>using namespace std;const int maxn = 200000 + 5;int p[maxn];int a[maxn];int b[maxn];set<int> s[4];map<int,int> maps;bool used[maxn];int main(){ int n; while(scanf("%d",&n)!=EOF && n) { memset(used,false,sizeof(used)); for(int i=0; i<n; i++) { scanf("%d", &p[i]); maps[p[i]] = i; } for(int i=0; i<n; i++) { scanf("%d",&a[i]); s[a[i]].insert(p[i]); } for(int i=0; i<n; i++) { scanf("%d",&b[i]); s[b[i]].insert(p[i]); } int m; scanf("%d",&m); for(int i=0; i<m; i++) { int ans = -1; int t; scanf("%d",&t); if(i) printf(" "); while(!s[t].empty()) { if(!used[maps[*(s[t].begin())]]) { used[maps[*(s[t].begin())]] = true; ans = *(s[t].begin()); s[t].erase(s[t].begin()); break; } else { s[t].erase(s[t].begin()); } } printf("%d",ans); } printf("\n"); }}
0 0
- Codeforces Round #413 Div. 1 + Div. 2 B. T-shirt buying
- Codeforces Round #413 (Div.1 + Div. 2) B. T-shirt buying
- codeforces Round #413 Div. 1 + Div. 2 B T-shirt buying
- Playrix Codescapes Cup (Codeforces Round #413, rated, Div. 1 + Div. 2) B. T-shirt buying
- codeforcesPlayrix Codescapes Cup (Codeforces Round #413, rated, Div. 1 + Div. 2)B题B. T-shirt buying
- Codeforces Round #413 B. T-shirt buying
- Codeforces Round #413 B. T-shirt buying
- codeforces round #412 B. T-shirt buying
- CodeForces 799B (B) T-shirt buying
- codeforces #413 B T-shirt buying(set快速查找)
- Codeforces 799B T-shirt buying 题解
- Codeforces Round #412 (rated, Div. 2, base on VK Cup 2017 Round 3) B. T-Shirt Hunt(模拟)
- (Codeforces Round #413, rated, Div. 1 + Div. 2)(A+B)
- Codeforces Round #142 (Div. 2) B - T-primes
- Codeforces Round #408 (Div. 2) A Buying A House
- codeforces round#408(div.2) A. Buying A House
- Codeforces Round #408 (Div. 2) A. Buying A House
- Codeforces Round #413(Div. 1 + Div. 2)
- 安卓开发中很多人都不知道的两个log技巧
- Java Thread(线程)sleep和wait的区别浅析
- java导出excel数据量过大解决方案
- CSS 伪类(a标签) 背景
- SVN创建分支/合并分支/切换分支
- Codeforces Round #413 (Div.1 + Div. 2) B. T-shirt buying
- ubuntu各种代理设置
- my97datepicker日历展示出现中文乱码的问题
- maven+jetty多webapp配置(非cargo)
- Python学习记录--第一天的练习
- yum源失效问题 网易 阿里等国内镜像失效
- Problem D: 质心算法
- 解决Caused by: javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibe
- C++ namespace