首页 > 其他分享 >Buy Tickets

Buy Tickets

时间:2024-03-08 11:46:59浏览次数:25  
标签:Tickets Buy val int queue person include was

Buy Tickets

  • Railway tickets were difficult to buy around the Lunar New Year in China, so we must get up early and join a long queue…
    The Lunar New Year was approaching, but unluckily the Little Cat still had schedules going here and there. Now, he had to travel by train to Mianyang, Sichuan Province for the winter camp selection of the national team of Olympiad in Informatics.
    It was one o’clock a.m. and dark outside. Chill wind from the northwest did not scare off the people in the queue. The cold night gave the Little Cat a shiver. Why not find a problem to think about? That was none the less better than freezing to death!
    People kept jumping the queue. Since it was too dark around, such moves would not be discovered even by the people adjacent to the queue-jumpers. “If every person in the queue is assigned an integral value and all the information about those who have jumped the queue and where they stand after queue-jumping is given, can I find out the final order of people in the queue?” Thought the Little Cat.

  • Input
    There will be several test cases in the input. Each test case consists of N + 1 lines where N (1 ≤ N ≤ 200,000) is given in the first line of the test case. The next N lines contain the pairs of values Posi and Vali in the increasing order of i (1 ≤ i ≤ N). For each i, the ranges and meanings of Posi and Vali are as follows:

    • Posi ∈ [0, i − 1] — The i-th person came to the queue and stood right behind the Posi-th person in the queue. The booking office was considered the 0th person and the person at the front of the queue was considered the first person in the queue.
    • Vali ∈ [0, 32767] — The i-th person was assigned the value Vali.
      There no blank lines between test cases. Proceed to the end of input.
  • Output
    For each test cases, output a single line of space-separated integers which are the values of people in the order they stand in the queue.

    这题可以用线段树,也可用树状数组
    需要逆向思维,倒着看,因为最后一个往往是确定的

image

val存的是在这一段中有几个空位

image

例如存放一个2 848439这一组数

image

线段树代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
int n;
int ans[200005];
const int N = 200010;
struct ac
{
	int x,val;
}a[N];
struct node
{
    int id,st,en,val;
}st[N<<2];
void bt(int rt,int L,int R)
{
    st[rt].st = L;
    st[rt].en = R;
    st[rt].val = R - L +1;
    if(L == R) return ;
    int mid = (L + R)>>1;
    bt(rt<<1, L, mid);
    bt(rt<<1|1, mid+1, R);
}
int query(int rt,int pos)
{
    st[rt].val--;
    if(st[rt].st == st[rt].en) return st[rt].st;
    if(pos <= st[rt<<1].val) return query(rt<<1, pos);
    else return query(rt<<1|1, pos - st[rt<<1].val);
}
int main()
{
//	ios_base::sync_with_stdio(false);
//	cin.tie(0);cout.tie(0);
	while(~scanf("%d",&n))
	{
		for(int i=1;i<=n;i++)
		{
			scanf("%d%d",&a[i].x,&a[i].val);
		}
		bt(1,1,n);
		for(int i=n;i>=1;i--)
		{
			int t=query(1,a[i].x+1);
			ans[t]=a[i].val;
			
		}
		for(int i=1;i<=n;i++)
		{
			printf("%d ",ans[i]);
		}
		putchar('\n');
	}
	return 0;
}

空位初始值为1,有人则初始值为0.所以我们要求前缀和为p[n]的最小值。树状数组可以在O(logn)时间求出前缀和,然后我们只需要二分查找即可,时间也是O(logn).

树状数组
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
int n;
int c[200005],ans[200005];
struct ac
{
	int x,val;
}a[200005];
int lowbit(int x)
{
	return x&(-x);
}
void update(int x,int val)
{
	while(x<=n)
	{
		c[x]+=val;
		x+=lowbit(x);
	}
}
int ask(int x)
{
	int ans=0;
	while(x)
	{
		ans+=c[x];
		x-=lowbit(x);
	}
	return ans;
}
int find(int x)
{
	int l=1,r=n;
	while(l<=r)
	{
		int mid=(l+r)>>1;
		if(ask(mid)<x)
		{
			l=mid+1;
		}else
		{
			r=mid-1;
		}
	}
	return l;
}
int main()
{
	while(~scanf("%d",&n))
	{
		memset(c,0,sizeof(c));
		for(int i=1;i<=n;i++)
		{
			scanf("%d%d",&a[i].x,&a[i].val);
			update(i,1);
		}
//		bt(1,1,n);
		for(int i=n;i>=1;i--)
		{
			int t=find(a[i].x+1);
			ans[t]=a[i].val;
			update(t,-1);
		}
		for(int i=1;i<=n;i++)
		{
			printf("%d ",ans[i]);
		}
		putchar('\n');
	}
	return 0;
}

