set notes of Chou Hanpai

Keywords: iOS REST

Similar digital advanced set

jw big guy's: set the naked question. First, each array is de duplicated and then thrown into the set. The answer is the number of elements reduced after de duplication / the number of elements in the set. You can use unique for de duplication. However, this problem feels that the time is a little short, and STL time is a little suspended, so all of them are implemented manually. The code is as follows

#include <bits/stdc++.h>
using namespace std;
int a[55][100005];
int k[55];
int main()
{
    //freopen("t5.in","r",stdin);
   //freopen("t5.out","w",stdout);
    int i,j,n,m,p;
    while(cin>>n){
    memset(a,-1,sizeof(a));
    memset(k,-1,sizeof(k));
    for(i=1;i<=n;i++)
    {
        scanf("%d",&k[i]);
        for(j=1;j<=k[i];j++)
        {
            scanf("%d",&a[i][j]);
        }
        sort(a[i]+1,a[i]+k[i]+1);
        for(j=1,p=0;j<=k[i];j++)
        {
            if(a[i][j]!=a[i][j+1]){p++;a[i][p]=a[i][j];}
        }
        k[i]=p;
    }
    cin>>m;
    while(m--)
    {
        int l,r;
        scanf("%d%d",&l,&r);
        int cnt=0;
        for(i=1,j=1;i<=k[l]&&j<=k[r];)
        {
            if(a[l][i]==a[r][j]){cnt++;i++;j++;}
            else if(a[l][i]>a[r][j])j++;
            else i++;
        }
        printf("%.2f",(double)(100*cnt)/(k[l]+k[r]-cnt));
        puts("%");
    }
  }
    return 0;
}

Teacher's

#include <bits/stdc++.h>
using namespace std;
int n,m,t,k,p,q;
double ans[55][55];
unordered_set<int> s[55];
int main()
{
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    cin>>n;
    for(int i=1;i<=n;i++) {
        for(int j=1;j<=n;j++) {
            if(i==j)
                ans[i][j]=1;
            else
                ans[i][j]=-1;
        }
    }
    for(int i=1;i<=n;i++) {
        cin>>m;
        while(m--) {
            cin>>t;
            s[i].insert(t);
        }
    }
    cin>>k;
    while(k--) {
        cin>>p>>q;
        if(ans[p][q]!=-1) {
            printf("%.2f%%\n",ans[p][q]);
            continue;
        }
        int num1=0, num2=s[q].size();
        for(auto t:s[p]) {
            if(s[q].find(t)!=s[q].end()) {
                num1++;
            } else {
                num2++;
            }
        }
        printf("%.2f%%\n",ans[p][q]=ans[q][p]=double(num1)/num2*100);
    }
    return 0;}

Mingming random number set

Mingming wants to invite some students to do a questionnaire survey together in school. For the objectivity of the experiment, he first generated N random integers (n < = 100) between 1 and 1000 by computer. For the repeated numbers, only one is reserved, and the rest of the same numbers are removed. Different numbers correspond to different student numbers. Then, rank the numbers from small to large, and go to the students to do the survey in the arranged order. Please help Mingming to complete the work of "de duplication" and "sorting".
Multiple groups of input data
There are 2 lines in each group, and the first line is a positive integer, indicating the number of generated random numbers: N
Line 2 has N positive integers separated by spaces, which are generated random numbers.
Each group of output is also 2 lines, the first line is a positive integer M, which represents the number of different random numbers. The second line is M positive integers separated by spaces, which are different random numbers arranged from small to large.
10
20 40 32 67 40 20 89 300 400 15
8
15 20 32 40 67 89 300 400

#include <iostream>
#include<bits/stdc++.h>
using namespace std;
set<int>s;
set<int>::iterator it;
int main()
{
    int n;
    while(cin>>n)
    {
        s.clear();
        int buf;
        for(int i=0;i<n;i++)
        {
            cin>>buf;
            s.insert(buf);
        }
        cout<<s.size()<<endl;
        it=s.begin();
        for(int i=1;i<s.size();i++)
        {
            cout<<*it<<" ";
            it++;
        }
        cout<<*it<<endl;
    }
    return 0;
}

sort+unique

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n,i,l,a[105];
    while(cin>>n)
    {
        for(i=0;i<n;i++)
            cin>>a[i];
        sort(a,a+n);
        l=unique(a,a+n)-a;
        printf("%d\n",l);
        for(i=0;i<=l-2;i++)
        printf("%d ",a[i]);
        printf("%d\n",a[l-1]);
    }
    return 0;
}

K small integer - SET

#include <iostream>
#include<bits/stdc++.h>
using namespace std;
set<int>s;
set<int>::iterator it;
int main()
{
    int n;
    cin>>n;
    int k;
    cin>>k;
    int buf;
    for(int i=0; i<n; i++)
    {
        cin>>buf;
        s.insert(buf);
    }
    int i=1;
    for(it=s.begin(); it!=s.end(); i++,it++)
    {
        if(i==k)
            break;
    }
    if(it==s.end())

    {
        cout<<"NO RESULT";
    }
    else
    {
        cout<<*it;
    }
    return 0;
}

