zoukankan      html  css  js  c++  java
  • E. Magic Stones CF 思维题

    E. Magic Stones
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Grigory has nn magic stones, conveniently numbered from 11 to nn . The charge of the ii -th stone is equal to cici .

    Sometimes Grigory gets bored and selects some inner stone (that is, some stone with index ii , where 2in12≤i≤n−1 ), and after that synchronizes it with neighboring stones. After that, the chosen stone loses its own charge, but acquires the charges from neighboring stones. In other words, its charge cici changes to ci=ci+1+ci1cici′=ci+1+ci−1−ci .

    Andrew, Grigory's friend, also has nn stones with charges titi . Grigory is curious, whether there exists a sequence of zero or more synchronization operations, which transforms charges of Grigory's stones into charges of corresponding Andrew's stones, that is, changes cici into titi for all ii ?

    Input

    The first line contains one integer nn (2n1052≤n≤105 ) — the number of magic stones.

    The second line contains integers c1,c2,,cnc1,c2,…,cn (0ci21090≤ci≤2⋅109 ) — the charges of Grigory's stones.

    The second line contains integers t1,t2,,tnt1,t2,…,tn (0ti21090≤ti≤2⋅109 ) — the charges of Andrew's stones.

    Output

    If there exists a (possibly empty) sequence of synchronization operations, which changes all charges to the required ones, print "Yes".

    Otherwise, print "No".

    Examples
    Input
    Copy
    4
    7 2 4 12
    7 15 10 12
    
    Output
    Copy
    Yes
    
    Input
    Copy
    3
    4 4 4
    1 2 3
    
    Output
    Copy
    No
    
    Note

    In the first example, we can perform the following synchronizations (11 -indexed):

    • First, synchronize the third stone [7,2,4,12][7,2,10,12][7,2,4,12]→[7,2,10,12] .
    • Then synchronize the second stone: [7,2,10,12][7,15,10,12][7,2,10,12]→[7,15,10,12] .

    In the second example, any operation with the second stone will not change its charge.

    思路:

    注意看题目给出的公式

    c[i]'=c[i+1]+c[i-1]-c[i]

    可以变形成

    c[i+1]-c[i]'=c[i]-c[i-1]

    c[i]'-c[i-1]=c[i+1]-c[i]

    这样看来就是将第i项左右差分交换,可以把这个看成一种排序

    所以将差分全部重新排序之后,如果上下差分都相同,则满足题目要求

    #include <iostream>
    #include <stdio.h>
    #include <stdlib.h>
    #include <algorithm>
    using namespace std;
    typedef long long ll;
    const int maxn=1e5+10;
    ll a[maxn],b[maxn],c[maxn],d[maxn];
    int main()
    {
        int n;
        cin>>n;
        for(int i=1;i<=n;i++) scanf("%I64d",&a[i]);
        for(int i=1;i<=n;i++) scanf("%I64d",&b[i]);
        for(int i=1;i<n;i++)
        {
            c[i]=a[i+1]-a[i];
            d[i]=b[i+1]-b[i];
        }
        sort(c+1,c+n);
        sort(d+1,d+n);
        if(a[1]!=b[1]||a[n]!=b[n])
        {
            printf("No
    ");
            return 0;
        }
        for(int i=1;i<n;i++)
        {
            if(c[i]!=d[i])
            {
                printf("No
    ");
                return 0;
            }
        }
        printf("Yes
    ");
        return 0;
    }
    

      

  • 相关阅读:
    JS设置CSS样式的几种方式
    jquery基础
    JS里面的两种运动函数
    JavaScript必须了解的知识点总结。
    JavaScript调用函数的方法
    纯CSS完成tab实现5种不同切换对应内容效果
    Web设计师值得收藏的10个jQuery特效
    jQuery的.bind()、.live()和.delegate()之间区别
    Web前端:11个让你代码整洁的原则
    js函数中参数的传递
  • 原文地址:https://www.cnblogs.com/EchoZQN/p/10357305.html
Copyright © 2011-2022 走看看