CodeForces 1234B1
有n条消息,类似QQ的聊天框,每收到一个当前聊天框里没有的联系人的消息就加入聊天框,但一个聊天框只能最多只能有k个不同的联系人,不同的是,每收到一条聊天框里已有联系人的消息,该联系人不需要置顶,聊天框顺序不变,如果聊天框已满,则删除最先加入的那个,求收完所有消息时聊天框里有几个联系人即他们的顺序
#include<bits/stdc++.h>
using namespace std;
vector<int> h;
int n,m;
int main()
{
scanf("%d%d",&n,&m);
h.clear();
int x;
for(int i=1;i<=n;i++)
{
scanf("%d",&x);
vector<int> ::iterator it=find(h.begin(),h.end(),x);
if(it!=h.end()) continue;
else
{
if(h.size()<m) h.insert(h.begin(),x);
else
{
h.pop_back();
h.insert(h.begin(),x);
}
}
}
cout<<h.size()<<endl;
for(int i=0;i<h.size();i++) cout<<h[i]<<" ";
cout<<endl;
return 0;
}
头一次深入了解了vector之后,才发现我对h.end()的返回值一直有误解,它返回的并不是最后一个元素的位置,而是最后一个元素的位置再加一,find函数的写法和sort类似,第二个位置要填入末位置加一,找到等于X的元素,就返回该元素在容器中的位置,找不到则返回末位置加一,所以 it!=h.end() 表示找到了等于x的元素,vector ::iterator it是定义了一个迭代器,类似普通int 循环中 i 遍历数组作用,用来遍历vector中的元素
CodeForces 1234B2
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ios::sync_with_stdio(false);
int n, k;
deque<ll>dq;
set<ll>st;
cin >> n >> k;
for (int i = 0; i < n; i++) {
int num;
cin >> num;
int nn=st.size();
st.insert(num);
if (st.size() == nn)
continue;
if (dq.size() == k) {
st.erase(dq.back());
dq.pop_back();
}
dq.push_front(num);
st.insert(num);
}
cout << dq.size() << endl;
while (!dq.empty()) {
cout << dq.front() << " ";
dq.pop_front();
}
return 0;
}
set不能有重复的元素,所以可以用set先判断,再决定是否插入deque,set是insert和erase,deque是push_front/back和pop_back/front
CodeForces 1234C
给六种类型的水管头, 多组输入样例, 问给定的水管能否从1, 1连通到2, n, 输出yes,no
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main ( ) {
int t;
string s[3];
cin >> t;
while ( t-- ) {
int n;
cin >> n;
cin >> s[0] >> s[1];
bool ans = true;
int row = 0; // 0为第一行, 1为第二行
for ( int col = 0; col < n; ++col ) {
if ( s[row][col] == '1' || s[row][col] == '2' )continue;
else {
row = 1 - row; // 换行
if ( s[row][col] == '1' || s[row][col] == '2' ) { // 换过行是1,2堵死了, 那么直接终止
ans = false;
break;
}
}
}
cout << ( ans&&row ? "Yes" : "No") << endl;
}
return 0;
}
CodeForces 1234D
两种操作,一种是改变某个位置的字符,一个是计算某个区间[l,r]内的不同字符数量因为数据比较大所以直接暴力肯定是不行的,我们可以把用set把每个字母的位置按照顺序记下来,对于操作1,我们可以删除原来字符对应的位置然后再添加新字符对应的位置,对于操作2,我么只要判断一下每个字符的位置是否在区间里面就行了
#include<bits/stdc++.h>
using namespace std;
string s;
int q,t,l,r,pos,cnt;
char c;
set<int> alp[26];
int main(){
ios::sync_with_stdio(false);
cin>>s;
int len=s.size();
for(int i=0;i<len;i++){
alp[s[i]-'a'].insert(i);
}
cin>>q;
while(q--){
cin>>t;
if(t==1){
cin>>pos>>c;
pos--;
alp[s[pos]-'a'].erase(pos);
s[pos]=c;
alp[s[pos]-'a'].insert(pos);
}
else{
cin>>l>>r;
l--,r--;
cnt=0;
for(int i=0;i<26;i++){
auto it=alp[i].lower_bound(l);
if(it!=alp[i].end()&&*it<=r) cnt++;
}
printf("%d\n",cnt);
}
}
return 0;
}
CodeForces 1234E
给m个数,每个数在1-n之间,有n个排列,求n次排列中,这m个数的相邻两个数之间的位置的绝对值之差。
#include<bits/stdc++.h>
using namespace std;
#define f first
#define s second
typedef long long LL;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;
const int N = 2e5 + 10;
int n,m,a[N],pos[N];
map<int,int> lin[N];
int main(){
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
//freopen("data.in","r",stdin);
//freopen("data.out","w",stdout);
cin>>n>>m;
for(int i = 1 ; i <= n; i ++) pos[i] = i;
for(int i = 0 ;i < m ;i ++) {
cin>>a[i];
}
for(int i = 0 ; i < m - 1;i ++) {
if(a[i] != a[i + 1] ){
if(!lin[a[i]].count(a[i + 1])) lin[a[i]][a[i+1]] = 0 ,lin[a[i+1]][a[i]] = 0;
lin[a[i]][a[i+1]]++ ;
lin[a[i+1]][a[i]]++ ;
}
}
}
LL sum = 0 ;
vector<LL> ans;
for(int i = 0 ; i < m - 1; i ++) sum += abs(pos[a[i]] - pos[a[i+1]]);
ans.push_back(sum);
for(int i = 1 ; i < n ; i ++){
//cout<<sum<<' ';
for(auto u : lin[i]) sum -= (LL)(abs(pos[u.f] - pos[i]) ) * u.s;
for(auto u : lin[i + 1]) sum -= (LL)(abs(pos[u.f] - pos[i + 1])) * u.s;
swap(pos[i] , pos[i + 1]);
//cout<<sum<<endl;
for(auto u : lin[i]) sum += (LL)(abs(pos[u.f] - pos[i]) ) * u.s;
for(auto u : lin[i + 1]) sum += (LL)(abs(pos[u.f] - pos[i + 1])) * u.s;
ans.push_back(sum);
}
for(auto u : ans) cout<<u<<' ';cout<<endl;
return 0;
}