A.Casimir‘s String Solitaire
题意:
对于一个字符串要求用两种规则把他消除完。。。。
思路:
记录A,B,C的数量,使A和B的和等于C就可以了。
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
string s;
cin>>s;
int len=s.size(),cont=0,cont2=0;
for(int i=0;i<len;i++)
{
if(s[i]=='B')
cont++;
if(s[i]=='C' || s[i]=='A')
cont2++;
}
if(cont==cont2)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
return 0;
}
B.Shifting Sort
题意:
给你一个数组,要求是,在内部划定一个区域在这个区域内你可以进行左移,并且在n步内将数组进行由小到大的排序。
思路:
我是用了冒泡排序从后往前枚举,遇到一个逆序对(a[i] > a[j]且,i < j)就整体前移一个,由于这个题数据范围只有50,可以过。
#include<bits/stdc++.h>
using namespace std;
int a[51],tmp[51], b[51];
int main()
{
int T;
cin >> T;
while (T--)
{
int n;
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) tmp[i] = a[i], b[i] = a[i];
sort(tmp + 1, tmp + 1 + n);
bool flag = true;
for (int i = 1; i <= n; i++)
if (tmp[i] != a[i])
flag = false;
if (flag)
{
cout << 0 << endl;
continue;
}
int k = 0;
for (int i = n; i >= 1; i--)
for (int j = i - 1; j >= 1; j--)
if (b[i] < b[j])
{
int x = b[j];
for (int p = j; p <= i - 1; p++)
b[p] = b[p + 1];
b[i] = x;
k++;
}
cout << k << endl;
int l = 0, r = 0, d = 1;
for (int i = n; i >= 1; i--)
for (int j = i - 1; j >= 1; j--)
if (a[i] < a[j])
{
cout << j << ' ' << i << ' ' << 1 << endl;
int x = a[j];
for (int p = j; p <= i - 1; p++)
a[p] = a[p + 1];
a[i] = x;
}
}
return 0;
}
C. Ticks
思路:
模拟题,判断是否所有的 * 可以组成v和长度要大于k,。。。。。。。。。。没写完。
D.Productive Meeting
题意:
给你一个数组,第i位表示这第i个人有a【i】次与其他人的交谈机会每一次减一,直到为0时,这个人就会退出,问当只剩下一个人的时候,前面都是那几个人在交谈。
思路:
优先队列,每次取出两个队头元素,然后判断是否大于1,大于一都自减一,然后再放回队列,直到队列中 元素只有一个或没有,需要注意的是,我们刚开始往队列中加元素的时候只存大于0的元素
#include<bits/stdc++.h>
using namespace std;
const int N=1e6+1;
int a[N];
pair<int,int> ans[N];
int main()
{
int t;
cin >> t ;
while (t--)
{
int n;
cin >> n ;
int s = 0 ;
priority_queue<pair<int, int> > q ;
for (int i = 1; i <= n; i++)
{
cin >> a[i] ;
if (a[i])
q.push({a[i], i}) ;
}
int h = 0 ;
while (q.size() >= 2)
{
auto t1 = q.top() ;
q.pop() ;
auto t2 = q.top() ;
q.pop() ;
ans[ ++ h] = {t1.second, t2.second} ;
t1.first -- ;
t2.first -- ;
if (t1.first) q.push({t1.first, t1.second}) ;
if (t2.first) q.push({t2.first, t2.second}) ;
}
cout<<h<<endl;
for(int i=1;i<=h;i++)
{
cout << ans[i].first << " " << ans[i].second <<endl;
}
}
return 0;
}
E. Permutation Minimization by Deque
思路:
双端队列,每次比较和队头元素的大小,大于队头元素就插到队尾,小于就插到队首
#include<bits/stdc++.h>
using namespace std;
typedef pair<int, int>PII;
int a[200010];
int main()
{
ios::sync_with_stdio(false);
int T;
cin >> T;
while (T--)
{
memset(a, 0, sizeof a);
int n;
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
deque<int>q;
q.push_back(a[1]);
for (int i = 2; i <= n; i++)
if (a[i] < q.front()) q.push_front(a[i]);
else q.push_back(a[i]);
for (auto x : q) cout << x << ' ';
cout << endl;
}
return 0;
}
F.Array Optimization by Deque
题意:双端队列,但是需要判断题目给的反转数的最小个数。
思路:。。。。。。