A - Rotate
题意:给x,y,z三个字符,求 a b c + b c a + c a b abc+bca+cab abc+bca+cab 的值.
#include<bits/stdc++.h>
#define ios ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
const int N = 150000 + 10;
int main()
{
ios
string s;
cin>>s;
int ans = 0;
int x = 0;
x = x * 10 + (s[0] - '0');
x = x * 10 + (s[1] - '0');
x = x * 10 + (s[2] - '0');
ans += x;
x = 0;
x = x * 10 + (s[1] - '0');
x = x * 10 + (s[2] - '0');
x = x * 10 + (s[0] - '0');
ans += x;
x = 0;
x = x * 10 + (s[2] - '0');
x = x * 10 + (s[0] - '0');
x = x * 10 + (s[1] - '0');
ans += x;
cout<<ans<<"\n";
return 0;
}
B - Climbing Takahashi
题意:给一个数组,从第0位开始,求一个连续的严格递增的最后一位的值。
#include<bits/stdc++.h>
#define ios ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
const int N = 1e5 + 10;
int n,a[N];
int main()
{
ios
cin>>n;
int ans = 0;
for(int i = 1 ; i <= n ; i ++ ) cin>>a[i];
ans = a[1];
for(int i = 1 ; i <= n ; i ++ )
{
if(a[i+1] > a[i])
{
ans = a[i+1];
}
else break;
}
cout<<ans<<"\n";
return 0;
}
C - The Kth Time Query
题意:给一个长为n的数组,q次询问,每次询问找x在数组中第k次出现的位置。
思路:存储x的位置和出现次数,排序后二分查找即可
#include<bits/stdc++.h>
#define ios ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
const int N = 2e5 + 10;
int n,q;
struct node
{
int a,idx,cnt;
}no[N];
bool cmp(node a,node b)
{
if(a.a == b.a) return a.cnt < b.cnt;
return a.a<b.a;
}
map<int,int> mp;
int main()
{
ios
cin>>n>>q;
for(int i = 1 ; i <= n ; i ++ )
{
cin>>no[i].a;
no[i].idx = i;
mp[no[i].a] ++;
no[i].cnt = mp[no[i].a];
}
sort(no + 1 , no + n + 1 , cmp);
while(q--)
{
int x,k;
cin>>x>>k;
if(mp[x] == 0)
{
cout<<-1<<"\n";
continue;
}
else if(mp[x] < k)
{
cout<<-1<<"\n";
continue;
}
int l = 1 , r = n;
int ans = 0;
while(l <= r)
{
int mid = l + r >> 1;
if(no[mid].a == x && no[mid].cnt == k)
{
ans = no[mid].idx;
break;
}
else if((no[mid].a == x && no[mid].cnt > k)|| no[mid].a > x) r = mid - 1;
else l = mid + 1;
}
cout<<ans<<"\n";
}
return 0;
}
D - Multiply and Rotate
题意:给一个十进制下的a和n。从1开始每次可以进行如下两种操作
-
x *= a
-
将x的最后一位移至第一位,例如123 变成 312,注意x要大于10且不是10的倍数
思路:对于每种情况爆搜即可,注意要使用bfs
#include<bits/stdc++.h>
#define ios ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
typedef long long ll;
const int N = 1e6 + 10;
int a,n;
struct node
{
ll x,cnt;
}no[N];
queue<node> qu;
bool flag;
bool st[N];
void bfs()
{
qu.push({1,0});
while(qu.size())
{
auto t = qu.front();
qu.pop();
if(t.x == n)
{
cout<<t.cnt<<"\n";
flag = true;
return ;
}
if(t.x > 1e6 || st[t.x]) continue;
st[t.x] = true;
if(t.x % 10 != 0 && t.x > 10)
{
int b = t.x;
int w = t.x % 10;
ll x2 = 0;
x2 += w;
string s = to_string(b);
for(int i = 0 ; i < s.size() - 1 ; i ++ )
{
x2 = x2 * 10 + (s[i] - '0');
}
qu.push({x2,t.cnt+1});
}
if(t.x*a < 1e6) qu.push({t.x*a , t.cnt+1});
}
}
int main()
{
cin>>a>>n;
bfs();
if(!flag) cout<<-1;
return 0;
}
E - MST + 1
题意:给定n个点,m条边,建立最小生成树(MST),q次询问,问每次询问的边是否存于最小生成树中,每次询问不会改变最小生成树,允许出现重边和自环。
思路:可以将所有的q+m条边一起排序,进行正常的kruskal建树操作,注意需要将每条边标记一下位置,如果位置大于m,则只判断建树是否会用到,小于m的边则进行正常的kruskal操作,最后一起输出结果。
#include<bits/stdc++.h>
#define ios ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
typedef long long ll;
const int N = 1e6 + 10;
int n,m,q;
int p[N];
int find(int x)
{
if(p[x] != x)
{
p[x] = find(p[x]);
}
return p[x];
}
bool st[N];
struct node
{
int a,b,w,id;
bool operator< (const node &W)const
{
return w<W.w;
}
}no[N];
int main()
{
ios
cin>>n>>m>>q;
for(int i = 0 ; i <= n ; i ++ ) p[i] = i;
for(int i = 0 ; i < m + q; i ++ )
{
int a,b,c;
cin>>a>>b>>c;
no[i] = {a,b,c,i};
}
sort(no,no+m+q);
for(int i = 0 ; i < m + q; i ++ )
{
int a = no[i].a , b = no[i].b;
a = find(a) , b = find(b);
if(a != b)
{
if(no[i].id >= m) st[no[i].id - m] = true;//不实际加边
else p[a] = b;
}
}
for(int i = 0 ; i < q ; i ++ )
{
if(st[i]) puts("Yes");
else puts("No");
}
return 0;
}