Example #1
0
 REP(i, MAXN) {
     mm[i][MAXN - 1] = num[i][MAXN - 1];
     pq.push(state(i, MAXN - 1, num[i][MAXN - 1]));
 }
Example #2
0
void init()
{
	int i,j,k;
	scanf("%d",&M);

	while( !heap.empty() ) heap.pop();

	//初始化
	for (i=1;i<=N;++i) 
	{
		dis[i]=INFINITE;
		visit[i]=false;
		path[i]=0;
		bian[i][0]=0;
		for (j=1;j<=N;++j) map[i][j]=INFINITE;
	}

	//读入信息
	for (k=1;k<=M;++k)
	{
		scanf("%d%d",&i,&j);
		scanf("%d",&map[i][j]);
		map[j][i]=map[i][j];

		bian[i][0]++;
		bian[j][0]++;
		bian[i][bian[i][0]]=j;
		bian[j][bian[j][0]]=i;
	}

	//堆初始化
	dis[2]=0;
	path[2]=1;
	node temp;
	temp.dist=0;
	temp.ID=2;
	heap.push(temp);

	int l;
	while (true)
	{
		//从堆中找出最小距离
		while (!heap.empty() && visit[heap.top().ID]) heap.pop();
		if (heap.empty()) break;

		k=heap.top().ID;
		dis[k]=heap.top().dist;
		heap.pop();

		visit[k]=true;
		for (j=1;j<=bian[k][0];++j) 
		{
			l=bian[k][j];
			//满足松弛操作,加入堆
			if (dis[k]+map[k][l]<dis[l]) 
			{
				dis[l]=dis[k]+map[k][l];
				temp.ID=l;
				temp.dist=dis[l];
				heap.push(temp);
			}
			//DP计算方案
			if (visit[l] && dis[l]<dis[k]) path[k]+=path[l];
		}

	}

	printf("%d\n",path[1]);
}
template<class T> inline void CLR(priority_queue<T, vector<T>, greater<T> > &Q) {
    while (!Q.empty()) Q.pop();
}
Example #4
0
void init(){
    for(int i=0;i<MAX_V;i++) G[i].clear();
    while(!que.empty()) que.pop();
}
Example #5
0
void insert(int x) {
    if(queue_b.empty()) {
        queue_b.push(x);
    }
    else{
      if(x > queue_b.top())
        queue_b.push(x);
      else
        queue_s.push(x);
    }
    if(queue_b.size() < queue_s.size() ) {
        queue_b.push(queue_s.top());
        queue_s.pop();
    }
    if(queue_b.size() > queue_s.size() + 1) {
        queue_s.push(queue_b.top());
        queue_b.pop();
    }
}
Example #6
0
void update(int pos)
{
	que.push(make_pair(tot[pos],pos));
}
Example #7
0
	size_t const current() const { return q.size(); }
