首页 > 其他分享 >CF1857D Strong Vertices

CF1857D Strong Vertices

时间:2023-11-22 21:44:53浏览次数:26  
标签:geq int Vertices CF1857D MAXN Strong

CF1857D Strong Vertices

Strong Vertices - 洛谷

题解是个好东西

题意

给定两个数组 $a$ 和 $b$,对此构造一张有向图:

  • 若 $a_u−a_v≥b_u−b_v$,则 $u$ 向 $v$ 连边。

求所有向其他所有顶点连边的顶点个数,并按从小到大顺序输出它们。

思路

先对原式进行转换:$a_u-b_u\geq a_v-b_v$

接着,由$a>b$且$b>c$,则$a>c$得到,$a$和$b$连接,$b$和$c$连接,但是$c$不能和$a$连接。故$a,b$两个数列一定无环,也就是说$a$的入度为$0$

在考虑$a=b$的情况,这是就是$a$和$b$之间有一条无向边相连。

综上,得到结论:当一点除了所有与之相连的无向边之外入度为$0$,该点符合题目要求(即称作Strong Vertices)

再根据最初的$a_u-b_u\geq a_v-b_v$,推出,我们只需要找到$a_i-b_i$的最大值,再算出该最大值的出现数量即可。

其实这道题和图基本上八竿子打不着,最多在推导时有点用

代码

#include<bits/stdc++.h>
using namespace std;
const int MAXN=2e5;
int maxn,n,a[MAXN],b[MAXN];
vector<int>ans;
int run()
{
	cin>>n;maxn=-2e9;ans.clear();
	for(int i=0;i<n;i++) cin>>a[i];
	for(int i=0;i<n;i++) cin>>b[i];
	for(int i=0;i<n;i++) maxn=max(maxn,a[i]-b[i]);
	for(int i=0;i<n;i++) if(a[i]-b[i]==maxn) ans.push_back(i+1);
	cout<<ans.size()<<endl;
	for(int i=0;i<ans.size();i++) cout<<ans[i]<<" ";
	cout<<endl;
	return 0;
}
int main()
{
	int t;
	cin>>t;
	while(t--) run();
	return 0;
}

(好整齐的for

标签:geq,int,Vertices,CF1857D,MAXN,Strong
From: https://www.cnblogs.com/lyk2010/p/17850362.html

相关文章

  • 在ARC(自动引用计数)下,IBOutlets 应该是强引用(strong)还是弱引用(weak)?
    内容来自DOChttps://q.houxu6.top/?s=在ARC(自动引用计数)下,IBOutlets应该是强引用(strong)还是弱引用(weak)?我正在使用ARC(自动引用计数)专门为iOS5进行开发。在这种情况下,指向UIView(及其子类)的IBOutlet应该是strong还是weak?以下是示例:@property(nonatomic,weak)IBO......
  • Apple开发_NSString 使用 strong 与 copy 进行修饰的区别
    测试代码-(BOOL)application:(UIApplication*)applicationdidFinishLaunchingWithOptions:(NSDictionary*)launchOptions{NSMutableString*m_notiion=[[NSMutableStringalloc]init];m_notiion.string=@"房号密码的功能";self.m_notiion=m......
  • D. Strong Vertices
    D.StrongVertices条件转移一下即可由a[u]−a[v]≥b[u]−b[v],可得a[u]-b[u]>=a[v]-b[v]。设c[i]=a[i]-b[i],由题意得只要c[i]>=cj,点i就有指向j的路。因此题目就转化成:求c数组中最大元素的个数及其位置。点击查看代码#include<bits/stdc++.h>usingnamespacestd;#define......
  • [React Typescript] Strongly type Shared props for multiple components (React.FC<
    import{Equal,Expect}from"../helpers/type-utils";typeInputProps=React.ComponentProps<"input">;constCOMPONENTS={text:(props)=>{return<input{...props}type="text"/>;},number:(p......
  • [React Typescript] Strongly type Render prop
    1.React.ReactNodeimport{useState}from"react";import{createPortal}from"react-dom";import{Equal,Expect}from"../helpers/type-utils";interfaceModalChildProps{isOpen:boolean;openModal:()=>void;......
  • [React Typescript] Strongly Typing Lazy Loaded Components with Generics
    Navigatingtothetypedefinitionfor lazy by CMD+click inlocalVSCode,orinthe DefinitelyTyped repo.Wecanseethefollowingdefinition:functionlazy<TextendsComponentType<any>>( factory:()=>Promise<{default:T}>):L......
  • Codeforces 1857D:Strong Vertices 与图论无关的出度最大统计
    1857D.StrongVerticesDescription:给定两个长度均为\(n\)的数组\(a\)和\(b\)(编号\(1\)~\(n\)),如果\(a_u-a_v\geqb_u-b_v\)\((u\neqv)\),那么从\(u\)到\(v\)建立一条有向边。"Strong"定义为:一个点\(V\)可以经过有向图中合法的通路到达其他所有的点。请求解出"......
  • Educational 151 DIV2 T3 strong password
    T3strongpassword就是对于输入的每一个\(l,r\),我们遍历\(s[l]~s[r]\),对于每次遍历,我们设置一个临时指针\(cur\),然后通过指针右移寻找所需要的值在外面我们弄两个指针,分别代表每次遍历\([l,r]\)的区间的指针\(nmx\)和全局指针\(mx\)我们用临时指针更新单次区间的......
  • [ARC162D] Smallest Vertices
    [ARC162D]SmallestVerticesAtcoder:[ARC162D]SmallestVertices洛谷:[ARC162D]SmallestVerticesProblem在本问题中,当我们提到有根有向树时,我们指的是所有边都指向从根到叶子的有根树。给定一个使得其总和为\(N-1\)的非负整数序列\(d=(d_1,d_2,\ldots,d_N)\)。对于带编......
  • Strong Password(贪心思想)
    StrongPasswordtimelimitpertest2secondsmemorylimitpertest256megabytesinputstandardinputoutputstandardoutputMonocarpfinallygotthecouragetoregisteronForceCoders.Hecameupwithahandlebutisstillthinkingaboutthepassw......