Changle training Day3

T1 cow Clothesline

topic

Description [title]

Under the wise leadership of Mother Bear, hour and his companions gave birth to many baby cow. Bear aunt decided to give every baby wear cute baby clothes. Then, wash drying of clothes for the baby cow became very uncomfortable thing.

Sage king take this important task. After washing the clothes, you have to dry the clothes. Clothes Under natural conditions can be dried by the time point A 1 humidity. Stingy bear aunt bought a table dryer.

Use dryer allows you to use the time to make a garment 1 Aside from the natural humidity dried at point A, point B, drying may humidity, but only for a time within a garment.

N pieces of clothes for various reasons and not as wet, and now tell you the humidity of each garment, you want to find all the clothes are dry minimum time (humidity of 0 to dry).

[Input Format]

The first row N, A, B, the next N rows, each row a number representing the humidity of clothes.

[Output format]

Line, the minimum time.

[Data] scale

1<=湿度,A,B<=500000,1<=N<=500000。

Resolve

The first problem is still sending points (zero to comfort other explosive title emotional QAQ).

Direct binary to, each cycle is determined only s [i] (humidity) and the size of a * mid (mid number of days) on it.

The final answer is, l.

Code

#include <algorithm>
#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <cmath>
using namespace std;
int read()
{
    int num=0,w=1;
    char ch=getchar();
    while(ch<'0'||ch>'9')
    {
        if(ch=='-') w=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')
    {
        num=(num<<1)+(num<<3)+ch-'0';
        ch=getchar();
    }
    return num*w;
}
int n,a,b,s[500100];
long long l,r,mid;
bool cmp(int x,int y)
{
    return x>y;
}
int main()
{
    //freopen("dry.in","r",stdin);
    //freopen("dry.out","w",stdout);
    n=read(),a=read(),b=read();
    for(int i=1;i<=n;i++)
    {
        s[i]+=read();
        r+=s[i];
    }
    while(l<=r)
    {
        long long ans=0;
        mid=(l+r)>>1;
        for(int i=1;i<=n;i++)
            if(s[i]-a*mid>0)
            {
                if((s[i]-a*mid)%b==0) ans+=(s[i]-a*mid)/b;
                else ans+=(s[i]-a*mid)/b+1;
            }
        if(ans<=mid) r=mid-1;
        else l=mid+1;
    }
    cout<<l;
    return 0;
    //fclose(stdin);
    //fclose(stdout);
}
View Code

 

 

 

 

 

T2 decrypt the file

topic

Description [title]

 26 letters in English known occurrence probability p [0], p [1] ......, p [25] (and their 1), respectively, 'a', 'b', 'c' ...... appears the probability (uppercase and lowercase letters are considered the same).

There is now a encrypted file, encryption is the original file for each letter of the same transformation, other characters the same, methods of transformation are as follows:

If a to z are numbered 0-25, the letter i is replaced with (i + k) mod 26,0 <= k <26. The original uppercase letters, still capitalized, lowercase letters still turned out to be lowercase.

But you do not know the value of k, so I had to enumerate. Knowing the frequency of occurrence of letters 26, so you can try to select a good k, such that the frequency of the variance and the minimum variance and is defined as follows:

You assume that the probability of k enumeration restore the original file out of the 26 letters appears to x [0], x [1], ......, x [25], and then the variance is: (p [0] -x [0]) ^ 2 + (p [1] -x [1]) ^ 2 + ...... + (p [25] -x [25]) ^ 2.

If there are as good as two identical k, then choose smaller k.

Finally, the output decrypted original file.

[Input Format]

The first 26 rows are the probability of occurrence of 26 letters.

Next is a case contains only 26 letters and spaces, line breaks, punctuation, digits and other text.

[Output format]

Decrypted text.

[Data] scale

Enter the file does not exceed 10k.

Resolve

This problem can direct simulation, very sick fun.

A first pre-out array (each number corresponding to the letter, 0-25 represents az, 26-51 represents AZ).

I use the handle text string, while loop directly getline (cin, s [++ temp]), there are two details:

1, first enter a "\ n" (I do not know why, anyway, it reads a newline) input;

2, since ++ temp, even if there is no reading from or will increase once, or when output to the -1 <= change <.

Frequency for each letter, the present konjac is calculated each time when enumerating all k, is not actually used so much trouble,

As each letter is converted by the initial letters of correspondence, it is only necessary to calculate the frequency on the line of the initial letter ( obviously said given input ).

And then take the smallest variance can be. If the output is to be divided uppercase and lowercase letters processing,

Lowercase output a [(int) ((s [i] [j] - 'a' + kk)% 26)], uppercase output a [(int) ((s [i] [j] - 'A' + kk )% 26 + 26)] (pretreatment of a capital letter array is +26 on the basis of it lowercase letters).

If not the letter directly to the output of the original text just fine.

Code

#include <algorithm>
#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <cmath>
using namespace std;
int temp=0,sum[55],kk;
double p[26],x[26],ans,minn=1000000.0;
char a[55];
string s[10001];
int main()
{
    //freopen("decode.in","r",stdin);
    //freopen("decode.out","w",stdout);
    for(int i=0;i<26;i++) a[i]=char('a'+i);
    for(int i=26;i<52;i++) a[i]=char('A'+i-26);
    for(int i=0;i<26;i++) cin>>p[i];
    scanf("\n");
    while(getline(cin,s[++temp])) ;
    for(int k=0;k<26;k++)
    {
        memset(sum,0,sizeof(sum));
        ans=0.0;
        for(int i=1;i<=temp;i++)
            for(int j=0;j<s[i].size();j++)
            {
                if(s[i][j]>='a'&&s[i][j]<='z') sum[(s[i][j]-'a'+k)%26]++;
                if(s[i][j]>='A'&&s[i][j]<='Z') sum[(s[i][j]-'A'+k)%26]++;
            }
        for(int i=0;i<26;i++)
        {
            x[i]=(double)(sum[i]*0.03846153846);
            ans+=(p[i]-x[i])*(p[i]-x[i]);
        }
        if(ans<minn)
        {
            minn=ans;
            kk=k;
        }
    }
    for(int i=1;i<temp;i++)
    {
        for(int j=0;j<s[i].size();j++)
        {
            if(s[i][j]>='a'&&s[i][j]<='z') cout<<a[(int)((s[i][j]-'a'+kk)%26)];
            else if(s[i][j]>='A'&&s[i][j]<='Z') cout<<a[(int)((s[i][j]-'A'+kk)%26+26)];
            else cout<<s[i][j];
        }
        cout<<endl;
    }
    return 0;
    //fclose(stdin);
    //fclose(stdout);
}
View Code

 

 

 

 

 

T3 rest

topic

Description [title]

Rest of the time, can relax the muscles of the whole body, cleaning cleaning, feel very comfortable. In one day, a LMZ began to organize his bookshelf.

His books are known n the present, are arranged in order from left to right. He wanted the book from low to high-sorted, and each book has a unique height H i .

His sort method: Each book is divided into all continuous portion as little as possible, so that the height of each portion of the book is monotone decreasing, and in which less than all of the entire section 2 flip book.

The above operation is repeatedly performed, so that the final book full height increases monotonically. But, after all, time to rest, LMZ not want to spend too much time on this sort of thing to the book above.

So after flipping his division and finished the first book, he wanted to calculate how many times he performed a total reversal operation to put all the books sorted.

LMZ surprised to find that the length before the first sort, he first carved out of all sections are even.

[Input Format]

The first line of a positive integer n, the total number of books.

Next n lines, each line H only a positive integer i , i is the height of the book.

[Output format]

Only an integer number of flip operations for the LMZ need to do.

[Data] scale

For 10% of the data: n <= 50

For 40% of the data: n <= 3000

To 100% of the data:. 1 <= n-<= 100000,. 1 <H = I <= n-

Resolve

In fact, this problem is in demand reverse order, so do with merge sort.

In the process of merging, if a [i]> a [j], i.e. reverse order, the ans + = mid-i + 1, simulation again know the specific reasons.

A program function is applied to the reverse <algorithm> where the role is inverted, in a p [cnt] .r time assignment, Fu is the i-1, so that when inverted to give the final +1.

The answer is in the n- 2 /2, it is clear int is not enough, remember to open long long.

Code

#include<algorithm>
#include<iostream>
#include<cstdio>
using namespace std;
const int N = 1e5 + 10;
int c[N], a[N], n;
long long ans;
void msort(int l, int r) {
    if(l == r) return;
    int mid = (l + r) >> 1;
    msort(l, mid), msort(mid + 1, r);
    int i = l, j = mid + 1, k = l;
    while(i <= mid && j <= r) {
        if(a[i] <= a[j]) 
            c[k++] = a[i++];
        else {
            ans += mid - i + 1;
            c[k++] = a[j++];
        }
    }
    while(i <= mid) c[k++] = a[i++];
    while(j <= r) c[k++] = a[j++];
    for(int t = l; t <= r; t++)
        a[t] = c[t];
}
struct rec {
    int l, r;
}p[N];
int main() {
    //freopen("rest.in", "r", stdin);
    //freopen("rest.out", "w", stdout);
    int cnt = 0;
    scanf("%d", &n);
    for(int i = 1; i <= n; i++) {
        scanf("%d", a + i);
        if(a[i] > a[i - 1]) {
            p[cnt].r = i - 1;
            p[++cnt].l = i;
        }
    }
    p[cnt].r = n;
    for(int i = 1; i <= cnt; i++) 
        reverse(a + p[i].l, a + p[i].r + 1), ans++;
    msort(1, n);
    cout<<ans;
    return 0;
}
View Code

 

 

 

 

 

Games T4 access matrix

topic

Description [title]

Handsome with the students often play a game access matrix: for a given n * m matrix, each element of the matrix A ij of both non-negative integers. Rules of the game are as follows:

Each line must be removed from each of a number of the elements 1. Each time taken, a total of n. After all of the elements of the matrix after taking m times;
2. Each element of each row is removed only the beginning or end of the element row;
3. Each access have a score value, is taken as the number per row the sum score; score = fetching each row is removed element value 2 * i , where i represents the i-th access (numbering begins at 1);
4. the end of the game the number of times m taking the total score of the score with.

Handsome like you to help write a program, for any matrix, you can find the maximum number of points after take.

[Input Format]

Input includes row n + 1:

The first line separated by a space two integers n and m.

2 ~ n + 1 of the behavior matrix of n * m, wherein each of the m rows separated by a single space.

[Output format]

The output file contains only one line, is an integer, i.e., enter the maximum number of minutes after the matrix is ​​taken.

[Data] scale

60% of the data satisfies:. 1 <= n- , m <= 30, the answer does not exceed 10 16

100% data satisfies:. 1 <= n-, m <= 80,0 <= A ij of <= 1000

Resolve

This question is actually one can see a range of DP.

Score Order f [i] [j] denotes the k-th row taking the number of take became [i, j] of the interval.

Since only taken from the right and left ends, so the apparent state transition equation:

f[i][j]=max(f[i-1][j]+a[i-1][j]*2m-j+i-1,f[i][j+1]+a[i][j+1]*2m-j+i-1)

Since the konjac code i and j are from 0 it is not a power of 2 m-j + i-1 of the cycle, but i + j (recommended above wording more personal, more user-friendly).

Well, this question is like that.

Strange,

If only just so, this question is too simple,

but! ! ! ! !

Note the data range, m up to 80, then the 80 th power is how much? 1208925819614629174706176!

Not so much the number of int, long long does not work, even if it is unsigned long long is not enough, then how to do it?

The answer is: high-precision!

This question is a high precision real disgusting place, need to use are:

+ High-precision high-precision, high-precision single-precision *, max (high-precision, high-precision).

So this konjac speaking about how to achieve high-precision ...... strange.

Since the high-precision Konjac does not, so high precision in the following code directly from the standard process "borrowed" (manual funny).

Code

#include <algorithm>
#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <cmath>
using namespace std;
int read()
{
    int num=0,w=1;
    char ch=getchar();
    while(ch<'0'||ch>'9')
    {
        if(ch=='-') w=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')
    {
        num=(num<<1)+(num<<3)+ch-'0';
        ch=getchar();
    }
    return num*w;
}
int n,m,a[85][85];
struct sb{
    int c[16];
    friend inline sb operator * (const sb &a, const int &b)
    {
        sb c;
        for(int i = 1; i <= 15; i ++)
            c.c[i] = 0;
        c.c[0] = a.c[0];
        int p = 0;
        for(int i = 1; i <= a.c[0]; i ++)
        {
            c.c[i] = a.c[i] * b + p;
            p = c.c[i] / 10000;
            c.c[i] %= 10000;
        }
        int &len = c.c[0];
        while (p)
        {
            c.c[++len] = p % 10000;
            p /= 10000;
        }
        return c;
    }
    friend inline sb operator * (const sb &a, const sb &b)
    {
        sb c;
        for(int i = 1; i <= 15; i ++)
            c.c[i] = 0;
        c.c[0] = a.c[0] + b.c[0] - 1;
        for(int i = 1; i <= a.c[0]; i ++)
        {
            int p = 0;
            for(int j = 1; j <= b.c[0]; j ++)
            {
                c.c[i + j - 1] += a.c[i] * b.c[j] + p;
                p = c.c[i + j - 1] / 10000;
                c.c[i + j - 1] %= 10000;
            }
            c.c[i + b.c[0]] += p;
        }
        if (c.c[c.c[0] + 1]) c.c[0] ++;
        return c;
    }
    friend inline sb operator + (const sb &a, const sb &b)
    {
        sb c;
        int &len = c.c[0];
        c.c[0] = max(a.c[0], b.c[0]);
        for(int i = 1; i <= 15; i ++)
            c.c[i] = 0;
        int p = 0;
        for(int i = 1; i <= len; i ++)
        {
            c.c[i] = a.c[i] + b.c[i] + p;
            p = c.c[i] / 10000;
            c.c[i] %= 10000;
        }
        if (p) len ++, c.c[len] = p;
        return c;
    }
    inline sb operator = (const sb &b)
    {
        c[0] = b.c[0];
        for(int i = 1; i <= 15; i ++)
            c[i] = 0;
        for(int i = 1; i <= b.c[0]; i ++)
            c[i] = b.c[i];
        return *this;
    }
    inline void print()
    {
        cout << c[c[0]];
        for(int i = c[0] - 1; i >= 1; i --)
            printf("%04d", c[i]);
        cout << endl;
    }
};
inline sb Max(const sb &a, const sb &b)
{
    if (a.c[0] > b.c[0]) return a;
    else if (a.c[0] < b.c[0]) return b;
    else if (a.c[0] == b.c[0])
    {
        for(int i = a.c[0]; i >= 1; i --)
            if (a.c[i] > b.c[i]) return a;
            else if (a.c[i] < b.c[i]) return b;
    }
    return a;
}
sb f[85][85],cf[85],tot,ans;
int main()
{
    //freopen("game.in","r",stdin);
    //freopen("game.out","w",stdout);
    n=read(),m=read();
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++) a[i][j]=read();
    cf[0].c[0]=1;cf[0].c[1]=1;
    for(int i=1;i<=m;i++) cf[i]=cf[i-1]*2;
    for(int k=1;k<=n;k++)
    {
        for(int i=0;i<=m;i++)
            for(int j=0;j<=m;j++) memset(f[i][j].c,0,sizeof(f[i][j].c));
        f[m][m].c[0]=1;
        memset(tot.c,0,sizeof(tot.c));
        tot.c[0]=1;
        for(int i=0;i<=m;i++)
            for(int j=0;j<=m-i;j++)
            {
                if(i==0&&j==0) continue;
                if(i) f[i][j]=Max(f[i][j],f[i-1][j]+cf[i+j]*a[k][i]);
                if(j) f[i][j]=Max(f[i][j],f[i][j-1]+cf[i+j]*a[k][m-j+1]);
                tot=Max(tot,f[i][j]);
            }
        ans=ans+tot;
    }
    ans.print();
    //fclose(stdin);
    //fclose(stdout);
    return 0;
}
View Code

 

Guess you like

Origin www.cnblogs.com/I-Love-You-520/p/11239720.html