B. pSort

题目链接:

http://codeforces.com/problemset/problem/28/B

题意:

给一个n,原本有个1到n按顺序排列的序列,给一个序列问,在给一个数组,表示这个位置的数可以换到pi位置,问能不能实现给的那个序列的排列。如果可以输出yes相反输出no

思路:

通过画图可知同一个集合的数字一定可以相互交换位置。所以这里使用并查集就好。

代码:

 1 #include<bits/stdc++.h>
 2 #define LL long long
 3 
 4 using namespace std;
 5 const int maxn=100+100;
 6 struct node
 7 {
 8     int to,Next;
 9 };
10 int Head[maxn];
11 node Edge[maxn*2];
12 int cnt=0;
13 int a[maxn],b[maxn],Fa[maxn];
14 void add(int u,int v)
15 {
16     Edge[++cnt].to=v;
17     Edge[cnt].Next=Head[u];
18     Head[u]=cnt;
19     return ;
20 }
21 int Find(int x)
22 {
23     if(x==Fa[x])
24     {
25         return x;
26     }
27     else
28     {
29         return Fa[x]=Find(Fa[x]);
30     }
31 }
32 int main()
33 {
34     int n;
35     cin>>n;
36     for(int i=1;i<=n;i++)
37     {
38         Fa[i]=i;
39     }
40     for(int i=1;i<=n;i++)
41     {
42         cin>>a[i];
43     }
44     for(int i=1;i<=n;i++)
45     {
46         cin>>b[i];
47     }
48     for(int i=1;i<=n;i++)
49     {
50         if(i+b[i]<=n||i-b[i]>=1)
51         {
52             int root1=Find(i);
53             if(i+b[i]<=n)
54             {
55                 int root2=Find(i+b[i]);
56                 if(root1!=root2)
57                 {
58                     Fa[root2]=root1;
59                 }
60             }
61             if(i-b[i]>=1)
62             {
63                 int root2=Find(i-b[i]);
64                 if(root1!=root2)
65                 {
66                     Fa[root2]=root1;
67                 }
68             }
69         }
70     }
71     int flag=1;
72     for(int i=1;i<=n;i++)
73     {
74         int root1=Find(a[i]);
75         int root2=Find(i);
76         if(root1!=root2)
77         {
78             //cout<<i<<" "<<a[i]<<endl;
79             flag=0;
80             break;
81         }
82     }
83     if(flag)
84     {
85         cout<<"YES"<<endl;
86     }
87     else
88     {
89         cout<<"NO"<<endl;
90     }
91     return 0;
92 }
View Code

 

转载于:https://www.cnblogs.com/dx666/p/dxnb667.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
import time import openpyxl from selenium import webdriver from bs4 import BeautifulSoup # 设置请求头,模拟真实浏览器访问 headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36', } # 使用Selenium启动浏览器 driver = webdriver.Chrome() data = [] counter = 1 # 序列号计数器 # 打开网页 for i in range(1,6): url = 'https://search.jd.com/Search?keyword=%E6%89%8B%E6%9C%BA&psort=3&wq=%E6%89%8B%E6%9C%BA&psort=3&pvid=0faa3ec65d444d68a66161cdf464d451&psort=3&page={}&s=61&click=0'.format( (i * 2) - 1) driver.get(url) # 模拟滚动页面,以触发异步请求加载更多商品信息 driver.execute_script('window.scrollTo(0, document.body.scrollHeight);') time.sleep(2) # 获取完整页面内容 html = driver.page_source # 解析网页内容,提取商品名称和价格信息 soup = BeautifulSoup(html, 'html.parser') products = soup.select('.gl-item') for product in products: product_id = product['data-sku'] # 提取产品ID name = product.select('.p-name em')[0].text.strip() product_url = 'https:' + product.select('.p-name a')[0]['href'] # 修改产品URL price = product.select('.p-price strong i')[0].text.strip() data.append([counter, product_id, name, product_url, price]) # 将产品数据添加到列表中 counter += 1 # 每个产品的增量计数器 # 关闭浏览器 driver.quit() # 创建Excel文件并保存数据 wb = openpyxl.Workbook() ws = wb.active ws.append(['top', '商品ID', '商品名称', '商品链接', '价格']) # 添加已修改列顺序的标题行 for item in data: ws.append(item) wb.save('jd_top300.xlsx') print("数据已保存到jd_top300.xlsx文件。")
06-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值