Codeforces Round #854 by cybercats (Div. 1 + Div. 2) A-B题解

比赛链接

A

可以发现,每次出去的顺序一定是按照 \(n -> 1\) 的顺序。因为新加入的东西只会放在最前面。相应的,如果其已在序列中,则这个操作不会对 \(1~n\) 的信息产生影响。
所以只需要统计当前累计出现过多少个不同的新消息即可。

#include <bits/stdc++.h>
using namespace std;
#define N 100010

int n, m; 
map<int, int> ans; 

int main(){
    int T; cin >> T; 
    while(T--){
        ans.clear(); 
        cin >> n >> m; 
        vector <int> G; 
        int now = n; 
        for(int i = 1; i <= m; i++){
            int x; cin >> x; 
            G.push_back(x); 
        }
        bitset <N> vis(0); 
        int num = 0; 
        for(int i = 0; i < G.size(); i++){
            if(!vis[G[i]]){  
                ans[now] = i + 1; 
                vis[G[i]] = 1; 
                now--; 
            }
        }
        for(int i = 1; i <= n; i++){
            printf("%d ", ans[i] == 0 ? -1 : ans[i]); 
        }
        cout << endl; 
    }
    return 0; 
}

B

首先特判,同时含有 \(1\) 和其他数字肯定是不行的。其次,显然,我们不能用小数字除以大数字,这样一定会产生 \(1\)
那么不妨如此操作:每次将当前最大的数字除以当前最小的数字(动态的),并统计当前最小数字出现的次数。当其出现次数为 \(n\) 时,那么就可以退出了。
(避坑:最小值不一定是 \(2\), 也可以是除到 \(3\) 之类的,因此必须通过出现次数来判断。)

每个数字除最小值显然是不超过 \(log\) 级别的,因此可以在 \(30n\) 次内完成。

···cpp

include <bits/stdc++.h>

using namespace std;

define N 1001

int a[N];
int n;

struct node{
int x;
int pos;

bool operator < (const node &a) const{
    if(x == a.x) return pos < a.pos; 
    else return x > a.x; 
}

} ;

map<int, int> g;

int main(){
int T; cin >> T;
while(T--){
g.clear();
cin >> n;
for(int i = 1; i <= n; i++)
cin >> a[i];
bool flag = 0, hav = 0;
bool ok = 1;
for(int i = 1; i <= n; i++){
g[a[i]]++;
if(a[i] == 1) hav = 1;
if(a[i] != 1) flag = 1;
}
for(int i = 2; i <= n; i++)
if(a[i] != a[i-1]) ok = 0;
if(hav && flag){
puts("-1");
continue;
}
else if(hav && !flag){
puts("0");
continue;
}
else if(ok){
puts("0");
continue;
}
set s;
vector <pair<int, int> > G;
for(int i = 1; i <= n; i++)
s.insert((node){a[i], i});
while(233){
auto t = s.end(); t--;
if(g[t->x] == n) break;
auto it = s.begin();
int tmp = it->x;
int pos = it->pos;
G.push_back(make_pair(it->pos, t->pos));
g[tmp]--;
tmp = ceil((double)tmp / (double)t->x);
g[tmp]++;
s.erase(it);
s.insert((node){tmp, pos});
}
printf("%d\n", G.size());
for(auto it : G){
cout << it.first << " " << it.second << endl;
}
}
return 0;
}

posted @ 2023-02-28 22:45  雪之下,树之旁  阅读(44)  评论(0编辑  收藏  举报