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

     A.Maximum Square

    Ujan decided to make a new wooden roof for the house. He has nn rectangular planks numbered from 11 to nn. The ii-th plank has size ai×1ai×1 (that is, the width is 11 and the height is aiai).

    Now, Ujan wants to make a square roof. He will first choose some of the planks and place them side by side in some order. Then he will glue together all of these planks by their vertical sides. Finally, he will cut out a square from the resulting shape in such a way that the sides of the square are horizontal and vertical.

    For example, if Ujan had planks with lengths 44, 33, 11, 44 and 55, he could choose planks with lengths 44, 33 and 55. Then he can cut out a 3×33×3square, which is the maximum possible. Note that this is not the only way he can obtain a 3×33×3 square.

    What is the maximum side length of the square Ujan can get?

    Input

    The first line of input contains a single integer kk (1k101≤k≤10), the number of test cases in the input.

    For each test case, the first line contains a single integer nn (1n10001≤n≤1000), the number of planks Ujan has in store. The next line contains nn integers a1,,ana1,…,an (1ain1≤ai≤n), the lengths of the planks.

    Output

    For each of the test cases, output a single integer, the maximum possible side length of the square.

    Example
    input
    Copy
    4
    5
    4 3 1 4 5
    4
    4 4 4 4
    3
    1 1 1
    5
    5 5 1 1 5
    
    output
    Copy
    3
    4
    1
    3
    
    Note

    The first sample corresponds to the example in the statement.

    In the second sample, gluing all 44 planks will result in a 4×44×4 square.

    In the third sample, the maximum possible square is 1×11×1 and can be taken simply as any of the planks.

     分析:找最大正方形的边长

    Code :max(ans,min(arr[i],n-i+1)); 先计算当前高度和宽度的最小值,然后和记录的最大正方形的边长比较

     

    const int mxn = 1e6+5;
    ll n,m,k,ans,cnt,col,t;
    int arr[mxn],flag;
    int main()
    {
        scanf("%d",&t);
        while(t--)
        {
            int n;
            scanf("%d",&n);
            for(int i=1;i<=n;i++) 
                scanf("%d",&arr[i]);
            sort(arr+1,arr+1+n);
            int ans=0;
            for(int i=n;i>0;i--)
                ans=max(ans,min(arr[i],n-i+1));
            printf("%d
    ",ans);
        }
        return 0;
    }

    B1. Character Swap (Easy Version)

    This problem is different from the hard version. In this version Ujan makes exactly one exchange. You can hack this problem only if you solve both problems.

    After struggling and failing many times, Ujan decided to try to clean up his house again. He decided to get his strings in order first.

    Ujan has two distinct strings ss and tt of length nn consisting of only of lowercase English characters. He wants to make them equal. Since Ujan is lazy, he will perform the following operation exactly once: he takes two positions ii and jj (1i,jn1≤i,j≤n, the values ii and jj can be equal or different), and swaps the characters sisi and tjtj. Can he succeed?

    Note that he has to perform this operation exactly once. He has to perform this operation.

    Input

    The first line contains a single integer kk (1k101≤k≤10), the number of test cases.

    For each of the test cases, the first line contains a single integer nn (2n1042≤n≤104), the length of the strings ss and tt.

    Each of the next two lines contains the strings ss and tt, each having length exactly nn. The strings consist only of lowercase English letters. It is guaranteed that strings are different.

    Output

    For each test case, output "Yes" if Ujan can make the two strings equal and "No" otherwise.

    You can print each letter in any case (upper or lower).

    Example
    input
    Copy
    4
    5
    souse
    houhe
    3
    cat
    dog
    2
    aa
    az
    3
    abc
    bca
    
    output
    Copy
    Yes
    No
    No
    No
    
    Note

    In the first test case, Ujan can swap characters s1s1 and t4t4, obtaining the word "house".

    In the second test case, it is not possible to make the strings equal using exactly one swap of sisi and tjtj.

    #include <bits/stdc++.h>
    #include <iostream>
    #include <algorithm>
    #include <cstdio>
    #include <string>
    #include <cstring>
    #include <cstdlib>
    #include <map>
    #include <vector>
    #include <set>
    #include <queue>
    #include <stack>
    #include <cmath>
    using namespace std;
    #define mem(s,t) memset(s,t,sizeof(s))
    #define pq priority_queue
    #define pb push_back
    #define fi first
    #define se second
    #define ac return 0;
    #define ll long long
    #define cin2(a,n,m)     for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) cin>>a[i][j];
    #define rep_(n,m)  for(int i=1;i<=n;i++) for(int j=1;j<=m;j++)
    #define rep(n) for(int i=1;i<=n;i++)
    #define test(xxx) cout<<"  Test  " <<" "<<xxx<<endl;
    #define TLE std::ios::sync_with_stdio(false);   cin.tie(NULL);   cout.tie(NULL);   cout.precision(10);
    #define lc now<<1
    #define rc now<<1|1
    #define ls now<<1,l,mid
    #define rs now<<1|1,mid+1,r
    #define half no[now].l+((no[now].r-no[now].l)>>1)
    #define ll long long
    const int mxn = 1e6+5;
    ll n,m,k,ans,cnt,col;
    int a[mxn],b[27]; //这个题a【4】就够用
    string str ,ch ;
    int main()
    {
        cin>>n;
        while(n--)
        {
            cin>>k;
            cin>>str>>ch;
            k = 0;
            memset(b,0,sizeof(b));
            for(int i=0;i<str.size() && k<3;i++)
            {
                b[str[i]-'a']++;
                if(str[i]!=ch[i])
                {
                    a[++k]=i+1;
                }
            }
            if(k>=3 || k==1 )
                cout<<"No"<<endl;
            else if(k==2)
            {
                if(str[a[1]-1] == str[a[2]-1] && ch[a[2]-1] == ch[a[1]-1] )
                    cout<<"Yes"<<endl;
                else
                    cout<<"No"<<endl;
            }
        }
        return 0;
    }

     

    B2. Character Swap (Hard Version)

    This problem is different from the easy version. In this version Ujan makes at most 2n2n swaps. In addition, k1000,n50k≤1000,n≤50 and it is necessary to print swaps themselves. You can hack this problem if you solve it. But you can hack the previous problem only if you solve both problems.

    After struggling and failing many times, Ujan decided to try to clean up his house again. He decided to get his strings in order first.

    Ujan has two distinct strings ss and tt of length nn consisting of only of lowercase English characters. He wants to make them equal. Since Ujan is lazy, he will perform the following operation at most 2n2n times: he takes two positions ii and jj (1i,jn1≤i,j≤n, the values ii and jj can be equal or different), and swaps the characters sisi and tjtj.

    Ujan's goal is to make the strings ss and tt equal. He does not need to minimize the number of performed operations: any sequence of operations of length 2n2n or shorter is suitable.

    Input

    The first line contains a single integer kk (1k10001≤k≤1000), the number of test cases.

    For each of the test cases, the first line contains a single integer nn (2n502≤n≤50), the length of the strings ss and tt.

    Each of the next two lines contains the strings ss and tt, each having length exactly nn. The strings consist only of lowercase English letters. It is guaranteed that strings are different.

    Output

    For each test case, output "Yes" if Ujan can make the two strings equal with at most 2n2n operations and "No" otherwise. You can print each letter in any case (upper or lower).

    In the case of "Yes" print mm (1m2n1≤m≤2n) on the next line, where mm is the number of swap operations to make the strings equal. Then print mm lines, each line should contain two integers i,ji,j (1i,jn1≤i,j≤n) meaning that Ujan swaps sisi and tjtj during the corresponding operation. You do not need to minimize the number of operations. Any sequence of length not more than 2n2n is suitable.

    Example
    input
    Copy
    4
    5
    souse
    houhe
    3
    cat
    dog
    2
    aa
    az
    3
    abc
    bca
    
    output
    Copy
    Yes
    1
    1 4
    No
    No
    Yes
    3
    1 2
    3 1
    2 3

    分析:问长度为n的两个字符串是否可以在2n次交换的操作中相同,
    Code:如果某字符的个数不能被2整除,输出No。
       否则,遍历S1,如果S1【i】!=S2【i】,从遍历后面的字符S1【j】,如果S1【i】==S1【j】保存i,j;如果S1【i】==S2【j】,保存并交换S1【j】s2【j】,最后输出就好了

    C. Tile Painting:
    https://www.cnblogs.com/Shallow-dream/p/11823678.html

    所遇皆星河
  • 相关阅读:
    Redis之scan
    MySQL中查看Blob类型的字段内容
    Redis之布隆过滤器BloomFilter
    Redis中的位图结构
    Redis之GeoHash根据经纬度距离排序
    .net 手写实现一个简单实体数据验证
    GogsWindows Server下配合Jenkins自动化发布
    C#的winform控件命名规范[转载]
    GogsWindows Server下搭建Git服务器
    Jenkins构建基于.NET Framework的web程序
  • 原文地址:https://www.cnblogs.com/Shallow-dream/p/11817053.html
Copyright © 2011-2022 走看看