首页 > 其他分享 >Codeforces Round 875 (Div. 2)B-D

Codeforces Round 875 (Div. 2)B-D

时间:2023-05-31 12:46:40浏览次数:58  
标签:std idx int 875 Codeforces cin add Div include

原题链接:https://codeforces.com/contest/1831

原文:https://www.cnblogs.com/edgrass/p/17440602.html

(B) Array merging

主体思想是找到ab数组的最长相同字串(c中操作可实现连续)

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int main(){
 4     int t;
 5     cin>>t;
 6     while(t--){
 7         int n;
 8         cin>>n;
 9         vector<int>a(n+1);
10         vector<int>b(n+1);
11         for(int i=1;i<=n;i++)cin>>a[i];
12         for(int i=1;i<=n;i++)cin>>b[i];
13         vector<int>ma(n+1+n);
14         vector<int>mb(n+1+n);
15         int q=1;
16         for(int i=2;i<=n;++i){
17             if(a[i]!=a[i-1]){ma[a[i-1]]=max(ma[a[i-1]],(i-q));
18             q=i;
19                 }        }
20          ma[a[n]] = max(ma[a[n]], n - q + 1);
21          q=1;
22          for(int i=2;i<=n;i++){
23             if(b[i]!=b[i-1]){mb[b[i-1]]=max(mb[b[i-1]],(i-q));
24             q=i;}
25         }
26          mb[b[n]] = max(mb[b[n]], n - q + 1);
27          int ans=0;
28          for(int i=1;i<=n+n;i++){
29             ans=max(ans,ma[i]+mb[i]);
30          }
31          cout<<ans<<endl;
32     }
33 }

(C) Copil Copac Draws Trees

根据编号判断

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int N=901100;
 4 int h[N],e[N],ne[N],w[N],idx;
 5 bool st[N];
 6 int mapp[N];
 7 void add(int a,int b,int c){
 8     e[idx]=b;w[idx]=c;ne[idx]=h[a];h[a]=idx++;
 9 }
10 void dfs(int a,int wn){
11     st[a]=true;
12     int wa;
13     for(int i=h[a];i!=-1;i=ne[i]){
14         int j=e[i],wa=w[i];
15         if(!st[j]){
16             if(wa<wn)mapp[j]=mapp[a]+1;
17             else mapp[j]=mapp[a];
18             dfs(j,wa);
19         }
20     }
21 }
22 int main(){
23     int t;
24     cin>>t;
25     while(t--){
26     idx=0;
27     memset(h, -1, sizeof h);
28     int n;
29     cin>>n;
30     for(int i=1;i<=n;i++)st[i]=false;
31     int ans=0;
32     for(int i=1;i<n;i++){
33         int a,b,c;
34         cin>>a>>b;
35         add(a,b,i);
36         add(b,a,i);
37     }
38     dfs(1,0x3f3f3f3f);
39     for(int i=1;i<=n;i++){
40         ans=max(ans,mapp[i]);
41     }
42     cout<<ans<<endl;
43     }
44 }

(D) The BOSS Can Count Pairs

#include<bits/stdc++.h>//b[i]*b[j]<=2n
#define PII pair<int,int>
#define int long long
using namespace std;
const int N=2e5+10;
PII p[N];//一定要写成全局变量
int ans=0;
signed main(){
    int t;
    cin>>t;
    while(t--){
        int n;
        ans=0;
        cin>>n;
        int a[N];
        for(int i=1;i<=n;i++){
            cin>>a[i];
        }       
        for(int i=1;i<=n;i++){
            int x;
            cin>>x;
            p[i]={a[i],x};
        }
        sort(p+1,p+1+n);
        for(int i=1;i*i<=n*2;i++){
            vector<int> a(n+1);
            for(int j=1;j<=n;j++){
                int num=i*p[j].first-p[j].second;//题中的关系式
                if(num>=1&&num<=n)ans+=a[num];
                if(i==p[j].first)a[p[j].second]++;
            }
        }
        cout<<ans<<endl;
    }
}

 

