标签:
题意:三个操作
1 a b : 队列中加入(x = a, y = b);
-1 a b : 队列中减去(x = a, y = b);
0 p q :从队列的数对中查询哪一对x,y能够让 p * x + q * y最大;
分析:因为一开始就觉得如果暴力绝对会超时,但是时限是30 000 ms,而且看见FB的又是8800ms过的,所以就暴力一次试试,但是TLE.
1 #include <iostream> 2 #include <cmath> 3 #include <vector> 4 #include <cstdio> 5 #include <cstring> 6 #include <cstdlib> 7 #include <string> 8 #include <sstream> 9 #include <algorithm> 10 #define Max 2147483647 11 #define INF -0x7fffffff 12 #define N 910 13 #define ll long long 14 #define mem(a,b) memset(a,b,sizeof(a)) 15 #define repu(i, a, b) for(int i = (a); i < (b); i++) 16 const double PI=-acos(-1.0); 17 using namespace std; 18 vector<pair<ll,ll> > v; 19 int main() 20 { 21 ll a,b,c; 22 int n; 23 while(scanf("%d",&n) && n) 24 { 25 v.clear(); 26 repu(i,0,n) 27 { 28 scanf("%I64d%I64d%I64d",&a,&b,&c); 29 vector<pair<ll,ll> > ::iterator it; 30 if(a == 1) 31 v.push_back(pair<ll,ll>(b,c)); 32 else if(a == -1) 33 { 34 pair<ll,ll> t = pair<ll,ll>(b,c); 35 for(it = v.begin(); it!=v.end(); it++) 36 { 37 if(*it == t) 38 { 39 v.erase(it); 40 break; 41 } 42 } 43 } 44 else 45 { 46 ll maxn = 0; 47 int flag = 1; 48 for(it = v.begin(); it!=v.end(); it++) 49 { 50 if(flag) 51 maxn = it->first * b + it->second * c,flag = 0; 52 else 53 maxn = max(it->first * b + it->second * c,maxn); 54 } 55 printf("%I64d\n",maxn); 56 } 57 } 58 } 59 return 0; 60 }
1 #include <iostream> 2 #include <cmath> 3 #include <list> 4 #include <cstdio> 5 #include <cstring> 6 #include <cstdlib> 7 #include <string> 8 #include <sstream> 9 #include <algorithm> 10 #define Max 2147483647 11 #define INF -0x7fffffff 12 #define N 910 13 #define ll long long 14 #define mem(a,b) memset(a,b,sizeof(a)) 15 #define repu(i, a, b) for(int i = (a); i < (b); i++) 16 const double PI=-acos(-1.0); 17 #define pii pair<ll,ll> 18 using namespace std; 19 list<pii > v; 20 int main() 21 { 22 ll a,b,c; 23 int n; 24 while(scanf("%d",&n) && n) 25 { 26 v.clear(); 27 repu(i,0,n) 28 { 29 scanf("%I64d%I64d%I64d",&a,&b,&c); 30 list<pii> ::iterator it; 31 if(a == 1) 32 v.push_back(pii(b,c)); 33 else if(a == -1) 34 { 35 pii t = pii(b,c); 36 for(it = v.begin(); it!=v.end(); it++) 37 { 38 if(*it == t) 39 { 40 v.erase(it); 41 break; 42 } 43 } 44 } 45 else 46 { 47 ll maxn = 0; 48 int flag = 1; 49 for(it = v.begin(); it!=v.end(); it++) 50 { 51 if(flag) 52 maxn = it->first * b + it->second * c,flag = 0; 53 else 54 maxn = max(it->first * b + it->second * c,maxn); 55 } 56 printf("%I64d\n",maxn); 57 } 58 } 59 } 60 return 0; 61 }
AC代码也是看了某个大神的代码仅仅用Ctrl+r把我得TLE代码中的vector改成了list,就A了,但是时间有点慢,我也是醉了;
PS:下一步得去看看vector和list的时间复杂度;
标签:
原文地址:http://www.cnblogs.com/ACMERY/p/4697023.html