[Session 8] problem solution

T1

Title Description
How can we evaluate the success of the scientists? Depending on the number of papers published or reference points - more precisely, citation counts? These two factors are very important. If a paper is paper other scientists cited a total of C times, the reference points of this paper is C. Scientists may be a measure of the success of their H index, it also takes into account the number of papers and reference points. Scientists H index is defined as the maximum integer H with the following properties: H scientists can select papers that meet cited score H papers are not less than H. For example, if a scientist has written 10 papers, and each one have been cited at least 10 times, then his H index (at least) 10. Write a program, enter a scientist all papers cited scores, his output H index.
Input
first line of input contains an integer N (1≤N≤5 * 1e5). It represents the number of papers. The second line contains N input a natural number Ci (Ci≤1e6). Each paper represents a reference scores.
Output
output an integer. H index represents the name of the scientist.
Sample
Sample Input 1
. 5
. 1. 1. 1. 4. 8
Sample Output 1
2
Sample input 2
. 5
. 8 10. 5. 4. 3
Sample Output 2
. 4

Problem-solving ideas

This problem is relatively easy, and different public practice, I have not sorted, I used a cnt number of occurrences statistics, then backwards to find qualified, that integration can choose to meet i i i chapter points at least of.

Code

#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<cstdlib>
using namespace std;
int n,c,cnt[1000005],maxn;
int sum;
int main(){
    //freopen("hindex.in","r",stdin);
    //freopen("hindex.out","w",stdout);
    scanf ("%d",&n);
    for (int i = 1;i <= n;i ++){
        scanf ("%d",&c);
        cnt[c] ++;
        maxn = max(c,maxn);
    }
    for (int i = maxn;i >= 0;i --){
        sum += cnt[i];
        if (sum >= i){
            printf("%d\n",i);
            return 0;
        }
    }
}

T2

Title Description
one room there are N tables, from left to right in a row. There are some phone on the table, and some tables are empty. All calls are bad, so when the phone on the i-th desk rang, the phone will ring on the j-th desk, if the desk from the j-th and i-th desk does not exceed D. In other words, j and i satisfy | ji | ≤D. There will be the first phone on the table and finally a table. Initially, the phone on the far left of the table will be sounded. Seeking to make a last phone rings, at least to be placed on a new phone number in a desk?
Input
The first input line contains two integers N (1≤N≤3e5) and D (1≤D≤N). Respectively, and the title number from the desk mentioned. Enter the second line contains N integers Ai (0≤Ai≤1). Ai represents the integer i th state of the i-th tables. Specifically, if Ai = 1 then there is the i-phone on a table, if Ai = 0 then no i-phone on a table.
Output
output an integer. He expressed the need to place the number of new phones.
Sample
Sample Input 1
. 4. 1
. 1 1 1 0
Sample Output 1
. 1
Sample Input 2
. 5 2
. 1 0 0 0. 1
Sample Output 2
. 1
Sample input 3
. 8 2
1 1 0 0 0 0. 1. 1
Sample Output 3
2

Problem-solving ideas

Feeling is a greedy simulation, we can directly ensure the farthest place, there are some half-way telephone line, then we'll start counting again.

Code

#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<cstdlib>
using namespace std;
int n,d,dis,tot;
int vis[300005];
int main(){
    //freopen("telefoni.in","r",stdin);
    //freopen("telefoni.out","w",stdout);
    scanf ("%d%d",&n,&d);
    for (int i = 1;i <= n;i ++)
        scanf ("%d",&vis[i]);
    for (int i = 1;i <= n;i ++){
        if (vis[i] == 1)
            dis = 1;
        else if (dis == d){
            tot ++;
            dis = 1;
        }
        else 
            dis ++;
    }
    printf("%d",tot);
}

T3

