Codeforces Round 723
A.Mean Inequality
题目大意
给定一个有2n个元素的数组a,要求你将其重新排列构成一个新数组 b 对于数组 b 而言
b
i
≠
b
i
−
1
+
b
i
+
1
2
b_i \neq \frac{b_{i-1}+b_{i+1}}{2}
bi=2bi−1+bi+1
b可以看作是首尾相接的。
解题思路
将a数组排序,之后每次拿出当前数组的最大值与最小值插入新的数组中,输出即可。
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<cmath>
#include<cstring>
#include<string>
#include<cstdio>
#include<stack>
using namespace std;
typedef unsigned long long ll;
typedef pair<ll, ll> PII;
const int mod = 1e9 + 7;
const int N = 2e5 + 100;
const int M = 1e6 + 10;
int a[200];
int main(){
std::ios::sync_with_stdio(false);
int t;
cin >> t;
while(t--){
int n;
cin>>n;
for (int i = 1; i <= 2 * n;i++){
cin >> a[i];
}
sort(a + 1, a + 2 * n + 1);
int tot1 = 1;
int tot2 = 2 * n;
vector<int> ans;
for (int i = 1; i <= n;i++){
ans.push_back(a[tot1]);
ans.push_back(a[tot2]);
tot1++;
tot2--;
}
for (int i = 0; i < ans.size();i++){
cout << ans[i] << ' ';
}
cout << endl;
}
return 0;
}
B.I Hate 1111
题目大意
给你一个数问能否用 11, 111,1111,11111,…这些数相加表示出来。
解题思路
易得 1111, 111111, 可以用11表示出来,111,11111可以用11表示余1,所以我们直接将给定的数除11,若余数不为0,那么除11的结果在除10,看看剩余的数够不够用111表出即可。
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<cmath>
#include<cstring>
#include<string>
#include<cstdio>
#include<stack>
using namespace std;
typedef unsigned long long ll;
typedef pair<ll, ll> PII;
const int mod = 1e9 + 7;
const int N = 2e5 + 100;
const int M = 1e6 + 10;
int a[200];
bool b[200];
int main(){
std::ios::sync_with_stdio(false);
ll t;
cin >> t;
ll dec = 11;
vector<ll> pp;
pp.push_back(dec);
for (int i = 2; i <= 9;i++){
dec += pow(10, i);
pp.push_back(dec);
}
reverse(pp.begin(), pp.end());
while (t--)
{
ll x;
cin >> x;
bool cann = 1;
if (x < 11)
{
cout << "NO" << endl;
continue;
}
else{
if(x%11==0){
cout << "YES" << endl;
continue;
}
else{
int num = x % 11;
int nu = x / 11;
nu /= 10;
if(nu>=num){
cout << "YES" << endl;
}
else{
cout << "NO" << endl;
}
}
}
}
return 0;
}
C12.Potions
题目大意
给定你一个长度为n的数组a,从左到右遍历,每个点都对应一个值 ai 每到一个点你可以选择将这个点的值加入到总值里或者跳过,总值必须要保证时刻大于等于0,求出最多可以加多少个点的值?
解题思路
直接遍历,对于大于等于0的数直接加进去,如果小于0,只要不让总值为负那么就加进去,反之在加进去的负数里最小的如果比它小,那么就把当前的数换进去,反之不动。
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<cmath>
#include<cstring>
#include<string>
#include<cstdio>
#include<set>
#include<stack>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
const int mod = 1e9 + 7;
const int N = 2e5 + 100;
ll a[N];
ll num[2010];
vector<int> G[2010];
int main(){
int n;
cin >> n;
priority_queue<ll> q;
int ans = 0;
ll sum = 0;
for (int i = 1; i <= n;i++){
cin >> a[i];
if(a[i]>=0){
sum += a[i];
ans++;
}
else{
if(sum+a[i]>=0){
q.push(-a[i]);
ans++;
sum += a[i];
}
else{
if(q.size()&&q.top()>-a[i]){
sum += q.top();
sum += a[i];
q.pop();
q.push(-a[i]);
}
}
}
}
cout << ans << endl;
return 0;
}
DELTIX Contest
A.Game of life
题目大意
给定一个 01 序列,在每个回合如果 0 的 两边只有一个1的话,那么它就会变成 1, 输出 m 个回合后的字符串。
解题思路
对于每一个点暴力遍历即可,因为n的范围较小,判断无法变化时直接跳出即可。
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<cmath>
#include<cstring>
#include<string>
#include<cstdio>
#include<set>
#include<stack>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
const int mod = 1e9 + 7;
const int N = 2e5 + 100;
char a[2010];
int main(){
int t;
cin>>t;
while(t--){
memset(a, 0, sizeof a);
int n, m;
cin >> n >> m;
for (int i = 1; i <= n;i++){
cin >> a[i];
}
while(m--){
bool cann = 0;
vector<int> tot;
for (int i = 1; i <= n;i++){
int num = 0;
if(a[i]=='0'){
if(a[i-1]=='1')
num++;
if(a[i+1]=='1')
num++;
}
if(num == 1){
cann = 1;
tot.push_back(i);
}
}
if(!cann){
break;
}
else{
for (int i = 0; i < tot.size();i++){
a[tot[i]] = '1';
}
}
}
for (int i = 1; i <= n;i++){
cout << a[i];
}
cout << endl;
}
return 0;
}
B.Lord of the Value
题目大意
给定一个长度为 n 的数组 a (n为偶数),每次可以选两个数 i,j(i < j)做以下两种操作中的一个
- ai = ai + aj
- aj = aj - ai
输出一种可行的方案使所有的数变为其相反数。
解题思路
n 为偶数可以提示可能存在某种潜藏的规律,观察可以发现,将所有数字两两分为一组,对每一组进行112112操作就可以
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<cmath>
#include<cstring>
#include<string>
#include<cstdio>
#include<set>
#include<stack>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
const int mod = 1e9 + 7;
const int N = 2e5 + 100;
ll a[2010];
int main(){
int t;
cin>>t;
while(t--){
int n;
cin >> n;
for (int i = 1; i <= n;i++){
cin >> a[i];
}
int tot = 2;
cout << 6 * n / 2 << endl;
while(tot<=n){
cout << "1 " << tot - 1 << ' ' << tot << endl;
cout << "1 " << tot - 1 << ' ' << tot << endl;
cout << "2 " << tot - 1 << ' ' << tot << endl;
cout << "1 " << tot - 1 << ' ' << tot << endl;
cout << "1 " << tot - 1 << ' ' << tot << endl;
cout << "2 " << tot - 1 << ' ' << tot << endl;
tot += 2;
}
}
return 0;
}
Compression and Expansion
题目大意
给定你n个数,每个数代表有序表的末尾的一个数,试着根据这些数字创造出一个有序表,答案可能不唯一,保证结果存在
解题思路
对于每个新输入的数如果为1,就将其加入一个新的子序列里,如果不为1,那么就从最深的子序列不断向上找,找到末尾比它小于1的数,加到这个子序列末尾。由于输入数据存在保证,不用担心这种方法会导致错误,用一个栈就可以维护。
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<cmath>
#include<cstring>
#include<string>
#include<cstdio>
#include<set>
#include<stack>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
const int mod = 1e9 + 7;
const int N = 2e5 + 100;
ll a[2010];
vector<int> G[2010];
int main(){
int t;
cin>>t;
while(t--){
int n;
cin >> n;
vector<int> ans;
for (int i = 1; i <= n;i++){
cin >> a[i];
if(a[i]==1){
ans.push_back(a[i]);
for (int i = 0; i < ans.size();i++){
if(i==0){
cout << ans[i];
}
else{
cout << '.' << ans[i];
}
}
cout << endl;
}
else{
while(ans.size()){
int tot = ans[ans.size() - 1];
if(tot==a[i]-1){
ans.pop_back();
ans.push_back(a[i]);
break;
}
else{
ans.pop_back();
}
}
for (int i = 0; i < ans.size();i++){
if(i==0){
cout << ans[i];
}
else{
cout << '.' << ans[i];
}
}
cout << endl;
}
}
}
return 0;
}