HZNU 2019 Summer training 6 -CodeForces - 622

A - Infinite Sequence  CodeForces - 622A 

Subject to the effect: this gives you a number of columns 1,1,2,1,2,3,1,2,3,4,1,2,3,4,5 .. . . Is arranged from 1 ~ n (n ++). Finally, the n-th position to ask you what the number is.

Ideas: If you take the time to the number of columns, then the column will find that 1 to a position corresponding to the last n is from 1 to n, and then we boldly using a binary search for the number.

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<set>
#include<string>
#include<vector>
#include<ctime>
#include<stack>
#include<fstream>
#include<iomanip>
#include<deque>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define mm(a,b) memset(a,b,sizeof(a))
#define maxn 300
#define len 150000000+5
#define inf 0x3f3f3f3f
int gcd(int a, int b)
{
    return a % b == 0 ? b : gcd(b, a%b);
}
int main()
{
    ll n;
    scanf("%lld", &n);
    ll l = 1, r = 1e8, mid;
    while (l <= r)
    {
        mid = (l + r) / 2;
        if ((mid + 1)*mid / 2 >= n && (mid - 1)*mid / 2 < n)
            break;
        else if ((mid + 1)*mid / 2 < n)
            l = mid + 1;
        else r = mid - 1;
    }
    ll maxx = (mid + 1)*mid / 2;
    ll ans = mid - (maxx - n);
    printf("%lld\n", ans);
    return 0;
}
View Code

 

B - Optimal Number Permutation CodeForces - 622D 

Question is intended: to give you an array of length 2 * n, the number that appears is 1 ~ n (each number appears twice). Defines the distance between the same two numbers di = xi-yi (xi> yi ), the final sequence asking how this array is configured such that the summed value of the minimum.

Idea: to see this problem do not worry, think about formula columns, we can find that you can construct a number of columns s = 0.

E.g:

n=1 1

n=2 1 1 2 2

n=3 1 3 1 2 2 3

n=4 1 2 4 2 1 3 3 4

1 ~ n-1 number ordered from the start, how to as n-0 is to be inserted empty row. If this number is odd then, we put from the left, from the right to put the even. Emissions to press so that the number (di + in) = 0 row position (I do not know why do not overwrite). Then finally put n.

There is a pit (my own), always open a small array, give you n <= 500000, you have to open twice ah. TAT

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<set>
#include<string>
#include<vector>
#include<ctime>
#include<stack>
#include<fstream>
#include<iomanip>
#include<deque>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define mm(a,b) memset(a,b,sizeof(a))
#define maxn 2*500000+50
#define len 150000000+5
#define inf 0x3f3f3f3f
int gcd(int a, int b)
{
    return a % b == 0 ? b : gcd(b, a%b);
}
int ans[maxn];
int main()
{
    int n;
    memset(ans, 0, sizeof(ans));
    scanf("%d", &n);
    int left = 1, right = n + 1;
    for (int i = 1; i <= n - 1; i++)
    {
        if (i % 2 == 1)
        {
            ans[left] = i;
            ans[left + n - i] = i;
            left++;
        }
        else
        {
            ans[right] = i;
            ans[right + n - i] = i;
            right++;
        }
    }
    for (int i = 1; i <= 2 * n; i++)
    {
        if (!ans[i]) ans[i] = n;
    }
    for (int i = 1; i <= 2 * n; i++)
    {
        printf("%d ", ans[i]);
    }
    printf("\n");
    return 0;
}
View Code

 

C - Not Equal on a Segment  CodeForces - 622C 

Meaning of the questions: to give you numbers n and m asking. Inquiry format: l, r, x, between the output l ~ r a [i] <> x subscript i.

Thinking: you need to be and this number is not equal to the location found in the array when it is read, go back to find. Because either the first interval and a digital x equal or unequal. It is not equal to the direct current output of position i, equal to the need to look for it later.

#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>
#include<functional>
using namespace std;
const int maxn = 2e5 + 50;
int a[maxn], f[maxn];
int main()
{
    int n, m;
    scanf("%d %d", &n, &m);
    for (int i = 1; i <= n; i++)
        scanf("%d", &a[i]);
    for (int i = 1; i <= n; i++)
    {
        if (a[i] != a[i + 1])
            f[i] = i + 1;
        else
        {
            int pos = i;
            for (; a[i] == a[i + 1];) i++;

            for (int j = pos; j <= i; j++)
                f[j] = i + 1;
        }
    }

    while (m--)
    {
        int l, r, x;
        scanf("%d %d %d", &l, &r, &x);
        int flag = 0;
        for (int i = l; i <= r; i++)
        {
            if (a[i] == x)
            {
                if (f[i] > r || f[i] == 0) break;
                printf("%d\n", f[i]);
                flag = 1;
                break;
            }
            else 
            { 
                printf("%d\n", i); 
                flag = 1;
                break; 
            }
        }
        if (!flag) printf("-1\n");
    }
    return 0;
}
View Code

 

D - Ants in Leaves  CodeForces - 622E 

Meaning of the questions: to give you a tree, a small ant on a leaf node, they want to return to the root node 1, they can go to their parent crawling, crawling spend a period of time. There is a requirement that, in addition to other than the root can accommodate an ant, other nodes can only accommodate an ant, ant so if a parent node. His siblings will have to wait some time. Q: All the ants back to the leaf nodes of the minimum time is how much?

Ideas: a giant giant laboratory that want to be against, you can assume that all the ants are on the root node, then that is all the ants into a leaf node shortest time. This problem is the need for achievements, then dfs, give each child node depth assignment.

#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>
#include<functional>
using namespace std;
const int maxn = 5e5 + 50;

int head[maxn * 2], num, cnt, dp[maxn], d[maxn];
struct node
{
    int to, next, w;];2*
} and [maxn

void init()
{
    num = 0;
    memset(head, -1, sizeof(head));
}

void add(int u, int v)
{
    e[num].to = v;
    e[num].next = head[u];
    head[u] = num++;
}

void dfs(int u, int fa, int depth)
{
    int flag = 0;
    for (int i = head[u]; ~i; i = e[i].next)
    {
        int v = e[i].to;
        if (v == fa)
            continue;
        flag = 1;
        dfs(v, u, depth + 1);
    }
    if (!flag)
        d[cnt++] = depth;
}

int main()
{
    init();
    int n, u, v, ans = 0;
    scanf("%d", &n);
    for (int i = 0; i < n - 1; i++)
    {
        scanf("%d %d", &u, &v);
        add(u, v);
        add(v, u);
    }
    for (int i = head[1]; ~i; i = e[i].next)
    {
        int v = e[i].to;
        cnt = 0;
        dfs(v, 1, 1);
        sort(d, d + cnt);
        for (int j = 1; j < cnt; j++)
            d [j] = max (d [j - 1 ] + 1 , d [j]); 

        years = max (years of [cnt - 1 ]); 
    } 
    Printf ( " % d \ n " , year);
    return  0 ; 
}
View Code

 

E - The Sum of the k-th Powers CodeForces - 622F 

 

Meaning of the questions:    %  10 9  + 7

Thinking: will not.

Guess you like

Origin www.cnblogs.com/Tangent-1231/p/11130306.html