标签:排序 技术 nic graphics ssi nice math change tom
a题只需要简单判断奇偶数的情况就可以解决。题目第一次读起来稍微有些复杂,理解后直接模拟过程即可
代码如下
#include <bits/stdc++.h>
using namespace std; int main() { int t; cin>>t;while (t--) { int n; char s[1005]; scanf("%d%s", &n, s); bool w; if (n & 1) { w = false; for (int i = 0; i < n; i += 2) if ((s[i] - ‘0‘) & 1) w = true; } else { w = true; for (int i = 1; i < n; i += 2) if (!((s[i] - ‘0‘) & 1)) w = false; } printf("%d\n", (int)!w + 1); } return 0; }
Jett is tired after destroying the town and she wants to have a rest. She likes high places, that‘s why for having a rest she wants to get high and she decided to craft staircases.
Staircase is a squared figure that consists of square cells. Each staircase consists of an arbitrary number of stairs. If a staircase has nn stairs, then it is made of nn columns, the first column is 11 cell high, the second column is 22 cells high, ……, the nn-th column if nn cells high. The lowest cells of all stairs must be in the same row.
A staircase with nn stairs is called nice, if it may be covered by nn disjoint squares made of cells. All squares should fully consist of cells of a staircase.
Find out the maximal number of different nice staircases, that can be built, using no more than xx cells, in total. No cell can be used more than once.
The first line contains a single integer tt (1≤t≤1000)(1≤t≤1000) — the number of test cases.
The description of each test case contains a single integer xx (1≤x≤1018)(1≤x≤1018) — the number of cells for building staircases.
For each test case output a single integer — the number of different nice staircases, that can be built, using not more than xx cells, in total.
4 1 8 6 1000000000000000000
1 2 1 30
In the first test case, it is possible to build only one staircase, that consists of 11 stair. It‘s nice. That‘s why the answer is 11.
In the second test case, it is possible to build two different nice staircases: one consists of 11 stair, and another consists of 33 stairs. This will cost 77 cells. In this case, there is one cell left, but it is not possible to use it for building any nice staircases, that have not been built yet. That‘s why the answer is 22.
In the third test case, it is possible to build only one of two nice staircases: with 11 stair or with 33 stairs. In the first case, there will be 55 cells left, that may be used only to build a staircase with 22 stairs. This staircase is not nice, and Jett only builds nice staircases. That‘s why in this case the answer is 11. If Jett builds a staircase with 33 stairs, then there are no more cells left, so the answer is 11 again.
题意很好理解,简单来说就是,如图所示的这种方式的楼梯被称为美观的楼梯,问给你n个砖头,问最多能建多少个美观的楼梯块,按题目的变化趋势,每个美观的楼梯的长度都会翻倍,用一个简单数组就可以计算这个过程。
代码如下
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
long long k;
cin>>k;
int ans=0;
for(long long n=1;n*(n+1)/2<=k;n=2*n+1)
{
k=k-n*(n+1)/2;
ans++;
}
cout<<ans<<endl;
}
return 0;
}
A new agent called Killjoy invented a virus COVID-2069 that infects accounts on Codeforces. Each account has a rating, described by an integer (it can possibly be negative or very large).
Killjoy‘s account is already infected and has a rating equal to xx. Its rating is constant. There are nn accounts except hers, numbered from 11 to nn. The ii-th account‘s initial rating is aiai. Any infected account (initially the only infected account is Killjoy‘s) instantly infects any uninfected account if their ratings are equal. This can happen at the beginning (before any rating changes) and after each contest. If an account is infected, it can not be healed.
Contests are regularly held on Codeforces. In each contest, any of these nn accounts (including infected ones) can participate. Killjoy can‘t participate. After each contest ratings are changed this way: each participant‘s rating is changed by an integer, but the sum of all changes must be equal to zero. New ratings can be any integer.
Find out the minimal number of contests needed to infect all accounts. You can choose which accounts will participate in each contest and how the ratings will change.
It can be proven that all accounts can be infected in some finite number of contests.
The first line contains a single integer tt (1≤t≤100)(1≤t≤100) — the number of test cases. The next 2t2t lines contain the descriptions of all test cases.
The first line of each test case contains two integers nn and xx (2≤n≤1032≤n≤103, ?4000≤x≤4000?4000≤x≤4000) — the number of accounts on Codeforces and the rating of Killjoy‘s account.
The second line of each test case contains nn integers a1,a2,…,ana1,a2,…,an (?4000≤ai≤4000)(?4000≤ai≤4000) — the ratings of other accounts.
For each test case output the minimal number of contests needed to infect all accounts.
3 2 69 68 70 6 4 4 4 4 4 4 4 9 38 -21 83 50 -59 -77 15 -71 -78 20
1 0 2
如果刚开始所有的数字都相等,那么可以直接全部传染,无需进行比赛来完成传染。
如果有一个除初始外的账号是x,那么它处于感染状态,进行一场比赛,我们让其他全部变为x,然后分数值的差值直接改变这个当前已经被感染的账号即可。
代码很简单
#include<bits/stdc++.h> using namespace std; #define ll long long int main(){ int t; cin >> t; while(t--){ ll n,x; cin >> n >> x; ll a[n]; ll sum=0,f1=0; for(ll i=0;i<n;++i){ cin >> a[i]; if(a[i]==x) f1++; sum+=a[i]-x; } if(f1==n) cout << 0 << endl; else if(f1>=1 || sum==0) cout << 1 << endl; else cout << 2 << endl; } return 0; }
D1. Sage‘s Birthday (easy version)
This is the easy version of the problem. The difference between the versions is that in the easy version all prices aiai are different. You can make hacks if and only if you solved both versions of the problem.
Today is Sage‘s birthday, and she will go shopping to buy ice spheres. All nn ice spheres are placed in a row and they are numbered from 11 to nn from left to right. Each ice sphere has a positive integer price. In this version all prices are different.
An ice sphere is cheap if it costs strictly less than two neighboring ice spheres: the nearest to the left and the nearest to the right. The leftmost and the rightmost ice spheres are not cheap. Sage will choose all cheap ice spheres and then buy only them.
You can visit the shop before Sage and reorder the ice spheres as you wish. Find out the maximum number of ice spheres that Sage can buy, and show how the ice spheres should be reordered.
The first line contains a single integer nn (1≤n≤105)(1≤n≤105) — the number of ice spheres in the shop.
The second line contains nn different integers a1,a2,…,ana1,a2,…,an (1≤ai≤109)(1≤ai≤109) — the prices of ice spheres.
In the first line print the maximum number of ice spheres that Sage can buy.
In the second line print the prices of ice spheres in the optimal order. If there are several correct answers, you can print any of them.
5 1 2 3 4 5
2 3 1 4 2 5
In the example it‘s not possible to place ice spheres in any order so that Sage would buy 33 of them. If the ice spheres are placed like this (3,1,4,2,5)(3,1,4,2,5), then Sage will buy two spheres: one for 11 and one for 22, because they are cheap.
d1的题意很简单,给一个长度n的数组,保证首位不是最小,问随意排列后,最多会有多少位置可以保证其左右全部比他大,并输出这个序列。这个d1和d2的区别在于简单模式中所有位置的值不可能相等,会减少很多的计算量
我们可以先排序,满足小的条件的个数一定等于(n-1)/2;至于输出我们只需把排好序的数组交替输出即可,这样就可以保证一大一小紧密相连。
#include<bits/stdc++.h> using namespace std; int main() { int n; cin>>n; long long int a[n]; for(int i=0;i<n;i++) cin>>a[i]; sort(a,a+n); cout<<(n-1)/2<<"\n"; for(int i=0;i<n;i+=2) { if(i<n-1) cout<<a[i+1]<<" "<<a[i]<<" "; else cout<<a[i]; } }
D2. Sage‘s Birthday (hard version)
这个题和d1的区别在于需要考虑相等的情况,对于这个问题,我们可以发现只要我们取排序后尽可能小的x个元素,我们只需要x+1个比大于等于他们的元素就可以的到目标数组,只需遍历一遍来确定有几个满足条件的元素即可。
#include <bits/stdc++.h> using namespace std; long long int a[100005],b[100005],n; int main(){ cin>>n; for(int i=1;i<=n;i++) cin>>a[i]; sort(a+1,a+n+1); int p=2; for(int i=1;i<=n/2;i++) { b[p]=a[i]; p+=2; } p=1; for(int i=n/2+1;i<=n;i++) { b[p]=a[i]; p+=2; } int sum=0; for(int i=2;i<n;i++) if(b[i]<b[i-1] && b[i]<b[i+1])sum++; cout<<sum<<endl; for(int i=1;i<=n;i++) cout<<b[i]<<" "; cout<<endl; }
EF题需要前置知识较多,等将来学会后,再来继续更新
放一张图告诉自己需要继续补题
标签:排序 技术 nic graphics ssi nice math change tom
原文地址:https://www.cnblogs.com/tscjj/p/13703037.html