unique practice
Author: Morales drives disaster tank to wipe out thousands of troops time: 2020-02-13 06:04:04pm

#include<bits/stdc++.h>
using namespace std;
int n, k, a[10010];
int main()
{
    ios::sync_with_stdio(false);
    cin>>n>>k;
    for(int i=0; i<n; i++)
        cin>>a[i];
    sort(a, a+n);
    int t=unique(a, a+n)-a;
    if(k<=t) cout<<a[k-1]<<endl;
    else cout<<"NO RESULT"<<endl;
    return 0;
}

This is the source code of the unique function:

template<typename _ForwardIterator, typename _BinaryPredicate>
  _ForwardIterator
  __unique(_ForwardIterator __first, _ForwardIterator __last,
       _BinaryPredicate __binary_pred)
  {
    // Skip the beginning, if already unique.
    __first = std::__adjacent_find(__first, __last, __binary_pred);
    if (__first == __last)
  return __last;

    // Do the real copy work.
    _ForwardIterator __dest = __first;
    ++__first;
    while (++__first != __last)
  if (!__binary_pred(__dest, __first))
    *++__dest = _GLIBCXX_MOVE(*__first);
    return ++__dest;
  }

Word memory set map

#include <iostream>
#include<bits/stdc++.h>
using namespace std;
set<string>s;
set<string>::iterator it;
int main()
{
    int n;
    cin>>n;
    int choose;string buf;
    for(int i=0;i<n;i++)
    {
        cin>>choose;
        if(choose==0)
        {

            cin>>buf;
            s.insert(buf);
        }
        if(choose==1)
        {

            cin>>buf;
            if(s.count(buf))
            {
                cout<<"YES"<<endl;
            }else{
                cout<<"NO"<<endl;
            }

        }
    }
    return 0;
}

Train dispatching SET

//Set a set in which the last train sequence number in each track is saved. Give you a line of order. First, judge whether it is listed into the existing rail with decreasing serial number. If possible, update the serial number of the last train passing through the rail. Otherwise, create a new rail and put the serial number of the last train passing through the rail into the set.

#include <iostream>
#include<bits/stdc++.h>
using namespace std;
set<int>s;
set<int>::iterator it;
queue<int>q[10000];
int cnt=0;
int find_if_could(int x)
{
    for(int i=0;i<cnt;i++)
    {
        if(q[i].back()<x)
        {
            return i;
        }
    }
    return -1;
}
int main()
{
    int n;
    cin>>n;
    int buf;
    for(int i=0;i<n;i++)
    {
        cin>>buf;
         if(s.lower_bound(buf)!=s.end())

        {

            s.erase(s.lower_bound(buf));

            s.insert(buf);

        }

        else s.insert(buf);
    }
    cout<<s.size()<<endl;
    return 0;
}
//Set a set in which the last train sequence number in each track is saved. Give you a line of order. First, judge whether it is listed into the existing rail with decreasing serial number. If possible, update the serial number of the last train passing through the rail. Otherwise, create a new rail and put the serial number of the last train passing through the rail into the set.

NOIP topic naval battle-SET-1

ljw de
Talk about the mistakes:
May repeat the same question / may have students repeat the registration how to solve? set de duplication is OK. If PE is OK, it may be because when the answer is output, after the last number of each line is output, another space is also output

#include <bits/stdc++.h>
using namespace std;
const int N=1e3+10;
int n,m,x,q,num,type;
set<int>s[N],quanji,ans;
set<int>::iterator it;
int main()
{
    ios::sync_with_stdio(false);
    cin>>n>>m;
    for(int i=1;i<=n;i++)
    {
        cin>>num;
        for(int j=1;j<=num;j++)
        {
            cin>>x;//The number of questions that contestant i has done is x (the questions must be de duplicated, otherwise it is not easy to count the number of times each question has been done later)
            s[i].insert(x);
        }
    }
    for(int i=1;i<=m;i++)
        quanji.insert(i);//Complete preprocessing
    cin>>q;
    while(q--)
    {
        cin>>type>>num;
        ans=quanji;//An equal sign can be used to assign values between two set s
        if(type==0)//In the competition, he should make sure that none of the students have ever done the questions
        {
            for(int i=1;i<=num;i++)
            {
                cin>>x;//Player number is x
                for(it=ans.begin();it!=ans.end();)
                {
                    if(s[x].count(*it))ans.erase(it++);//Sift out all the questions that player x has done
                    else it++;
                }
            }
        }
        else//In training, he should make sure that every student has done all the questions
        {
            for(int i=1;i<=num;i++)
            {
                cin>>x;//Player number is x
                for(it=ans.begin();it!=ans.end();)
                {
                    if(!s[x].count(*it))ans.erase(it++);//Sift out all the questions that player x hasn't done
                    else it++;
                }
            }
        }
        for(it=ans.begin();it!=ans.end();it++)
            printf("%d ",*it);
        printf("\n");
    }
    return 0;
}

I'll go to oj to check myself. It's too long. It breaks down as soon as I write it

Published 4 original articles, won praise 2, visited 32
Private letter follow

Posted by juniorbisono on Tue, 18 Feb 2020 05:06:40 -0800