uva11423 - Cache Simulator 树状数组

本文深入探讨了缓存的工作原理、LRU算法及其在不同缓存大小下的表现,通过实例解析了数据引用与缓存命中率之间的关系,并介绍了如何通过输入指令(如ADDR、RANGE、STAT)来优化缓存性能。

Problem G
Cache Simulator
Input:
Standard Input

Output: Standard Output

 

Cache is a special type of memoryproviding very small latency for accessing. The basic idea of using cache is tokeep recently used data close to processors. So, modern processors use smallcache along with large main memory. Program performance depends on the cacheperformance. If data is found in cache (Hit), execution can proceed normally.On the other hand if data is not found in cache, execution stalls until thatdata is fetched from main memory. Computer architects alwayslooks for miss rate of cache (#of misses per 100 accesses).

When a miss occurs, the newdata is kept in the cache and some existing data is removed from cache. Thereare several policies to select which one to evict. A common technique used inany computer systems is LRU (Least Recently Used). According to this scheme,the data that has not been used for longest time is evicted. Let us explain howthis scheme works –

Assume a cache with 4 entries.So, if the processor accesses data 1, 2, 3, 4, 5, 2, 3, 99, 2, 5, 4 thefollowing things will occur -

Cache

Hit/Miss

Data evicted

 

Miss (1)

 

1

Miss (2)

 

1 2

Miss (3)

 

1 2 3

Miss (4)

 

1 2 3 4

Miss (5)

1

2 3 4 5

Hit (2)

 

2 3 4 5

Hit (3)

 

2 3 4 5

Miss (99)

4

2 3 5 99

Hit (2)

 

2 3 5 99

Hit (5)

 

2 3 5 99

Miss (4)

3

2 4 5 99

 

 

 

Here for 11 data references, wehave 7 misses. In this problem you have to find the number of misses fordifferent cache sizes. You will be given data references in several ways –

a)     ADDR      x        : Processor will access data x

b)     RANGE  b y n:Processor will access all data references in the form b+y*k,where k is 0  to n-1  

Your will also be givencommands STAT to print the number of misses for each cache (excluding themisses occurred before last STAT command). The above example can be abstractedin the following way –

RANGE 1 1 5

     RANGE 2 1 2

     ADDR  99

     STAT               ---------------------------------- 6

     ADDR  2

     RANGE 5 –1 2

     STAT                ----------------------------------1

Input

Your program starts with apositive integer N (1<=N<=30), which is the number of caches. The nextline contains size of each cache (in increasing order). Cache size will be atleast 2 but not exceeding 220. The next few lines will contain anyof RANGE, ADDR, or STAT. The last line will contain STAT. Total number of datareferences (all data accessed by processor) will not exceed 107. Forthe sample input, total number of data references is 30 (5+2+1+1+2+10+5+4). Also,the processor uses 24-bit address, no data reference will exceed 224-1and will be non-negative. The value of b, y, n (in RANGE), and x (in ADDR) willbe consistent with all restrictions. Input is terminated by a line containingEND. The input file has around 20000 lines of inputs.

 
Output

For each STAT command just print thenumber of misses (as described above) in a line.

 

Sample Input                             Output for SampleInput

2
4 8
RANGE 1 1 5
RANGE 2 1 2
ADDR   99
STAT                
ADDR   2
RANGE 5 -1 2
STAT                
RANGE 0 10000 10
RANGE 0 20000 5
RANGE 0 30000 4
STAT
END

6 6

1 0

18 13

 

  有N个cash,工作方式就是找当前地址在不在cash中,如果在就把它放到最后面,否则MISS,把最前面那个从cash中删掉,把它放到最后面。每个cash容量不一样大,RANGE操作是查询b+y*k,where k is 0  to n-1,ADDR是查询x,STAT输出每个cash MISS量。

  用树状数组c[i]表示第i个查询的数。vis[x]表示x最近一次出现的位置,那么如果最近出现和这次出现中间的数小于等于cash数说明命中,否则未命中。如果再次出现就要把上次出现的删掉,add(vis[x],-1)。每次把当前出现的加上add(vis[x]=++now,1)。

#include<iostream>
#include<queue>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<set>
#include<map>
#include<vector>
#include<stack>
#include<algorithm>
#define INF 0x3f3f3f3f
#define eps 1e-9
#define MAXNODE 105
#define MOD 10000007
#define SIGMA_SIZE 4
typedef long long LL;
using namespace std;

const int MAXN=17000000;
const int MAXM=35;

int N,now;
int c[MAXN],vis[MAXN],cash[MAXM],ans[MAXM];

int lowbit(int x){
    return x&(-x);
}

int add(int x,int v){
    while(x<MAXN){
        c[x]+=v;
        x+=lowbit(x);
    }
}

int sum(int x){
    int ret=0;
    while(x>0){
        ret+=c[x];
        x-=lowbit(x);
    }
    return ret;
}

void solve(int x){
    if(vis[x]){
        int len=sum(now)-sum(vis[x]-1);
        for(int i=1;i<=N;i++){
            if(cash[i]<len) ans[i]++;
            else break;
        }
        add(vis[x],-1);
    }
    else{
        for(int i=1;i<=N;i++) ans[i]++;
    }
    add(vis[x]=++now,1);
}

int main(){
    freopen("in.txt","r",stdin);
    while(scanf("%d",&N)!=EOF){
        now=0;
        memset(vis,0,sizeof(vis));
        memset(ans,0,sizeof(ans));
        memset(c,0,sizeof(c));
        char str[20];
        for(int i=1;i<=N;i++) scanf("%d",&cash[i]);
        while(scanf("%s",str),str[0]!='E'){
            int b,y,n;
            if(str[0]=='A'){
                scanf("%d",&b);
                solve(b);
            }
            else if(str[0]=='R'){
                scanf("%d%d%d",&b,&y,&n);
                for(int i=0;i<n;i++) solve(b+i*y);
            }
            else{
                for(int i=1;i<N;i++) printf("%d ",ans[i]);
                printf("%d\n",ans[N]);
                memset(ans,0,sizeof(ans));
            }
        }
    }
    return 0;
}



内容概要:本文介绍了一个基于冠豪猪优化算法(CPO)的无人机三维路径规划项目,利用Python实现了在复杂三维环境中为无人机规划安全、高效、低能耗飞行路径的完整解决方案。项目涵盖空间环境建模、无人机动力学约束、路径编码、多目标代价函数设计以及CPO算法的核心实现。通过体素网格建模、动态障碍物处理、路径平滑技术和多约束融合机制,系统能够在高维、密集障碍环境下快速搜索出满足飞行可行性、安全性与能效最优的路径,并支持在线重规划以适应动态环境变化。文中还提供了关键模块的代码示例,包括环境建模、路径评估和CPO优化流程。; 适合人群:具备一定Python编程基础和优化算法基础知识,从事无人机、智能机器人、路径规划或智能优化算法研究的相关科研人员与工程技术人员,尤其适合研究生及有一定工作经验的研发工程师。; 使用场景及目标:①应用于复杂三维环境下的无人机自主导航与避障;②研究智能优化算法(如CPO)在路径规划中的实际部署与性能优化;③实现多目标(路径最短、能耗最低、安全性最高)耦合条件下的工程化路径求解;④构建可扩展的智能无人系统决策框架。; 阅读建议:建议结合文中模型架构与代码示例进行实践运行,重点关注目标函数设计、CPO算法改进策略与约束处理机制,宜在仿真环境中测试不同场景以深入理解算法行为与系统鲁棒性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值