Summary
Maybe yesterday's question is less, today's question is a lot more, and also crazy TLE / (ㄒ o ㄒ)\~~
Information
No. | Title | AC/Submit |
---|---|---|
A | Mingming random number set | 60/101 |
B | K small integer - SET | 57/89 |
C | Word memory set map | 55/63 |
D | Train dispatching SET | 32/67 |
E | Similar number SET simple version SET | 31/88 |
F | NOIP topic naval battle-SET-1 | 10/64 |
G | Exponential Series set | 28/44 |
Problem A: explicit random number set (743) [60/101]
Tips
Be familiar with set and its iterator usage.
By using the non repetition + order feature of set, insert set directly.
Code
#include <bits/stdc++.h> using namespace std; int main() { set<int>s; int n,i,x; while(cin>>n) { s.clear(); for(i=1;i<=n;i++) { cin>>x; s.insert(x); } printf("%d\n",s.size()); for(set<int>::iterator it=s.begin();it!=s.end();it++) { if(it!=s.begin())printf(" "); printf("%d",*it); } printf("\n"); } return 0; }
Problem B: K small integer - SET (1684) [57/89]
Tips
In fact, this problem can be counted in the iterator, and can not be tossed into the array.
Code
#include <bits/stdc++.h> using namespace std; int main() { set<int>s; int ans[10000]; int n,i,x,an,k; while(cin>>n>>k) { an=0; s.clear(); for(i=1;i<=n;i++) { cin>>x; s.insert(x); } for(set<int>::iterator it=s.begin();it!=s.end();it++) { ans[an++]=*it; } if(k<=an&&k>0)printf("%d\n",ans[k-1]); else printf("NO RESULT\n"); } return 0; }
Problem C: word memory set map (2117) [55 / 63]
Tips
No difficulty, sign in question + 1.
Code
#include <bits/stdc++.h> using namespace std; int main() { set<string>s; int n,o; string word; cin>>n; while(n--) { cin>>o>>word; if(o==0) { s.insert(word); } else { if(s.count(word))printf("YES\n"); else printf("NO\n"); } } return 0; }
Problem D: train dispatching SET (1680) [32/67]
Tips
This question is a little difficult. Analyze it.
At the entrance, there are a group of trains {8, 4, 2, 5, 3, 9, 1, 6, 7} lining up in order to enter. They are required to go out in the order of decreasing sequence number.
First of all, it must be ensured that the trains in each track at any time must be in ascending order (number from small to large), so as to ensure that the trains with large number can go out first.
Therefore, it is only necessary to record the number of the last train on each track. If the number of the train to be entered is less than the number of the last train on the existing track, then the train can directly enter the track. If the number of trains to be entered is greater than the number of trains on all tracks, then another track needs to be opened.
Code
#include <bits/stdc++.h> using namespace std; int main() { set<int>s; set<int >::iterator it; int n,tmp; cin>>n; while(n--) { cin>>tmp; if(s.empty())s.insert(tmp); else { it=s.lower_bound(tmp); if(it==s.end())s.insert(tmp); else { s.erase(it); s.insert(tmp); } } } cout<<s.size(); return 0; }
Problem E: similar data set simple version - SET (2119) [31/88]
Tips
I don't know why the title has been used several times. I guess it's a bit of a problem at the beginning.
In the last version, the method of array + unique is chosen instead of set.
The original method of this question should be set.
Code
#include <bits/stdc++.h> using namespace std; int main() { int arr[50][5001]; double ans[50][50]={0},r; int n,num,s1,s2,same,tmp,diff,pos; scanf("%d",&n); for(int i=0;i<n;i++) { scanf("%d",&arr[i][5000]); for(int j=0;j<arr[i][5000];j++) { scanf("%d",&arr[i][j]); } sort(arr[i],arr[i]+arr[i][5000]); arr[i][5000]=unique(arr[i],arr[i]+arr[i][5000])-arr[i]; } scanf("%d",&n); while(n--) { scanf("%d %d",&s1,&s2); if(ans[s1-1][s2-1]!=0) { printf("%.2f%\n",ans[s1-1][s2-1]); continue; } same=diff=0; for(int i=0;i<arr[s2-1][5000];i++) { pos=lower_bound(arr[s1-1],arr[s1-1]+arr[s1-1][5000],arr[s2-1][i])-arr[s1-1]; if(pos<=arr[s1-1][5000]&&arr[s1-1][pos]==arr[s2-1][i])same++; } r=same*100.0/(arr[s1-1][5000]+arr[s2-1][5000]-same); ans[s1-1][s2-1]=ans[s2-1][s1-1]=r; printf("%.2f%\n",r); } return 0; }
Problem F: Naval Battle of noip - SET-1 (1679) [10/64]
Tips
At the beginning, I didn't know why I handed it in. Later, local test data found that if I didn't RE, I would also TLE.
After learning from the idea of czy, I went directly to the array to find the answer. I was going to deal with it.
My version of set is a student - > topic, and the code of the boss is a student - > topic. The data range is almost the same, so you can save it any way.
Code
#include <bits/stdc++.h> using namespace std; int n,m,p,k,num,type,tmp,ok; set<int>s[1000]; int sp[1000],spp; int main() { scanf("%d %d",&n,&m); for(int i=0;i<n;i++) { scanf("%d",&num); for(int j=0;j<num;j++) { scanf("%d",&tmp); s[i].insert(tmp); } } scanf("%d",&k); while(k--) { scanf("%d %d",&type,&num); spp=0; for (int i=0;i<num;i++) { scanf("%d",&tmp); sp[spp++]=tmp-1; } for(int i=1;i<=m;i++) { ok=1; for (int j=0;j<spp;j++) { if (type==0&&s[sp[j]].count(i)) { ok=0; break; } if (type==1&&!s[sp[j]].count(i)) { ok=0; break; } } if(ok)printf("%d ",i); } printf("\n"); } return 0; }
Problem G: Index Series set (1677) [28/44]
Tips
It is yesterday's map problem with a set suffix. See yesterday's Problem F for details.
NEFU freshman winter holiday training eleven (map) February 17, 2020
Code
#include <bits/stdc++.h> using namespace std; int main() { int n; long long maxa=0,num,cnt=0,ans; map<long long,int>ma; scanf("%lld",&n); while(n--) { scanf("%lld",&num); ma[num]++; if(ma[num]==2) { for(long long i=num;ma[i]==2;i++) { ma[i]-=2; ma[i+1]++; } } } for(map<long long,int>::iterator it=ma.begin();it!=ma.end();it++) { if(it->second!=0) { cnt++; if(it->first>maxa)maxa=it->first; } } printf("%lld",maxa<cnt?0:maxa-cnt+1); return 0; }