10 条题解
-
31
题解两三条,细节提一条 若是没有赞,作者两行泪!!!
#include <iostream> #include <stdio.h> #include <algorithm> using namespace std; int n,m,x,ans,l,r,a[1005],b[1005]; int main() { cin>>m>>n; l=0;r=0; for (int i=1;i<=n;i++) { while(1){cout<<"你居然抄题解!!!!“<<endl; scanf("%d",&x); if (a[x]==0) { ans++; r++;b[r]=x;a[x]=1; if (r>m) {l++;a[b[l]]=0;} } } cout<<ans; return 0V0; }
-
9
#include <bits/stdc++.h> using namespace std; const int MAXN = 1123; int m, n, q[MAXN], h, t, ans; bool inq[MAXN]; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> m >> n; while (n--) { int x; cin >> x; if (!inq[x]) { q[t++] = x; inq[x] = true; if (t - h > m) { inq[q[h++]] = false; } ans++; } } cout << ans; return 0; }
-
6
#include <bits/stdc++.h> using namespace std; queue<int> q;//我超前用了一点,这样会轻松一点。。。。。。 bool a[1005];//标记该单词是否在内存中 int main() { int n,m; int ans = 0; cin >> m >> n; int x; for (int i = 1;i <= n;i++) { cin >> x; if (int(q.size())< m)//这里不强制转换一下会有提示,但也可以AC { if (a[x] == 1)//这里和下面的该语句可以放在if外面,cin之后直接判断 { continue;//接上:因为写的时候忘了,所以也不想改了,反正都能过 } else { ans++; q.push(x); a[x] = 1; } }//如果字典没满 else//如果字典满了 { if (a[x] == 1) { continue; } else { ans++; a[q.front()] = 0; q.pop();//先把最前一个删掉,并设置为没有该词 q.push(x); a[x] = 1;//把词加入内存 } } } cout << ans;//输出查字典的次数即可 return 0; }//勿要抄袭,切记 //by hetao4582353 //If you are a good person,please do not copy it. //No pain,no gain.别忘点赞
-
3
可以使用vector存储单词
所以查找便可以用二分查找
#include <bits/stdc++.h> using namespace std; int n,m,x,ans; vector<int>v; bool check(vector<int>v1,int x1){ int l=0,r=v1.size()-1,mid; sort(v1.begin(),v1.end()); while (l<=r){ mid=(r+l)/2; if (v1[mid]==x1)return 0; if (v1[mid]>x1)r=mid-1; else l=mid+1; } return 1; } int main(){ cin>>m>>n; for (int i=1;i<=n;i++){ cin>>x; if (check(v,x)){ v.push_back(x); ans++; } if ((int)v.size()>m)v.erase(v.begin()); } cout<<ans; return 0; }
-
2
本题使用队列来模拟即可,由于需要判断新当前单词是否已经在内存中,因此需要用一个数组inq来标记每个数字是否在队列里面。
代码
#include <bits/stdc++.h> using namespace std; const int MAXN = 1123; int m, n, q[MAXN], h, t, ans; bool inq[MAXN]; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> m >> n; while (n--) { int x; cin >> x; if (!inq[x]) { q[t++] = x; inq[x] = true; if (t - h > m) { inq[q[h++]] = false; } ans++; } } cout << ans; return 0; }
-
1
按照题意模拟即可
核心代码:
while(n--){ bool find=false; scanf("%d",&k); for(int i=L;i<R;i++) if(que[i]==k) find=true; if(!find) cnt++,que[R++]=k; if(R-L>m) L++; }
循环队列
for(int i = 0;i < n;++i){ int tmp; scanf("%d", &tmp); if(hash[tmp]) continue; hash[tmp] = 1; if(head == tail && i ) hash[zhan[tail]] = 0,tail = (tail + 1) % m; //删除标记。出队 zhan[head] = tmp, head = (head + 1) % m; //入队
-
-14
正宗的模拟队列解法。。。 //后的语句建议运行一下,看看效果
#include<bits/stdc++.h> using namespace std; int m,n,a[1005],sum; int que[100005],head=1,tail; bool book(int x){ // cout<<head<<'-'<<tail<<' '; for (int j=head;j<=tail;j++){ if (x==que[j])return 0; } return 1; } int main(){ cin>>m>>n; for (int i=1;i<=n;i++)cin>>a[i]; for (int i=1;i<=n;i++){ // for (int j=head;j<=tail;j++)cout<<que[j]<<' '; if (book(a[i])){ if (tail-head+1==m)head++; que[++tail]=a[i]; sum++; // cout<<a[i]<<'-'<<sum<<endl; } } // cout<<endl; cout<<sum; return 0; }
- 1
信息
- ID
- 48
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 4
- 标签
- (无)
- 递交数
- 1619
- 已通过
- 715
- 上传者