2018.09.02 ox OI competition system test 1

https://www.nowcoder.com/acm/contest/181#question

A. Fibonacci

Find F[n-1]*F[n+1]-F[n]*F[n]

Hit the table and find the rule, 1, - 1, 1, - 1, 1, - 1.....

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
char s[1000001];
int main()
{
    while(cin>>s)
    {
        if ((s[strlen(s)-1]-'0')&1)
        {
            cout<<"-1"<<endl;
        }
        else
        {
            cout<<"1"<<endl;
        }
    }
 
    return 0;
}

B. send sub questions

a+b

#include<cstdio>
#include<iostream>
using namespace std;
int main()
{
    long long a,b,c;
    cin>>a>>b;
    cout<<a+b<<endl;
    return 0;
}

C. sequence

Can a sequence of length n be divided into k segments

The sum of the numbers is not a multiple of k, and it can't be divided. Then it can be judged one by one. The violence can be over, and the number of judgments is deceiving

#include <cstdio>
#include <algorithm>
using namespace std;
long long a[200000];
long long sum,n,m,v;
int main()
{
    scanf("%lld%lld",&n,&m);
    sum=0;
    for(int i=1;i<=n;i++)
    {
        scanf("%lld",&a[i]);
        sum+=a[i];
    }
    while(m--)
    {
        scanf("%lld",&v);
        if (sum%v)
        {
            puts("No");
            continue;
        }
        long long t=sum/v;
        long long num=0,res=0;
        for(int i=1;i<=n;i++)
        {
            res+=a[i];
            if (res==t)
            {
                num++;
                res=0;
            }
        }
        if (num==v)
        {
            puts("Yes");
            continue;
        }
        else
        {
            puts("No");
            continue;
        }
    }
    return 0;
}

D. Inspection of lobules

Diameter template of tree

  1. The diameter of a tree is the longest and simplest path of a tree. Solution: two times BFS: first select a starting point BFS to find the end of the longest road, and then carry out BFS from the end, then the longest road found by the second BFS is the diameter of the tree;

  2. Principle: set the starting point as u, and the end point v found by BFS for the first time must be an end point of the tree diameter

  3. 1) If u is the point on the diameter, then v is obviously the end of the diameter (because if v is not, there must be another point w that makes the distance between u and W longer, then v contradiction is found in BFS)

  4. 2) If u is not a point on the diameter, then u to v must intersect with the diameter of the tree. Then the intersection to v must be the second half of the diameter, so v must be an endpoint of the diameter, so the BFS from v must be the diameter length

 

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#define ll long long
using namespace std;
int n,s,cnt;
long long ans;
struct EDGE
{
    int next,to,val;
} edge[233330];
int head[33333],vis[33333];
long long dis[33333];
void add(int from,int to,int val)
{
    edge[++cnt].next=head[from];
    edge[cnt].to=to;
    edge[cnt].val=val;
    head[from]=cnt;
}
void dfs(int u,int fa)
{
    for(int i=head[u];i;i=edge[i].next)
    {
        int v=edge[i].to;
        if (v!=fa)
        {
            dis[v]=dis[u]+edge[i].val;
            if (dis[v]>ans)
            {
                ans=dis[v];
                s=v;
            }
            dfs(v,u);
        }
    }
}
int main()
{
 
    scanf("%d",&n);
    for (int i=1; i<n; i++)
    {
        int a,b,c;
        scanf("%d%d%d",&a,&b,&c);
        add(a,b,c);
        add(b,a,c);
    }
    dis[1]=0;
    dfs(1,0);
    dis[s]=0;
    dfs(s,0);
    printf ("%lld\n",ans*(21+ans)/2);
 
    return 0;
}

E. Traveling frog

Longest non descending subsequence, (deformation of longest ascending subsequence)

Template problem

#include<cstdio>
#include<iostream>
#include <algorithm>
using namespace std;
int dp[23334];
int a[23334];
int main()
{
    int n=1;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    dp[1]=a[1];
    int len=1;
    for (int i=2;i<=n;i++)
    {
        if (a[i]>=dp[len])
        {
            dp[++len]=a[i];
        }
        else
        {
            int j=upper_bound(dp+1,dp+len+1,a[i])-dp;
            dp[j]=a[i];
        }
    }
    printf ("%d\n",len);
 
    return 0;
}

 

 

Posted by thz_new_york on Thu, 02 Jan 2020 12:40:47 -0800