标签:Tickets,Buy,val,int,queue,person,include,was
From: https://www.cnblogs.com/wlesq/p/18060610

相关文章

  • CF103E Buying Sets(最大权闭合子图模型)
    题意简述有\(n\)个元素和\(n\)个集合,保证任意\(k\)个集合的并\(\gek\)。每个集合有权值\(a_i\),你需要选出一些集合使得集合数等于集合并大小,并在此基础上最小化选出的集合权值和。\(n\le300,|a_i|\le10^6\)。分析将集合和元素看成物品,我们发现,若选择了一个集合,则......
  • 初中英语优秀范文100篇-044Can Money Buy Happiness?钱能买到幸福?
    PDF格式公众号回复关键字:SHCZFW044记忆树1Canmoneybuyhappiness?翻译钱能买到幸福吗简化记忆幸福句子结构主语:money(金钱)谓语:canbuy(能够购买)宾语:happiness(幸福)这是一个陈述句,谓语动词"canbuy"表达了金钱的购买能力。宾语"happiness"指的是幸福。整个句子在语......
  • MongoDB 7.0 动态 WiredTiger tickets
    在WiredTiger存储引擎中,WiredTigertickets提供了并发控制机制。这些tickets分为读tickets和写tickets。当多个操作,比如读和写尝试并发访问数据库,WiredTiger使用tickets来确保这些操作不会冲突,从而保证数据的完整性和性能。WiredTiger中的"tickets"实际上是一种资源管理机制,用于限......
  • B - Buy One Carton of Milk
    B-BuyOneCartonofMilkhttps://atcoder.jp/contests/abc331/tasks/abc331_b 思路dfs递归搜索,按照依此按照三种策略:6个一打-costS8个一打-costM12个一打-costL 递归到叶子节点终止条件为,总的cost超过预算N,记录此时花费,更新mincost Codehttps://a......
  • [极客大挑战 2019]BuyFlag 1(两种解法)
    题目环境:<br/><br/><br/>FLAGNEEDYOUR100000000MONEYflag需要你的100000000元F12瞅瞅源代码:<br/>if(isset($_POST['password'])){$password=$_POST['password'];if(is_numeric($password)){echo"pas......
  • [极客大挑战 2019]BuyFlag
    打开网页,发现右手边的菜单中有个PAYFLAG,打开后跳转到pay.php中,其中网页源代码有提示,主要内容如下:IfyouwanttobuytheFLAG:YoumustbeastudentfromCUIT!!!Youmustbeanswerthecorrectpassword!!!OnlyCuit'sstudentscanbuytheFLAG ~~~postmoneyand......
  • [CF335F] Buy One,Get One Free
    气死我了,我决定水了这篇题解。反悔贪心,考虑决策及反悔,记到第三层反悔就行。然后你发现要一次只考虑一个不行,要两个两个考虑,然后就做完了,如果深入往下分析能分析出更多可以简化做法的结论。甚至可以简化到只用一层反悔,具体就是第一层可以简化到只记数量,第三层分析出可以归成第二......
  • 「BZOJ2505」tickets 题解
    preface网上目前还没看到我的方法,就大概讲一下做法solution首先想到贪心,考虑\([l,r]\)的最大次数,一定是找到最小的\(x\)满足\(l\simx\)的位数的和大于等于\(k\),然后递归的求解\([x+1,r]\),易证。还是考虑将\(Query(l,r)\)拆分成\(Query(1,r)\)和\(Query......
  • [极客大挑战 2019]BuyFlag
    原理弱比较问题科学计数法绕过cookie字段的修改解题过程进入靶场,页面没什么提示,就先看下原代码吧看到有两个超链接,现在这个页面是index.php,看一下flag.php有这几个提示,要满足两个条件咯,再看下原代码发现有后端源码,也就是说要传入password和money,这里的源码需要password......
  • buy
    BuyLowSellHigh考虑反悔贪心。对于三个股票\(i,j,k,p_i<p_j<p_k\),我们可以在发现有利可图时立刻卖掉\(p_i\),然后插入\(p_j\)(用来反悔),然后再插入一个\(p_j\)(真正用来交易),下次遇到\(p_k\),我们搞用来反悔的\(p_j\),发现\((p_k-p_j)+(p_j-p_i)=p_k-p_i\),符合要求。注意由......