Topcoder 11_21

第一次做topcoder,状态不是很好,一个向量容器的排序都让我想了好久,感觉到头文件定义的强大,但是我的头文件明显是不完善的,我只写了常用的几个。

适当的头文件定义可以加快变成的速度:

头文件定义(有待完善):

View Code
 1 #include <cassert>
 2 #include <iostream>
 3 #include <cstdio>
 4 #include <vector>
 5 #include <map>
 6 #include <cstdlib>
 7 #include <ctime>
 8 #include <iterator>
 9 #include <cmath>
10 #include <queue>
11 #include <string>
12 #include <cstring>
13 #include <algorithm>
14 
15 #pragma comment(linker,"/STACK:60777216")
16 
17 using namespace std;
18 
19 typedef long long LL;
20 typedef vector<int> VI;
21 
22 #define SORT(c) sort((c).begin(),(c).end())
23 #define REV(c) reverse((c).begin(),(c).end())
24 #define FOR(i,a,b) for(int i=(a); i < (b);i++)
25 #define CL(a,b) memset(a,b,sizeof(a))
26 #define pb push_back
27 
28 int max(int x,int y) {return x>y?x:y;}
29 int min(int x,int y) {return x>y?y:x;}
30 int f_abs(int x) {return (x)>(0)?(x):(-x);}

topcoder div2 500

size有M,L两种,提取出来单独比较(一道题目气球的大小一定要一样),结果maxAccepted中元素最多为15个,枚举每个题size的状态,复杂度O(2^N*N),比较取出最小的。

View Code
 1 class ICPCBalloons{
 2     public:
 3     int solve(VI a,VI b) {
 4         int suma=0,sumb=0;
 5         FOR(i,0,a.size())suma+=a[i];
 6         FOR(i,0,b.size())sumb+=b[i];
 7         if(suma>sumb) return -1;
 8         else {
 9             int sum=0;
10             SORT(a);  SORT(b);
11             REV(a), REV(b);
12             for(int i=0,j=0;i<a.size();i++) {
13                 if(j<b.size()) sum+=max(a[i]-b[j],0),j++;
14                 else sum+=a[i];
15             }
16             return sum;
17         }
18     }
19     int minRepaintings(vector <int> bc, string bs, vector <int> mac){
20         VI la,ma;
21         la.clear(); ma.clear();
22         FOR(i,0,bs.size()) {
23             if(bs[i]=='M') ma.pb(bc[i]);
24             else la.pb(bc[i]);
25         }
26         int ans=-1,len=mac.size();
27         VI L,M;
28         FOR(mask,0,1<<len) {
29             L.clear(); M.clear();
30              FOR(j,0,len) {
31                  if(mask&(1<<j)) M.pb(mac[j]);
32                  else L.pb(mac[j]);
33              }
34              int ansl=solve(M,ma),ansm=solve(L,la);
35              if(ansl==-1 || ansm==-1) continue;
36              ansl+=ansm;
37              if(ans==-1||ans>ansl) ans=ansl;
38         }
39         return ans;
40     } 
41 };
原文地址:https://www.cnblogs.com/zhang1107/p/2780608.html