2017 Summer Training Day1

来源:互联网 发布:使命召唤11优化怎么样 编辑:程序博客网 时间:2024/06/04 19:35

A - Hexadecimal’s Numbers

题目:
One beautiful July morning a terrible thing happened in Mainframe: a mean virus Megabyte somehow got access to the memory of his not less mean sister Hexadecimal. He loaded there a huge amount of n different natural numbers from 1 to n to obtain total control over her energy.
But his plan failed. The reason for this was very simple: Hexadecimal didn’t perceive any information, apart from numbers written in binary format. This means that if a number in a decimal representation contained characters apart from 0 and 1, it was not stored in the memory. Now Megabyte wants to know, how many numbers were loaded successfully.

Input
Input data contains the only number n (1 ≤ n ≤ 109).

Output
Output the only number — answer to the problem.

Example
Input
10
Output
2
Note
For n = 10 the answer includes numbers 1 and 10.

题意

给一个数,问不大于他且只由0,1组成的数字有多少。

理解

因为二进制只有0,1,所以用二进制来表示这个数,最后看以二进制形式表示的数字是否大于给定数字来进行判断数字多少。

代码

#include<bits/stdc++.h>using namespace std;int main(){    int n = 0;    cin >> n;    int ans = 0;    for(int i = 1;i <= n;i++)    {        int temp = i;        int j = 1;        while(temp != 0)        //二进制        {            ans += (temp % 2)*j;            j*=10;            temp /= 2;        }        if(ans == n)             //判断        {            ans = i;            break;        }        if(ans > n)        {            ans = i-1;            break;         }         ans = 0;    }    cout << ans << endl;}

C - Minimal string

题目:
Petya recieved a gift of a string s with length up to 105 characters for his birthday. He took two more empty strings t and u and decided to play a game. This game has two possible moves:
Extract the first character of s and append t with this character.
Extract the last character of t and append u with this character.
Petya wants to get strings s and t empty and string u lexicographically minimal.
You should write a program that will help Petya win the game.

Input
First line contains non-empty string s (1 ≤ |s| ≤ 105), consisting of lowercase English letters.
Output
Print resulting string u.

Example
Input
cab
Output
abc
Input
acdb
Output
abdc

题意

给一个字符串s,有两种操作:
1、把s的第一个放在t的尾部;
2、把t的尾部放在u的尾部;
求输出字典序最小的u。

理解

贪心,永远把S中剩下的最小字典序的先输出,但是在维护t这个栈的时候,用了一个后置位数组,就是来判断每一位该进栈的元素是不是这一位后面最小的元素。

代码

#include<bits/stdc++.h>using namespace std;string s;int a[1000040];int main(){    cin >> s;    int l = s.length();    memset(a,0,sizeof(a));    a[l] = 30;    for(int i = l-1;i >= 0;i--)        //后置位数组    {        a[i] = min(a[i+1],s[i]-'a');    }    stack<char> c;    for(int i = 0;i < l;i++)    {        if(c.empty())        {            c.push(s[i]);        }        else        {            while(!c.empty())            {                if(c.top()-'a' <= a[i])  //判断栈顶元素是不是这一位之后最小的元素                {                    cout << c.top();                    c.pop();                }                else                    break;            }            c.push(s[i]);               //无论怎么操作,都要把这一个元素入栈        }    }    while(!c.empty())    {        cout << c.top();        c.pop();    }    cout << endl;}

I - 4 Values whose Sum is 0

题目:
The SUM problem can be formulated as follows: given four lists A, B, C, D of integer values, compute how many quadruplet (a, b, c, d ) ∈ A x B x C x D are such that a + b + c + d = 0 . In the following, we assume that all lists have the same size n .

Input
The first line of the input file contains the size of the lists n (this value can be as large as 4000). We then have n lines containing four integer values (with absolute value as large as 2 28 ) that belong respectively to A, B, C and D .
Output
For each input file, your program has to write the number quadruplets whose sum is zero.

Sample Input
6
-45 22 42 -16
-41 -27 56 30
-36 53 -37 77
-36 30 -75 -46
26 -38 -10 62
-32 -54 -6 45
Sample Output
5
Hint
Sample Explanation: Indeed, the sum of the five following quadruplets is zero: (-45, -27, 42, 30), (26, 30, -10, -46), (-32, 22, 56, -46),(-32, 30, -75, 77), (-32, -54, 56, 30).

题意

给你n行数,每行4个,分别是ai,bi,ci,di;问用ai+bi+ci+di=0,有多少种组合。

理解

这里数据范围太大,所以采用折半枚举,先计算a+b,c+d。然后在这两个和里在找和为0。

代码

#include<iostream>#include<algorithm>using namespace std;int a[5000];int b[5000];int c[5000];int d[5000];int n[20000000];int m[20000000];int main(){    int N = 0;    int ans = 0;    cin >> N;    for(int i = 0;i < N;i++)    {        cin >> a[i] >> b[i] >> c[i] >> d[i];    }    for(int i = 0;i < N;i++)        for(int j = 0;j < N;j++)        {            n[i*N+j] = a[i]+b[j];              //a+b            m[i*N+j] = c[i]+d[j];              //c+d        }    sort(n,n+N*N);    sort(m,m+N*N);    for(int i = 0,j = N*N-1;i < N*N;i++)    {        while(j >= 0 && n[i]+m[j] > 0)        //最小+最大            j--;        if(j < 0)            break;        int t = j;        while(t >= 0 && n[i]+m[t] == 0)        {            ans++;            t--;        }    }    cout << ans << endl;}
原创粉丝点击