首页 > 其他分享 >cf797eE. Array Queries(暴力+复杂度分析)

cf797eE. Array Queries(暴力+复杂度分析)

时间:2023-11-06 20:11:19浏览次数:34  
标签:head now int 复杂度 Queries Array include id define

cf797e

还是暴力,将不同的询问根据k分开,然后bfs,建出一棵树,然后dfs。
时间复杂度:O(能过)
稍微口胡分析一下

大概是

\(min(1,q[1])*n/1 +min(2.q[2])*n/2+min(3,q[3])*n/3+.....\)
qi表示第k=i的询问个数
因为每一种k它最多将所有的a分成k类,如果全部满了,就是n,那么显然是尽量分配给前面的使得复杂度最大。

大约是\(O(n\sqrt{n})\)级别

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<map>
#include<vector>
#include<set>
#include<ctime>
#include<unordered_map>
#include<queue>
#include<bitset>
#define A puts("YES")
#define B puts("NO")
#define fo(i,a,b) for (int (i)=(a);(i)<=(b);(i)++)
#define fd(i,b,a) for (int (i)=(b);(i)>=(a);(i)--)
#define mk(x,y) make_pair((x),(y))
#define eb(x) .emplace_back(x)
#define lc (o<<1)
#define rc (o<<1|1)
using namespace std;
//typedef __int128 i128;
typedef double db;
typedef long long ll;
const ll mo=1e9+7;
const int N=1e5+5;
struct node{
	int p,id;
};
bool operator <(const node &a,const node& b){
	return a.p<b.p;
}
vector<node> v[N];
int n,a[N],m;
node c[N];
queue<int> q;
int vis[N],ans[N],p,k,x,y,f[N];
int head[N],to[N],nex[N],tot,now;
int bz[N];
int b[N],h;
void add(int x,int y){
	if (bz[x]!=now) {
		head[x]=0;
		bz[x]=now;
	}
	if (bz[y]!=now) {
		head[y]=0;
		bz[y]=now;
	}
	to[++tot]=y; nex[tot]=head[x]; head[x]=tot;
}
void dfs(int x){
	for (int i=head[x];i;i=nex[i]){
		int v=to[i];
		f[v]=f[x]+1;
		dfs(v);
	}
}
int main()
{
//	freopen("data.in","r",stdin);
//	freopen("data.out","w",stdout);
	

	scanf("%d",&n);
	fo(i,1,n) scanf("%d",&a[i]);
	
	scanf("%d",&m);
	fo(i,1,m) {
		scanf("%d %d",&p,&k);
		v[k].push_back((node){p,i});
	}
	
	fo(id,1,n) { //
	
		if (!v[id].size()) continue;
		now=id;
		
		tot=0;
		for (node i:v[id]){
			if (vis[i.p]!=id) {
				vis[i.p]=id;
				q.push(i.p);
			}
		}

		while (!q.empty()) {
			x=q.front(); q.pop();
			y=x+a[x]+id;
			if (y>n) {
				add(n+1,x);
				continue;
			}
			add(y,x);
			if (vis[y]!=id) {
				vis[y]=id;
				q.push(y);
			}
		}
		
		dfs(n+1);
		for (node i:v[id]) {
			ans[i.id]=f[i.p];
		}
		
	}
	
	fo(i,1,m) printf("%d\n",ans[i]);
	return 0;
}

 
 

标签:head,now,int,复杂度,Queries,Array,include,id,define
From: https://www.cnblogs.com/ganking/p/17813600.html

相关文章

  • Python数据类型bytes 和 bytearray
    bytes和bytearray都是二进制世界的成员,用二进制的方式去理解才能看清他的本质。理解bytes和bytearray0和1是计算机工作的根本,单个的0和1只能表达两种状态,无法满足我们复杂的计算,于是计算机使用了8位即一个byte作为一个储存的基本单位。byte由8bit组成,例如   0000......
  • [LeetCode] 1535. Find the Winner of an Array Game
    Givenanintegerarrayarrofdistinctintegersandanintegerk.Agamewillbeplayedbetweenthefirsttwoelementsofthearray(i.e.arr[0]andarr[1]).Ineachroundofthegame,wecomparearr[0]witharr[1],thelargerintegerwinsandremainsat......
  • JSONArray 分页
    publicJSONArraydatePageSize(IntegerpageNo,IntegerpageSize,JSONArraydata){JSONArraynewDate=newJSONArray();Integercounts=data.size();//获取数据总数Integerstart=(pageNo-1)*pageSize;//获取开始值Integerend=(pageNo)*p......
  • [LeetCode] 2149. Rearrange Array Elements by Sign
    Youaregivena0-indexedintegerarraynumsofevenlengthconsistingofanequalnumberofpositiveandnegativeintegers.Youshouldrearrangetheelementsofnumssuchthatthemodifiedarrayfollowsthegivenconditions:Everyconsecutivepairofint......
  • JavaScript Array对象(属性、方法) 留言板案例
    一、创建数组对象的方式vararrOb=newArray(值,........)vararrOb=Array(值,.......)vararrOb=[值,.........]vararrOb=newArray(n);arrOb[0]=值1;arrOb[1]=值2;二、数组的属性length   //数组中元素的数目vararr=['云南','九寨沟','拉萨','西双版纳','......
  • ArrayLsit
    ArrayList本质上就是数组,但是其特点是没有大小限制(定义时不规定大小)。 ArrayList的构造方法: ArrayList的方法:  常见方法使用的代码示例:importjava.util.ArrayList;publicclassMain{publicstaticvoidmain(String[]args){ArrayList<Str......
  • HanLP — Aho-Corasick DoubleArrayTire 算法 ACDAT - 基于双数组字典树的AC自动机
    双数组字典树能在O(1)(1是模式串长度)时间内高速完成单串匹配,并且内存消耗可控,然而软肋在于多模式匹配。如果要匹配多个模式串,必须先实现前缀查询,然后频繁截取文本后缀才可多匹配。比如ushers、shers、hers…这样一份文本要回退扫描多遍,性能较低。既然AC自动机的goto表本身就是一......
  • Hive / ClickHouse 行转列函数 collect_set() / groupUniqArray() 入门
    Hive/ClickHouse行转列函数collect_set()/groupUniqArray()入门在数据处理和分析中,我们经常会遇到需要将一行数据转换为多列的情况。在Hive和ClickHouse中,可以使用collect_set()和groupUniqArray()函数来实现行转列操作。collect_set()1.功能说明collect_set()函......
  • CF911G Mass Change Queries
    CF911GMassChangeQueries题解首先这题有一个很一眼的分块做法,并且由于只需要维护颜色,所以会极其好写。对每个块维护并查集,表示整块中颜色变成了哪个颜色,每个位置单独也指向一个颜色表示最初指向哪个颜色,这样就很好维护了。但是发现值最大只有\(100\),所以考虑和值相关的做......
  • Sasha and Array 题解
    SashaandArray题目大意给定一个长为\(n\)的序列\(a\),支持以下操作:\(\foralli\in[l,r],a_i\getsa_i+x\)。求\(\left(\sum\limits_{i=l}^{r}F_{a_i}\right)\bmod(10^9+7)\),其中\(F\)表示斐波那契数列,即有\(F_1=1,F_2=1,\foralli>2,F_i=F_{i-1}+F_{i-2}\)。......