int main()
{
    //freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);

    init();

    int res = game(h, t);

    if (res == 0)
    {
        puts("Draw");
        return 0;
    }

    if (res == 1) puts("Ivan");
    if (res == 2) puts("Zmey");

    if (res == 2)
    {
        int res2 = rec(h, t, res);
        printf("%d\n", res2);
    } else {
        q.push(make(0, h, t));
        dp[h][t] = 0;
        while (!q.empty())
        {
            piii cur = q.top();
            q.pop();
            int x = cur.second.first;
            int y = cur.second.second;

            if (x + y > R) continue;

            int w = cur.first;
            if (dp[x][y] != w) continue;

            int lim = min(x, n);
            for (int i = 1; i <= lim; ++i)
            {
                int dx = H[i].first;
                int dy = H[i].second;
                int nx = x - i + dx;
                int ny = y + dy;

                if (dp[nx][ny] == -1 || dp[nx][ny] > dp[x][y] + 1)
                {
                    dp[nx][ny] = dp[x][y] + 1;
                    q.push(make(dp[nx][ny], nx, ny));
                }
            }

            lim = min(y, m);
            for (int i = 1; i <= lim; ++i)
            {
                int dx = T[i].first;
                int dy = T[i].second;
                int nx = x + dx;
                int ny = y - i + dy;

                if (dp[nx][ny] == -1 || dp[nx][ny] > dp[x][y] + 1)
                {
                    dp[nx][ny] = dp[x][y] + 1;
                    q.push(make(dp[nx][ny], nx, ny));
                }
            }
        }

        printf("%d\n", dp[0][0]);
    }
    return 0;
}
int main() {
    int N;
    int ans = 0;
    int size = 0;
    scanf("%d", &N);
    while(N--) {
        int type;
        scanf("%d", &type);
        if(type == 2) {
            if(ans) printf("%d\n", ans);
            else puts("No reviews yet");
        } else {
            int x;
            scanf("%d", &x);
            switch(size) {
                case 0: 
                case 1: bad.push(x); break;
                case 2: bad.push(x); ans = bad.top(); bad.pop(); break;
                default: {
                    if (x > ans) {
                        good.push(x);
                        if(size % 3 != 2) {
                            bad.push(ans);
                            ans = good.top();
                            good.pop();
                        }
                    }
                    else {
                        bad.push(x);
                        if(size % 3 == 2) {
                            good.push(ans);
                            ans = bad.top();
                            bad.pop();
                        }
                    }
                }
            }
            ++size;
        }
    }
}
Example #10
0
int main ( ) {
    while(scanf("%d %d", &v, &e) != EOF && v && e) {
        scanf("%d %d", &f, &t);
        for (int i = 0; i < e; i++) {
            scanf("%d %d %d", &a, &b, &w);
            ed.to = b;
            ed.from = a;
            ed.w = w;
            adj[a].push_back(ed);
            ed.to = a;
            ed.from = b;
            adj[b].push_back(ed);
        }
        for (int i = 0; i < v; i++) {
            verts[i].w = -1;
            verts[i].n = i;
        }
        verts[f].w = 0;
        fila.push(verts[f]);

        while (!fila.empty()) {
            nd = fila.top();
            fila.pop();

            for (int i = 0; i < adj[nd.n].size(); i++) {
                if (verts[adj[nd.n][i].to].w == -1 || verts[adj[nd.n][i].to].w > nd.w + adj[nd.n][i].w) {
                    verts[adj[nd.n][i].to].w = nd.w + adj[nd.n][i].w;
                    fila.push(verts[adj[nd.n][i].to]);
                }
            }
        }

        for (int i = 0; i < adj[t].size(); i++) {
            regr.push(adj[t][i]);
        }

        while (!regr.empty()) {
            ed = regr.front();
            regr.pop();
            
            if (ed.w != -1 && verts[ed.to].w + ed.w == verts[ed.from].w) {
                for (int i = 0; i < adj[ed.to].size(); i++) {
                    if (adj[ed.to][i].to = ed.from && adj[ed.to][i].w == ed.w) adj[ed.to][i].w = -1;
                    else regr.push(adj[ed.to][i]);
                }
                for (int i = 0; i < adj[ed.from].size(); i++) if (adj[ed.from][i].to = ed.to && adj[ed.from][i].w == ed.w) adj[ed.from][i].w = -1;
            }
        }
        for (int i = 0; i < v; i++) {
            printf("%d(%d) -> ", i, verts[i].w);
            for (int j = 0; j < adj[i].size(); j++) printf("%d(%d) ", adj[i][j].to, adj[i][j].w);
            printf("\n");
        }

        for(int i = 0; i < v; i++) verts[i].w = -1;

        fila.push(verts[f]);
        while (!fila.empty() ) {
            nd = fila.top();
            fila.pop();

            for (int i = 0; i < adj[nd.n].size(); i++) {
                if (adj[nd.n][i].w != -1 && (verts[adj[nd.n][i].to].w == -1 || verts[adj[nd.n][i].to].w > nd.w + adj[nd.n][i].w)) {
                    verts[adj[nd.n][i].to].w = nd.w + adj[nd.n][i].w;
                    if (adj[nd.n][i].to == t) goto endloop;
                    fila.push(verts[adj[nd.n][i].to]);
                }
            }
        }
endloop:
        printf("%d\n", verts[t].w);
        for (int i = 0; i < e; i++) adj[i].clear();

        while (!fila.empty()) fila.pop();
    }  
}
Example #11
0
int main()
{
    int tc;
    scanf("%i\n", &tc);
    for(int i = 0; i < tc; i++)
    {
        int n;
        scanf("%i\n", &n);
        while(!asks.empty())
            asks.pop();
        while(!bids.empty())
            bids.pop();
        int stockPrice = -1;
        for(int j = 0; j < n; j++)
        {
            int numero;
            int precio;
            scanf("%s %i shares at %i", temp, &numero, &precio);
            string tipo(temp);
            bool compra = tipo == "buy";
            if(compra)
                bids.push(Oferta(numero, precio));
            else   
                asks.push(Oferta(numero, precio));
            while(!bids.empty() && !asks.empty() && bids.top().precio >= asks.top().precio)
            {
                Oferta bid = bids.top();
                Oferta ask = asks.top();
                bids.pop();
                asks.pop();
                if(bid.numero > ask.numero)
                {
                    bid.numero -= ask.numero;
                    bids.push(bid);
                }
                else if(bid.numero < ask.numero)
                {
                    ask.numero -= bid.numero;
                    asks.push(ask);
                }
                stockPrice = ask.precio;
            }
            if(bids.empty() && asks.empty())
            {
                if(stockPrice == -1)
                    printf("- - -\n");
                else
                    printf("- - %i\n", stockPrice);
            }
            else if(bids.empty())
            {
                if(stockPrice == -1)
                    printf("%i - -\n", asks.top().precio);
                else
                    printf("%i - %i\n", asks.top().precio, stockPrice);
            }
            else if(asks.empty())
            {
                if(stockPrice == -1)
                    printf("- %i -\n", bids.top().precio);
                else
                    printf("- %i %i\n", bids.top().precio, stockPrice);
            }
            else
            {
                if(stockPrice == -1)
                    printf("%i %i -\n", asks.top().precio, bids.top().precio);
                else
                    printf("%i %i %i\n", asks.top().precio, bids.top().precio, stockPrice);
            }
        }
    }
}
Example #12
0
void init() {
    while(!pq.empty()) pq.pop();
    for(int i = 0; i <= maxn; i++) {
        pre[i] = i;
    }
}
Example #13
0
// it destroys queue
void showQueue(priority_queue<edge, vector< edge >, compareWeights> queue) {
	while (!queue.empty()) {
		cout << queue.top().v << "-" << queue.top().u << ":" << queue.top().w << endl;
		queue.pop();
	}
}
Example #14
0
void AwithTheManhattanDistanceHeuristic()
{

    int stateNum = 0;
    while(!PQ.empty())PQ.pop();

    Initstate.g=0;
    Initstate.h=getManhattanDistanceNum(Initstate);

    PQ.push(Initstate);

    printf("Expanding state:\n");
    Initstate.output();


    while (!PQ.empty())
    {
        if(PQ.size()>lenOfQueue){
            lenOfQueue = PQ.size();
        }

        stateNum++;
        Puzzle queueFront = PQ.top();
        PQ.pop();

        printf("g(n)=%d  " ,queueFront.g);
        printf("h(n)=%d  ",queueFront.h);
        outputExpanding(queueFront);

        if(judgeComplete(queueFront))
        {
            break;
        }
        else
        {
            Puzzle tPuzzle = queueFront;
            if(tranform(LEFT,tPuzzle)&&!judgeSame(tPuzzle,queueFront))
            {
                tPuzzle.g = queueFront.g+1;
                tPuzzle.h = getManhattanDistanceNum(tPuzzle);
                giveFatherValue(tPuzzle,queueFront);
                PQ.push(tPuzzle);
                stateNum++;
            }
            tPuzzle = queueFront;
            if(tranform(RIGHT,tPuzzle)&&!judgeSame(tPuzzle,queueFront))
            {
                tPuzzle.g = queueFront.g+1;
                tPuzzle.h = getManhattanDistanceNum(tPuzzle);
                giveFatherValue(tPuzzle,queueFront);
                PQ.push(tPuzzle);
                stateNum++;
            }
            tPuzzle = queueFront;
            if(tranform(UP,tPuzzle)&&!judgeSame(tPuzzle,queueFront))
            {
                tPuzzle.g = queueFront.g+1;
                tPuzzle.h = getManhattanDistanceNum(tPuzzle);
                giveFatherValue(tPuzzle,queueFront);
                PQ.push(tPuzzle);
                stateNum++;
            }
            tPuzzle = queueFront;
            if(tranform(DOWN,tPuzzle)&&!judgeSame(tPuzzle,queueFront))
            {
                tPuzzle.g = queueFront.g+1;
                tPuzzle.h = getManhattanDistanceNum(tPuzzle);
                giveFatherValue(tPuzzle,queueFront);
                PQ.push(tPuzzle);
                stateNum++;
            }
        }
    }
    printf("%d\n",stateNum);


}
Example #15
0
int main(){
    int n,l,i,bl;
    scanf("%d%d",&n,&l);
    for(i=0;i<l;i++){
        //scanf("%d",&b[i]);
        b[i]=scanInt();
        maxq.push(b[i]);
        minq.push(b[i]);
    }
    if(maxq.top()-minq.top()+1==l) ans++;
    bl=0;
    while(i<n){
        black[b[bl]]=1;
        //scanf("%d",&b[i]);
        b[i]=scanInt();
        maxq.push(b[i]);
        minq.push(b[i]);
        while(black[maxq.top()])
            maxq.pop();
        while(black[minq.top()])    
            minq.pop();
        if(maxq.top()-minq.top()+1==l) ans++;
        bl++;
        i++;
    }
    printf("%d",ans);
    return 0;
}
Example #16
0
File: tour.cpp Project: atriel/code
int main(){

	int t;
	inp(t);
	while(t--){
		memset(g,0, sizeof g);
		memset(visit,0, sizeof visit);
		inp(n);
		for(int i = 1; i <= n; i++){
			int nv;
			inp(nv);
			for(int j = 0; j < nv; j++){
				int neigh;
				inp(neigh);
				g[neigh][i] = 1;
			}
		}
		
		while(!desc.empty())
			desc.pop();
		
		timer = 0;
		
		for(int i = 1; i <= n; i++){
			if(visit[i] == 0)
				dfs(i);
		}

		memset(scc,0,sizeof scc);
		memset(visit,0,sizeof visit);
		int count = 0;
		while(!desc.empty()){
			int i = (desc.top()).second;
			desc.pop();
			if(visit[i] == 0){
				count++;
				redfs(i, count);
			}
		}

		int inedge[1001];
		memset(inedge,0,sizeof inedge);
		for(int i = 1; i <= n; i++){
			for(int j = 1; j <= n; j++){
				if(g[i][j] == 1 && scc[i] != scc[j]){
					inedge[scc[j]] = 1;
				}
			}
		}
		// for(int i = 1; i <= n; i++)
		// 	cout << scc[i] << endl;
		int zin = 0;
		int cone;
		for(int i = 1; i <= count; i++){
			if(inedge[i] == 0){
				zin++;
				cone = i;
			}
		}

		if(zin == 1){
			int setpl = 0;
			for(int i = 1; i <= n; i++){
				if(scc[i] == cone){
					setpl++;
				}
			}
			printf("%d\n", setpl);
		}
		else{
			printf("0\n");
		}

	}

	return 0;	
}
Example #17
0
int main()
{
	int hh, mm, ss, viptag, index, nowtime;
	node temp;
	pair<int, int> tplayer;

	cin >> N;
	play_time.resize(N);
	is_vip_player.resize(N);
	for (int i = 0; i != N; ++i)
	{
		scanf("%d:%d:%d %d %d", &hh, &mm, &ss, &play_time[i], &viptag);
		is_vip_player[i] = (bool)viptag;
		play_time[i] = 60 * min(play_time[i], 120);
		temp.time = hh*3600 + mm*60 + ss;
		temp.id = i;
		temp.type = 1;
		affairs.push(temp);
	}

	cin >> K >> M;
	nums.resize(K);
	is_vip_table.resize(K);
	for (int i = 0; i != M; ++i)
	{
		cin >> index;
		is_vip_table[--index] = true;
	}
	for (int i = 0; i != K; ++i)
	{
		if (is_vip_table[i])
		{
			vip_table.insert(i);
		}
		else
		{
			ord_table.insert(i);
		}
	}

	while (!affairs.empty())
	{
		temp = affairs.top();
		affairs.pop();
		nowtime = temp.time;
		if (nowtime >= close_time)
		{
			break;
		}

		if (0 == temp.type)
		{
			freeTable(temp.id);
			/* 多个桌子可能同时释放*/
			while (!affairs.empty() && affairs.top().type == 0 && affairs.top().time == nowtime)
			{
				freeTable(affairs.top().id);
				affairs.pop();
			}

			while (!vip_table.empty() && !vip_player.empty())
			{
				temp.time = nowtime + vip_player.begin()->second;
				temp.type = 0;
				temp.id = *vip_table.begin();
				++nums[temp.id];
				printTime(vip_player.begin()->first, nowtime);
				affairs.push(temp);
				vip_table.erase(vip_table.begin());
				vip_player.erase(vip_player.begin());
			}

			while ((!ord_player.empty() || !vip_player.empty()) && (!ord_table.empty() || !vip_table.empty()))
			{
				tplayer = getPlayer();
				temp.time = nowtime + tplayer.second;
				temp.type = 0;
				temp.id = getTable();
				++nums[temp.id];
				printTime(tplayer.first, nowtime);
				affairs.push(temp);
			}
		}
		else
		{
			if (is_vip_player[temp.id])
			{
				if (!vip_table.empty())
				{
					temp.time = nowtime + play_time[temp.id];
					printTime(nowtime, nowtime);
					temp.type = 0;
					temp.id = *vip_table.begin();
					++nums[temp.id];
					affairs.push(temp);
					vip_table.erase(vip_table.begin());
				}
				else
				{
					if (!ord_table.empty())
					{
						temp.time = nowtime + play_time[temp.id];
						printTime(nowtime, nowtime);
						temp.type = 0;
						temp.id = *ord_table.begin();
						++nums[temp.id];
						affairs.push(temp);
						ord_table.erase(ord_table.begin());
					}
					else
					{
						vip_player.insert(make_pair(nowtime, play_time[temp.id]));
					}
				}
			}
			else
			{
				if (!vip_table.empty() || !ord_table.empty())
				{
					temp.time = nowtime + play_time[temp.id];
					printTime(nowtime, nowtime);
					temp.type = 0;
					temp.id = getTable();
					++nums[temp.id];
					affairs.push(temp);
				}
				else
				{
					ord_player.insert(make_pair(nowtime, play_time[temp.id]));
				}
			}
		}
	}
	
	for (unsigned int i = 0; i != nums.size(); ++i)
	{
		if (0 != i)
		{
			cout <<  ' ';
		}
		cout << nums[i];
	}
	cout << endl;
	return 0;
}
void dijkstra(int x, int y, int d) {
    if (x >= 0 && y >= 0 && x < C && y < R && t[y][x] == -1 && lab[y][x] != '#') {
        t[y][x] = t[j][i] + d;
        q2.push(make_pair(-t[y][x], make_pair(x, y)));
    }
}
Example #19
0
int getCity()
{
    int ret=-1;
    while (!que.empty() && tot[que.top().second]!=que.top().first)
        que.pop();
    ret=que.top().second;
    que.pop();
    while (!que.empty() && (tot[que.top().second]!=que.top().first || que.top().second==ret))
        que.pop();
    if (!que.empty() && que.top().first==tot[ret])
    {
        que.push(make_pair(tot[ret],ret));
        return -1;
    }
    que.push(make_pair(tot[ret],ret));
    return ret;
}
Example #20
0
void insert(int v) {
  h.push(v);
  ans.push_back("insert " + to_string(v));
}
Example #21
0
void process(int vtx) {    // so, we use -ve sign to reverse the sort order
  taken[vtx] = 1;
  for (int j = 0; j < (int)AdjList[vtx].size(); j++) {
    ii v = AdjList[vtx][j];
    if (!taken[v.first]) pq.push(ii(-v.second, -v.first));
} }
Example #22
0
int main(int argc, char* argv[]){
	
	string ifn;
	string ofn;
	FILE* ifp = NULL;
	FILE* ofp = NULL;
	bool isFile = true;
	int i = 0;
	
	if(argc == 3){
		ifn = argv[1];
		ofn = argv[2];
	}else if(argc == 2){
		ifn = argv[1];
		ofn = "output.txt";
	}else{
		isFile = false;
	}
	
	if(isFile){
		ifp = fopen(ifn.c_str(), "r");
		if(ifp == NULL){
			printf("入力ファイル %s を開けませんでした。", ifn.c_str());
			return -1;
		}
		ofp = fopen(ofn.c_str(), "w");
		if(ofp == NULL){
			printf("出力ファイル %s を開けませんでした。", ofn.c_str());
			return -1;
		}
	}
	
	if(ifp != NULL){
		fscanf(ifp, "%d", &N);
		for(i = 0; i < N; i++){
			fscanf(ifp, "%d %d", &cow[i].first, &cow[i].second);
		}
	}else{
		scanf("%d", &N);
		for(i = 0; i < N; i++){
			scanf("%d %d", &cow[i].first, &cow[i].second);
		}
	}
	
	for(i = 0; i < N; i++){
		stall[i] = 0;
		cow[i].n = i;
	}
	
	sort(cow, cow + N, comp1);
	
	cow[0].first = 1;
	stall[cow[0].n] = 1;
	q.push(cow[0]);
	ans = 1;
	for(i = 1; i < N; i++){
		if(cow[i].first <= q.top().second){
			cow[i].first = ans + 1;
			stall[cow[i].n] = ans + 1;
			q.push(cow[i]);
			ans++;
		}else{
			cow[i].first = q.top().first;
			stall[cow[i].n] = q.top().first;
			q.pop();
			q.push(cow[i]);
		}
	}
	
	if(ofp != NULL){
		fprintf(ofp, "%d\n", ans);
		for(i = 0; i < N; i++){
			fprintf(ofp, "%d\n", stall[i]);
		}
	}
	printf("%d\n", ans);
	for(i = 0;i < N; i++){
		printf("%d\n", stall[i]);
	}
	
	if(ifp != NULL){
		fclose(ifp);
	}
	if(ofp != NULL){
		fclose(ofp);
	}
	
	return 0;
	
}
int main() {

    // nds
    scanf("%d", &n);
    for (int i = 0; i < n; ++i) {
        int id;
        double lat, lon;
        scanf("%d%lf%lf", &id, &lat, &lon);
        st[id] = cnt;
        pos[cnt] = make_pair(lat, lon);
        cnt++;
    }

    // egs
    scanf("%d", &m);
    for (int i = 0; i < m; ++i) {
        int x, y;
        scanf("%d%d", &x, &y);
        x = st[x];
        y = st[y];
        e[x].push_back(y);
        e[y].push_back(x);
    }

    for (int i = 0; i < n; ++i)
        dis[i] = 1e9;


    // find a relative node
    int id = 0;
    for (int i = 0; i < n; ++i)
        if (e[i].size() > 0) {
            id = i;
            break;
        }
    dis[id] = 0;
    Q.push(make_pair(0, id));


    // dij with heap
    for ( ; !Q.empty(); Q.pop()) {

        double cdis = Q.top()._1;
        int cid = Q.top()._2;
        
        if (abs(cdis - dis[cid]) > eps)
            continue;

        for (int i = 0; i < e[cid].size(); ++i) {
            double temp = cdis + cal_dis(pos[cid], pos[e[cid][i]]);
            if (temp < dis[e[cid][i]]) {
                dis[e[cid][i]] = temp;
                Q.push(make_pair(temp, e[cid][i]));
            }
        }
    }

    for (int i = 0; i < n; ++i)
        if (dis[i] < 1e7)
            printf("%d %.5f\n", i, dis[i]);
    

    return 0;
}
Example #24
0
int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0);
    //freopen ("1Garden.txt","r",stdin);

    LL i, j, k, u, v, sum;

    cin >> C >> R;
    cin >> N >> K;


    for (i=0;i<N;i++) {
        cin >> j >> k;
        G[k][j] ++;
    }


    for (j=1; j<=C; j++) {
        for (i=1; i<=R; i++)
            G[i][j] += G[i-1][j];
    }


    for (i=1; i<=R; i++) RS[i] = RE[i] = INF;
    for (i=1; i<=C; i++) CS[i] = CE[i] = INF;

    for (i=1; i<=R; i++) {
        for (j=i; j<=R; j++) {
            u = 1, v = 1, sum = 0;
            for ( ; v <= C; v++) {
                sum += G[j][v]-G[i-1][v];
                while (sum > K || G[j][u]-G[i-1][u] == 0) {
                    sum -= G[j][u]-G[i-1][u];
                    u++;
                }
                if (sum == K) {
                    k = 2*(j-i+1) + 2*(v-u+1);
                    RS[i] = min(k,RS[i]);
                    RE[j] = min(k,RE[j]);
                    CS[u] = min(k,CS[u]);
                    CE[v] = min(k,CE[v]);
                }
            }
        }
    }


    LL ans = INF;
    PLL now;

    for (i=1; i<=R; i++) Q1.push (MP(-RS[i], i));
    for (i=1; i<=C; i++) Q2.push (MP(-CS[i], i));

    for (i=1, j=INF; i<R; i++) {
        j = min (RE[i], j);
        while (Q1.top().Y <= i) Q1.pop();
        ans = min (ans, j-Q1.top().X);
    }

    for (i=1, j=INF; i<C; i++) {
        j = min (CE[i], j);
        while (Q2.top().Y <= i) Q2.pop();
        ans = min (ans, j-Q2.top().X);
    }

    if (ans == INF) cout << "NO\n";
    else cout << ans << endl;

    return 0;
}
Example #25
0
int medianaSubarreglo(int prox, priority_queue<int>& menores, priority_queue<int, vector<int>, greater<int> >& mayores)
{
	
	if (menores.empty())
	{
		menores.push(prox);
	}
	else
	{
		if (prox < menores.top())
		{
			if (menores.size() > mayores.size())
			{
				mayores.push( menores.top() );
				menores.pop();
				menores.push(prox);
			}
			else
			{
				menores.push(prox);
			}
		}
		else
		{
			if (mayores.size() > menores.size())
			{
				menores.push( mayores.top() );
				mayores.pop();
				mayores.push(prox);
			}
			else
			{
				mayores.push(prox);
			}			
		}
	}

	// cout << prox << endl;

	if (menores.size() > mayores.size())
		return menores.top();
	if (menores.size() < mayores.size())
		return mayores.top();
	if (menores.size() == mayores.size())
		return (menores.top()+mayores.top())/2;

}
Example #26
0
bool getAns(priority_queue<Node>& heapOne, vector<Edge>&edgeOne) {
    deque<Node> queOne;
    int i,num;

    while(!heapOne.empty()) {
        now = heapOne.top();
        heapOne.pop();
        num = now.du;

        if(num > heapOne.size()) {
            return false;
        }

        for(i=0; i<num; i++) {
            queOne.push_back(heapOne.top());
            heapOne.pop();
        }

        if(ok && !heapOne.empty()) {
            ok = false;
            heapTwo = heapOne;
            deque<Node> queTwo = queOne;
            edgeTwo = edgeOne;

            heapNode = heapTwo.top();
            heapTwo.pop();

            dequeNode = queTwo.back();
            queTwo.pop_back();

            heapTwo.push(dequeNode);
            queTwo.push_back(heapNode);

            while(!queTwo.empty()) {
                dequeNode = queTwo.back();
                queTwo.pop_back();
                edgeTwo.push_back(Edge(now.pos, dequeNode.pos));
                if(dequeNode.du > 1) {
                    dequeNode.du--;
                    heapTwo.push(dequeNode);
                }
            }
            if(!getAns(heapTwo, edgeTwo)) {
                ok = true;
            }
        }
        while(!queOne.empty()) {
            dequeNode = queOne.back();
            queOne.pop_back();
            edgeOne.push_back(Edge(now.pos, dequeNode.pos));
            str[now.pos][dequeNode.pos] = str[dequeNode.pos][now.pos] = 1;
            if(dequeNode.du > 1) {
                dequeNode.du--;
                heapOne.push(dequeNode);
            }
        }

    }
    return true;
}
int main(){
    int i;
    while(scanf("%d%d", &n, &k) == 2){
        for(i = 0; i < n; i++){
            scanf("%d", &a[i].d);
            a[i].p = i;
        }
        if(n <= k){
            for(i = 0; i < n; i++)
                pq1.push(a[i]);
            win b = pq1.top();
            printf("%d\n", b.d); 
            while(!pq1.empty())
                pq1.pop();
            for(i = 0; i < n; i++)
                pq2.push(a[i]);
            win c = pq2.top();
            printf("%d\n", c.d);
            continue;
        }
        for(i = 0; i < n; i++){
            if(i > k - 1){
                win b = pq1.top();
                while(b.d <= a[i].d && b.p <= i - k){
                    pq1.pop();
                    b = pq1.top();
                }
            }
            pq1.push(a[i]);
            if(i < k - 1)
                continue;
            win c = pq1.top();
            printf("%d ", c.d);
        }
        printf("\n");
        while(!pq1.empty())
            pq1.pop();
        for(i = 0; i < n; i++){
            if(i > k - 1){
                win b = pq2.top();
                while(b.d >= a[i].d && b.p <= i - k){
                    pq2.pop();
                    b = pq2.top();
                }
            }
            pq2.push(a[i]);
            if(i < k - 1)
                continue;
            win c = pq2.top();
            printf("%d ", c.d);
        }
        printf("\n");
    }
    return 0;
}
Example #28
0
int main(int argc,char *argv[])										
{
	int in_file;													//Input file position.
	int out_file;													//Output file position.
	int sel;														//0 is to compress, 1 is to uncompress.
	for(int i=1;i<argc;i++)											//Use argument
	{
		if(strcmp(argv[i],"-i")==0)
			in_file = i+1;
		else if(strcmp(argv[i],"-o")==0)
			out_file = i+1;
		else if(strcmp(argv[i],"-u")==0)
			sel = 1;
		else if(strcmp(argv[i],"-c")==0)
			sel = 0;
	}
	if(!sel)
	{
		FILE *file1;
		FILE *file2;
		
		long filelen;
		file1 = fopen(argv[in_file],"rb");
		file2 = fopen(argv[out_file],"wb");
		clock_t t;
		t = clock();
		
		cout<<"Start to compress...";
		
		fseek(file1,0,SEEK_END);									//Jump to the end of the file
		filelen = ftell(file1);										//Calculate the number of bytes of the file.
		rewind(file1);												//Jump back to the begining of the file.
		
																	//Give the enough memory to the pointer.
		un_code = (unsigned char *)malloc((filelen+1)*sizeof(unsigned char)); 	
		
		fread(un_code,filelen,1,file1);								//fread will read all of the characters in the file.

		
		for(int i=0;i<filelen;i++)									//Count the appearing times.
			count[un_code[i]]++;
		
		fclose(file1);
		
		create_p_queue();											//Create priority queue.
		
		build_huffman_tree();										//Build the huffman tree.
			
		
		char str[9];
		huffman_encoding(p_queue.top(),str,0);						//Build encoding table.
		
		
	
		bit_code = (char*)malloc(filelen*8);
		int bit_size = bit_with_encoding(filelen);
		
		
		
		com_code = (unsigned char*)malloc((bit_size+1)*sizeof(unsigned char));
		int now = compress_data(bit_size);
		
																	//Before compress.
		cout<<"\nThe number of bytes before compress : "<<filelen<<endl;
		fprintf(file2,"The number of bytes before compress : %d\n",filelen);
																	//After compress.
		cout<<"The number of bytes after compress : "<<now<<endl;
		fprintf(file2,"The number of bytes after compress : %d\n",now);	
																	//Compression rate.
		cout<<"The compression rate : "<<(filelen-now)/(filelen*1.0)*100<<"%"<<endl;
		fprintf(file2,"The compression rate : %0.4f%\n",(filelen-now)/(filelen*1.0)*100);
		
																	//Print encoding table.
		fprintf(file2,"Encoding table\n");
		cout<<"Encoding table"<<endl;
		
		map<int,array>::iterator it2;
		for(it2=encode_table.begin();it2!=encode_table.end();it2++)
		{
			cout<<it2->first<<" : "<<it2->second.a<<endl;
			fprintf(file2,"%d : %s\n",it2->first,it2->second.a);
		}
		
		
		fprintf(file2,"\n");
		fprintf(file2,"%d",exceed);									//Record the excessive binay code.
		fwrite(com_code,sizeof(com_code[0]),now,file2);
		fclose(file2);
		t = clock() - t;
		
		printf("Spending time : %f",((float)t)/CLOCKS_PER_SEC);
	
	}
	else
	{
		FILE *file3;
		FILE *file4;
		
		file3 = fopen(argv[in_file],"rb");
		
		char str[128];
		
		cout<<"Start to uncompress..."; 
		
		clock_t t;
		
		t = clock();
		int com_data_size;											//Compression data size
		int origin_data_size;										//Origin data size
		
		for(int i=0;i<4;i++)		
		{				
			fgets(str,128,file3);
			if(i==0 || i==1)										//Inorder to find the compression data size.
			{
				int j=0;
				while(str[j]!=':')
					j++;
				j+=2;
				char com_size[128];
				int k = 0;
				for(j;str[j]!='\n';j++)
				{
					com_size[k] = str[j];
					k++;
				}
				if(i==1)	
					com_data_size = atoi(com_size);					//At the end of the second line is the compression data size
				else 
					origin_data_size = atoi(com_size);				//At the end of the first line is origin data size.
			}
		}
		
			
		while(fgets(str,128,file3))
		{
			char number[4];
			if(str[0]=='\n')
				break;
			int i=0;
			for(i=0;str[i]!=' ';i++)
				number[i] = str[i];
			int num = atoi(number);									//Code index get! Convert char array to integer.
			
			i+=3;
			int k = 0;												//Record the encode in encode_table.
			for(i;str[i]!='\n';i++)
			{
				encode_table[num].a[k++] = str[i];
				encode_table[num].size = k;
			}
		}
		
		fscanf(file3,"%d",&exceed);									//Record the exceesive bianry code.
		
		com_code = (unsigned char*)malloc(com_data_size*sizeof(unsigned char));
		fread(com_code,com_data_size,1,file3);						//Read remain data in the file.		
		
		bit_code = (char *)malloc(com_data_size*8);
		
		int bit_size = com_to_bit(com_data_size);					//Convert compresstion data to binary code.
		
		un_code = (unsigned char *)malloc((origin_data_size+1)*sizeof(unsigned char));		
		
		
		int now = umcompress_data(bit_size);						//Umcompress data.
		
		file4 = fopen(argv[out_file],"wb");							//Write the file in byte mod.
		
		fwrite(un_code,sizeof(un_code[0]),now,file4);
		
		fclose(file3);
		fclose(file4);
		
		cout<<"\nFinished!!"<<endl;
		t = clock()-t;
		printf("Spending time : %f",((float)t)/CLOCKS_PER_SEC);
	}
	
	return 0;
} 
Example #29
0
int main1057stack()
{
	int count;
	cin >> count;
	char* inputstr = new char[20];
	while (count-- > 0)
	{
		scanf("%s",inputstr);
		if (strcmp(inputstr, "PeekMedian")==0)
		{
			if (sta.empty())
			{
				printf("Invalid\n");
			}
			else
			{
				int temp;
				int median = (pq.size()+1)/2;
				while (--median)
				{
					temp = pq.top();
					stacktemp.push(temp);
					pq.pop();
				}
				printf("%d\n", pq.top());
				while (!stacktemp.empty())
				{
					temp = stacktemp.top();
					stacktemp.pop();
					pq.push(temp);
				}
			}
		}
		else if (strcmp(inputstr, "Pop") == 0)
		{
			int num,temp;
			if (sta.empty())
			{
				printf("Invalid\n");
			}
			else
			{
				num = sta.top(); 
				sta.pop();
				printf("%d\n",num);
				while (num != pq.top())
				{
					temp = pq.top();
					stacktemp.push(temp);
					pq.pop();
				}
				pq.pop();
				while (!stacktemp.empty())
				{
					temp = stacktemp.top();
					stacktemp.pop();
					pq.push(temp);
				}
			}
		}
		else if (strcmp(inputstr, "Push") == 0)
		{
			int num;
			scanf("%d", &num);
			sta.push(num);

			pq.push(num);
		}
		else
		{
			assert(false);
		}
	}
	return 0;
}
Example #30
0
void opepush(int x){
    if (sc) s.push(x);
    if (pc) p.push(x);
    if (qc) q.push(x);
}