标签:reg sizeof sam return 最小值 ref content test string
在第4次缺失时将3号单元换出Cache。
1 #include <iostream> 2 #include <cstdio> 3 #include <cstdlib> 4 #include <cstring> 5 #include <algorithm> 6 #include <map> 7 #include <queue> 8 #define max(a, b) ((a) > (b) ? (a) : (b)) 9 #define min(a, b) ((a) < (b) ? (a) : (b)) 10 11 inline void read(long long &x) 12 { 13 x = 0;char ch = getchar(), c = ch; 14 while(ch < ‘0‘ || ch > ‘9‘)c = ch, ch = getchar(); 15 while(ch <= ‘9‘ && ch >= ‘0‘)x = x * 10 + ch - ‘0‘, ch = getchar(); 16 if(c == ‘-‘)x = -x; 17 } 18 19 const long long MAXN = 100000 + 10; 20 21 long long n,m,num[MAXN],cnt[MAXN],nxt[MAXN],ans,chuxian[MAXN],sum; 22 23 bool cmp(long long a, long long b) 24 { 25 return num[a] < num[b]; 26 } 27 28 struct cmpp 29 { 30 bool operator()(long long a, long long b) 31 { 32 return nxt[a] < nxt[b]; 33 } 34 }; 35 36 std::priority_queue <int, std::vector<int>, cmpp> q; 37 38 int main() 39 { 40 read(n), read(m); 41 for(register long long i = 1;i <= n;++ i) 42 read(num[i]), cnt[i] = i; 43 std::sort(cnt + 1, cnt + 1 + n, cmp); 44 long long tot = 1, pre = num[cnt[1]]; 45 num[cnt[1]] = tot; 46 for(register long long i = 2;i <= n;++ i) 47 if(num[cnt[i]] == pre)num[cnt[i]] = tot; 48 else ++ tot, pre = num[cnt[i]], num[cnt[i]] = tot; 49 memset(cnt, 0, sizeof(cnt)); 50 memset(nxt, 0x3f, sizeof(nxt)); 51 for(register long long i = n;i >= 1;-- i) 52 { 53 if(cnt[num[i]])nxt[i] = cnt[num[i]]; 54 cnt[num[i]] = i; 55 } 56 memset(cnt, 0, sizeof(cnt)); 57 58 ///cnt[i]表示i这个数是否在堆中以及i在原数组中的位置,chuxian[i]表示原数组i元素是否在堆中 59 for(register long long i = 1;i <= n;++ i) 60 { 61 if(cnt[num[i]]) 62 { 63 q.push(i); 64 chuxian[cnt[num[i]]] = 0; 65 chuxian[i] = 1; 66 } 67 else 68 { 69 if(sum < m)q.push(i), cnt[num[i]] = i, chuxian[i] = 1, ++ans, ++sum; 70 else 71 { 72 while(chuxian[q.top()] == 0)q.pop(); 73 cnt[num[q.top()]] = 0; 74 chuxian[q.top()] = 0; 75 q.pop(); 76 q.push(i); 77 chuxian[i] = 1; 78 cnt[num[i]] = i; 79 ++ ans; 80 } 81 } 82 } 83 printf("%lld", ans); 84 return 0; 85 }
标签:reg sizeof sam return 最小值 ref content test string
原文地址:http://www.cnblogs.com/huibixiaoxing/p/7642231.html