标签:std,idx,int,875,Codeforces,cin,add,Div,include
From: https://www.cnblogs.com/Nclown/p/17445786.html

相关文章

  • Codeforces Round #548 (Div. 2) 题解
    题目链接http://codeforces.com/contest/1139 A.EvenSubstrings判断个位是否是奇数即可。#include<iostream>#include<set>#include<array>#include<vector>usingnamespacestd;typedeflonglongll;constintmx=1e5+10;lldp[mx][2];charstr[......
  • CodeForces 1250E [2-sat]
    题目链接:https://codeforces.com/contest/1250/problem/E 解题思路:首先根据反转的性质有:    有字符串a,b,a的反转A,b的反转B,如果a和b匹配值为K,那么A和B的值也为K    同样a和B匹配值为M,那么A和b的匹配值也为M,因此有对称性那么就可以转换成2-sat问题了,设点i的对立......
  • Codeforces Round #594 (Div. 2) 题解
    题目链接:https://codeforces.com/contest/1248 A-IntegerPoints水题#include<bits/stdc++.h>usingnamespacestd;constintmx=1e5+5;typedeflonglongll;inta[mx],b[mx];intmain(){ intt; scanf("%d",&t); while(t--){ intn,m; scan......
  • CodeForcese 1256F [思维]
    题目链接:https://codeforces.com/problemset/problem/1256/F 解题思路:任意的翻转都可以认为是翻转若干个长度为2的,交换两个数主要奇数次翻转。两个串可以翻转成一样,那么一定可以再花费一样的翻转次数变成有序的字符串,连续两个相等的字符翻转任意数次是一样的,所以只要有两个字符......
  • Codeforces Round #599 (Div. 2) 题解
    题目链接:https://codeforces.com/contest/1243 A-MaximumSquare水题不说了#include<bits/stdc++.h>usingnamespacestd;constintmx=1e5+50;typedeflonglongll;intn;inta[mx];intmain(){ intt; scanf("%d",&t); while(t--){ scan......
  • Codeforces #564 (Div. 2) C. Nauuo and Cards[贪心]
    题目链接:http://codeforces.com/contest/1173/problem/C 解题思路:很明显总的抽卡次数是不会超过2*n的。然后我们再将情况分成两种:1.编号1的卡片已经在里面了,并且最底部已经形成了一个1~x的连续的卡片,而且之后x+1,x+2都可以来得及补上,在这种情况下抽卡次数肯定就不会超过n了。2.......
  • Codeforces #564 (Div. 2) D. Nauuo and Circle[树形DP]
    题目链接:http://codeforces.com/contest/1173/problem/D 解题思路:首先得知道按照圆周顺序的话,那么一颗子树必须存放在连续的一段区间里面,现在我们假设根节点是1,那么一颗为u做根节点的子树他的方案数就是各个儿子的方案数的乘积最后再乘以儿子个数+1(u节点本身)的排列方案数。所......
  • Educational Codeforces Round 149 (Rated for Div. 2)
    A.GrasshopperonaLine#include<bits/stdc++.h>usingnamespacestd;#defineintlonglongvoidsolve(){intx,k;cin>>x>>k;if(x%k==0){cout<<"2\n"<<x-1<<"&......
  • leetcode 728. Self Dividing Numbers
    Aself-dividingnumberisanumberthatisdivisiblebyeverydigititcontains.Forexample,128isaself-dividingnumberbecause128%1==0,128%2==0,and128%8==0.Also,aself-dividingnumberisnotallowedtocontainthedigitzero.Givenal......
  • Codeforces Round 875 (Div. 2) A-D
    A.TwinPermutations题意:给出一个由[1,2,...,n]组成的数组a,构造另一个由[1,2,...,n]组成的数组b,使得a[1]+b[1]<=a[2]+b[2]<=...<=a[n]+b[n]Solution可以想到只要让他们全为n+1就行了,这样是一定有解的voidsolve(){ intn;cin>>n; for(inti=1;i<=n;i++) { cin>>a[i]; ......