zoukankan      html  css  js  c++  java
  • Codeforces Round #396 (Div. 2) A

      地址:http://codeforces.com/contest/766/problem/A

    A题:

    A. Mahmoud and Longest Uncommon Subsequence
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    While Mahmoud and Ehab were practicing for IOI, they found a problem which name was Longest common subsequence. They solved it, and then Ehab challenged Mahmoud with another problem.

    Given two strings a and b, find the length of their longest uncommon subsequence, which is the longest string that is a subsequence of one of them and not a subsequence of the other.

    A subsequence of some string is a sequence of characters that appears in the same order in the string, The appearances don't have to be consecutive, for example, strings "ac", "bc", "abc" and "a" are subsequences of string "abc" while strings "abbc" and "acb" are not. The empty string is a subsequence of any string. Any string is a subsequence of itself.

    Input

    The first line contains string a, and the second line — string b. Both of these strings are non-empty and consist of lowercase letters of English alphabet. The length of each string is not bigger than 105 characters.

    Output

    If there's no uncommon subsequence, print "-1". Otherwise print the length of the longest uncommon subsequence of a and b.

    Examples
    input
    abcd
    defgh
    output
    5
    input
    a
    a
    output
    -1
    Note

    In the first example: you can choose "defgh" from string b as it is the longest subsequence of string b that doesn't appear as a subsequence of string a.

     

    思路:最大的连续不同子串肯定是整个串,如果两个串相同则是-1。

     1 #include <bits/stdc++.h>
     2 
     3 using namespace std;
     4 
     5 #define MP make_pair
     6 #define PB push_back
     7 typedef long long LL;
     8 typedef pair<int,int> PII;
     9 const double eps=1e-8;
    10 const double pi=acos(-1.0);
    11 const int K=1e6+7;
    12 const int mod=1e9+7;
    13 
    14 char s1[K],s2[K];
    15 
    16 int main(void)
    17 {
    18     cin>>s1>>s2;
    19     if(strcmp(s1,s2)==0)
    20         printf("-1
    ");
    21     else
    22         printf("%d
    ",max(strlen(s1),strlen(s2)));
    23     return 0;
    24 }

    B题:

    B. Mahmoud and a Triangle
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Mahmoud has n line segments, the i-th of them has length ai. Ehab challenged him to use exactly 3 line segments to form a non-degenerate triangle. Mahmoud doesn't accept challenges unless he is sure he can win, so he asked you to tell him if he should accept the challenge. Given the lengths of the line segments, check if he can choose exactly 3 of them to form a non-degenerate triangle.

    Mahmoud should use exactly 3 line segments, he can't concatenate two line segments or change any length. A non-degenerate triangle is a triangle with positive area.

    Input

    The first line contains single integer n (3 ≤ n ≤ 105) — the number of line segments Mahmoud has.

    The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the lengths of line segments Mahmoud has.

    Output

    In the only line print "YES" if he can choose exactly three line segments and form a non-degenerate triangle with them, and "NO" otherwise.

    Examples
    input
    5
    1 5 3 2 4
    output
    YES
    input
    3
    4 1 2
    output
    NO
    Note

    For the first example, he can use line segments with lengths 2, 4 and 5 to form a non-degenerate triangle.

     思路:判三角形成立的条件,两边之和大于第三边,两边之差小于第三边。枚举边肯定不行,时间复杂度太高。

      所以可以先从小到大排个序,然后判断第i-1条边和第i条边之和是否大于第i+1条边即可,因为第i-1条边和第i条边之差必定小于第i+1条边。

      这样扫一遍即可,复杂度O(nlogn)。

     1 #include <bits/stdc++.h>
     2 
     3 using namespace std;
     4 
     5 #define MP make_pair
     6 #define PB push_back
     7 typedef long long LL;
     8 typedef pair<int,int> PII;
     9 const double eps=1e-8;
    10 const double pi=acos(-1.0);
    11 const int K=1e6+7;
    12 const int mod=1e9+7;
    13 
    14 int a[K];
    15 
    16 int main(void)
    17 {
    18     int n,ff=0;
    19     cin>>n;
    20     for(int i=1;i<=n;i++)
    21         scanf("%d",&a[i]);
    22     sort(a+1,a+1+n);
    23     for(int i=2;i<n&&!ff;i++)
    24         if(a[i-1]+a[i]>a[i+1])
    25             ff=1;
    26     if(ff)
    27         printf("YES
    ");
    28     else
    29         printf("NO
    ");
    30     return 0;
    31 }

     

  • 相关阅读:
    面向对象的程序设计-2-创建对象
    面向对象的程序设计-1-理解对象
    react组件的生命周期
    react-router 组件式配置与对象式配置小区别
    mobx @computed的解读
    十分钟介绍mobx与react
    less学习
    git-简单流程(学习笔记)
    几种视频编码器的编译及使用方法
    一位程序员工作10年总结的13个忠告
  • 原文地址:https://www.cnblogs.com/weeping/p/6419018.html
Copyright © 2011-2022 走看看