Title Description
Young Jozef obtain a N-th power of 2 set of positive integers as a gift. Considering Jozef regularly participate in football tournaments, he decided that N is a positive integer power of 2 to organize a tournament. Digital tournament shown below. Competition in pairs, the higher of the two figures advancing to a higher level. Level of competition is represented by a number between 1 and N, where the highest level is represented by the number 0.
Rendering
Since Jozef no time to organize all the games, he wanted to know, for each set of figures, against all the arrangements, to the highest attainable level (lowest number of levels).
Input
The first line of the input contains an integer N (1≤N≤20). Represents N. mentioned in the title The second input line N contains a positive integer power of 2 Ai (Ai≤1e9). N represents the set of positive integer power of 2.
Output
Output line contains N integer power of 2. Wherein the i-th denotes the order of input in accordance with the set of positive integers i can reach the highest level.
Sample
Sample Input 1
2
. 1. 4. 3 2
Sample output 1
2 0. 1. 1
Sample Input 2
. 4
. 5. 4. 3 2. 6. 8. 7. 1 2. 4
. 3. 4. 3. 6. 8. 1
Sample Output 2
. 1. 1. 1 2 2 0 2. 3. 1. 1
2 0 2. 1. 1. 3
sample input 3
. 1
. 1. 1
sample output 3
0 0

Problem-solving ideas

Let me talk about the meaning of the questions it, some people may feel that the sample does not, after all, when I started to have it all wrong.
Simply put, he is to make your own arrangements, where to find every number as far as energy.
Then we certainly have to sort ah. a great value, we are discrete.
Here one thing is the same number can be cut. So how far it can reach the count of it.
Here too, you can find some law.
If i can to n - 1, then certainly it should be at least equal to the number i 2 is small.
If i can to n - 2, then at least 4 small to certainly equal to the number of i.
If i to be n - k, then certainly it should be at least equal to 2 ^ k small numbers of i.
Lose it all, deal with what you get almost.

Code

#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<cstdlib>
using namespace std;
struct node{
    int x,pos;
    bool operator < (const node &a)const {
        return x < a.x;
    }
}s[1500005];
int n,n1,num[1500005],ans[1500005],tot[1500005],sum,cnt;
int qkpow(int x,int y){
    int ans = 1;
    while (y){
        if (y % 2 == 1)
            ans = ans * x ;
        x = x * x;
        y /= 2;
    }
    return ans;
}
int main(){
    //freopen("turnir.in","r",stdin);
    //freopen("turnir.out","w",stdout);
    scanf ("%d",&n);
    n1 = qkpow(2,n);
    for (int i = 1;i <= n1;i ++){
        int a;
        scanf ("%d",&a);
        s[i].x = a;
        s[i].pos = i;
    }
    sort(s + 1,s + 1 + n1);
    for (int i = 1;i <= n1;i ++){
        if (s[i].x != s[i - 1].x){
            num[s[i].pos] = ++ cnt;
            tot[cnt] ++;
        }
        else {
            num[s[i].pos] = cnt;
            tot[cnt] ++;
        }
    }
    for (int i = 1;i <= cnt;i ++){
        sum += tot[i];
        int j,tot1 = 0;
        for (j = 1;j <= n1;j *= 2){
            if (sum < j)
                break;
            tot1 ++;
        }
        ans[i] = n - tot1 + 1;
    }
    for (int i = 1;i < n1;i ++)
        printf("%d ",ans[num[i]]);
    printf("%d\n",ans[num[n1]]);
}

T4

Title Description
If a number is equal to its all factor (smaller than itself) sum, then this number is perfect. For example, a perfect 28, because 28 = 1 + 2 + 4 + 7 + 14.

Based on this definition, we imperfect degree number n as F (n), which is equal to the absolute value of the difference between all of the factors n (less than n) and the sum of n, therefore not exactly perfect number is 0, the remaining incomplete of natural numbers greater than 0.
E.g:

●f(6)=|6-(1+2+3)|=0
●f(11)=|11-(1)|=10
●f(24)=|24-(1+2+3+4+6+8+12)|=|-12|=12

