Niuke IOI week 27 - popularity group

Posted by coja1 on Sat, 22 Jan 2022 04:52:09 +0100

A. Little H's kitten

Title Link

meaning of the title

Take the x-axis and y-axis as the wall, the origin as the corner, and the kitten is in the corner. Give several points to find out whether the kitten can be surrounded in the corner with the fence around the point, and find the shortest total length of the fence

thinking

  • 1. A simple drawing on paper can prove that there must be a point on the x-axis and a point on the y-axis.
  • 2. The two points closest to the origin can form the shortest circle
  • 3. Set it Two point distance formula

Pit point

  • 1. The drawing proves that it is not rigorous enough
  • 2. Accuracy
  • 3. The time complexity of cin and cout is jammed

code

#include<bits/stdc++.h>
using namespace std;
struct name
{
    double x;
    double y;
    double len;
}num[1000005];
double func (double a,double b,double c,double d)//Find the distance between two points
{
	double dis=sqrt((c-a)*(c-a)+(d-b)*(d-b));
	return dis;
}
bool cmp(name a,name b)//Custom structure Sorting
{
    return a.len<b.len;
}
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        scanf("%lf%lf",&num[i].x,&num[i].y);
        num[i].len=func(num[i].x,num[i].y,0,0);
    }
    sort(num,num+n,cmp);
    int res1=-1,res2=-1;
    for(int i=0;i<n;i++)
    {
        if(num[i].x==0&&res1==-1)
        {
            res1=i;
        }
        if(num[i].y==0&&res2==-1)
        {
            res2=i;
        }
        if(res1!=-1&&res2!=-1)
        {
            break;
        }
    }
    double ans=0;
    if(res1!=-1&&res2!=-1)
    {
        ans+=func(num[res1].x,num[res1].y,num[res2].x,num[res2].y);
        printf("%.10lf\n",ans);
    }else{
        printf("Poor Little H!",ans);
    }
    return 0;
}

summary

Simple questions need to be written quickly.

B. Sequence of small H

Title Link

meaning of the title

A univariate quadratic equation is given and solved.

thinking

  • 1. According to the example explanation, you can simply draw a conclusion
  • 2. It can be seen that A1 = 1, S2 = 4, A3 = 9, and an is the square of n
  • 3. The data is so large that it must be of high precision

Pit point

  • 1. Set a high-precision template
  • 2. More than half of the violence

code

pow cheat points, use more, have accuracy problems, be careful and useful

#include<bits/stdc++.h>
using namespace std;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    long long int n;
    cin>>n;
    cout<<pow(n,2);
    return 0;
}

High precision Full Score bare formwork

#include <iostream>
#include <vector>

using namespace std;

vector<int> mul(vector<int> &A, vector<int> &B) {
    vector<int> C(A.size() + B.size(), 0); // Initialize to 0, and 999 * 99 can be up to 5 bits

    for (int i = 0; i < A.size(); i++)
        for (int j = 0; j < B.size(); j++)
            C[i + j] += A[i] * B[j];

    int t = 0;
    for (int i = 0; i < C.size(); i++) { // When i = C.size() - 1, t must be less than 10
        t += C[i];
        C[i] = t % 10;
        t /= 10;
    }

    while (C.size() > 1 && C.back() == 0) C.pop_back(); // You must go to the leading 0, because the highest bit is likely to be 0
    return C;
}

int main() {
    string a, b;
    cin >> a ; // a = "1222323", b = "2323423423"
    b=a;
    vector<int> A, B;
    for (int i = a.size() - 1; i >= 0; i--)
        A.push_back(a[i] - '0');
    for (int i = b.size() - 1; i >= 0; i--)
        B.push_back(b[i] - '0');

    auto C = mul(A, B);

    for (int i = C.size() - 1; i >= 0; i--)
        cout << C[i];

    return 0;
}

summary

Naked template, the conclusion is easy to guess. Set a template and it will be over in 2 minutes

C. Little H's candy

Title Link

meaning of the title

Given n, there are two operations, operation 1: N divided by k (if divisible), operation 2: subtract 1 to find the minimum operand that makes n become 1.

thinking

  • 1. It's good to change the topic ideas in place
  • 2. First change n into a number divisible by k (through operation 2)
  • 3. Divide k and simulate

Pit point

  • 1. The timeout is cool for a while, and the timeout is cool for a long time. I stuck my endl, and I cracked. I said why "\ n" passed, and endl timed out, resulting in no score during the game (reason: endl has one more refresh cache than "\ n", which affects efficiency). This tells us that endl is less used
  • 2. From multiplication and addition to subtraction and division, it requires certain thinking ability
  • 3. Learn to optimize. If you don't reduce 1, you will really reduce 1 one by one

code

#include<bits/stdc++.h>
using namespace std;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    int t;
    cin>>t;
    while(t--)
    {
        long long int k,n,ans=0;
        cin>>k>>n;
        if(k==1)
        {
            cout<<n-1<<"\n";
            continue;
        }
        while(n>=k)
        {
            ans+=n%k;//So that n can be divided by k, so we need to change n into a number that can be divided by k
            ans++;//Divide by + 1
            n=n/k;
        }
        ans+=n-1;//If the number of while loop ends is less than or equal to k, it needs to be changed to 1
        cout<<ans<<"\n";
    }
    return 0;
}

summary

In addition to the time card, the topic is still interesting. I have seen many similar topics in CF.

Topics: Algorithm Dynamic Programming