Writing a program, for positive integers A and B, calculation of the imperfections of all numbers A and B and between: i.e., f (A) + f (A + 1) + ... + f (B).
Input
The first input line contains two integers A and B (1≤A≤B≤1e7). It represents A and B. title
Output
output an integer. It represents f (A) + f (A + 1) + ... + f (B).
Sample
Sample Input 1
. 1. 9
Sample output 1
21 is
a sample input 2
24 24
Sample Output 2
12 is

Problem-solving ideas

In fact, this is a template and ask about the number of questions, not perfection you find this stuff about the number and you'll be in the O (1) time to solve.
It should be about the number and is seeking two methods, the first is nlog (n) of the method for finding the code short. The second method is to find the Euler sieve, slightly longer codes, time complexity is O (n).
But the examination time 3s, like nlog (n) does not burst, the evaluation also passed. But TLE on the OJ, so it was Euler sieve

Code

#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<cstdlib>
using namespace std;
int a,b,psum[10000005],cnt,pri[1500005],s[10000005];
bool vis[10000005];
long long ans;
int main(){
    scanf ("%d%d",&a,&b);
    /*约数个数
    for (int i = 2;i <= b;i ++){
        if (!vis[i]){
            pri[++ cnt] = i;
            num[i] = 1;
            d[i] = 2;
        }
        for (int j = 1;j <= cnt && 1ll  * i * pri[j] <= b;j ++){
            vis[pri[j] * i] = 1;
            if (i % pri[j] == 0){
                num[pri[j] * i] = num[i] + 1;
                d[pri[j] * i] = d[i] / (num[i] + 1) * (num[i] + 2);
                break;
            }
            d[pri[j] * i] = d[i] * 2;
            num[i] = 1;
        }
    }*/
    s[1] = 1;
    for(int i = 2;i <= b;i ++){
        if (!vis[i]){
            pri[++ cnt] = i;
            s[i] = i + 1;
            psum[i] = i + 1;
        }
        for (int j = 1;j <= cnt && 1ll * i * pri[j] <= b;j ++){
            vis[pri[j] * i] = 1;
            if (i % pri[j] == 0){
                psum[pri[j] * i] = psum[i] * pri[j] + 1;
                s[pri[j] * i] = s[i] / psum[i] * psum[pri[j] * i];
                break;
            }
            s[pri[j] * i] = s[i] * (pri[j] + 1);
            psum[pri[j] * i] = pri[j] + 1;
        }
    }
    for (int i = a;i <= b;i ++)
        ans += abs(i * 2 - s[i]);
    printf("%lld\n",ans);
}

T5

Description Title
Daniel has a packet of sugar and N cards.

Each card has a write positive integer Pi. Daniel eating candy when the thought of a fun game. Daniel can pick two cards, to tie them together, if Daniel selection of a card and b card, then Daniel will immediately eaten min (Pa% Pb, Pb% Pa) candy, which represents x% y x is divided by y.

Daniel will each choose two cards will tie them together in the end, when he lifted one of the cards, all the rest of the cards will be lifted. He can be tied together many times the same card and other cards. Daniel asks you to calculate how much you should eat his candy.
Input
The first line of the input contains an integer N (1≤N≤1e5). It indicates the number of the card.

Next N lines contains a positive integer Pi (Pi≤1e7). Pi represents an integer of i-th on the first positive integer i cards.
Output
output an integer. Daniel indicates how many candy to eat at least.
Sample
Sample Input 1
. 4
2
. 6
. 3
. 11
Sample Output 1
. 1
Sample Input 2
. 4
. 1
2
. 3
. 4
Sample output 2
0
Sample input 3
. 3
. 4
. 9
15
Sample Output 3
. 4

Problem-solving ideas

A look also know that is a minimum spanning tree. However, the data is too big, with a bare minimum spanning tree of run time out will not only burst space, terrible terrible.
No positive solution code

Guess you like

Origin www.cnblogs.com/lover-fucker/p/11250071.html