题解
Codeforces Round 860 (Div. 2)
A-Showstopper
思路:对同一位的ai,bi进行a[n],b[n]不交换,a[n],b[n]交换,a[i],b[i]交换比较
#include
#include
using namespace std;
int a[110], b[110];
int main()
{
ios::sync_with_stdio(false);
int t;
cin >> t;
while (t--) {
memset(a, 0, sizeof(a));
memset(b, 0, sizeof(b));
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int i = 1; i <= n; i++) {
cin >> b[i];
}
bool flag = 0;
for (int i = 1; i <= n; i++) {
if (a[i] > a[n] || b[i] > b[n]) {
if (a[i] > b[n] || b[i] > a[n]) { //交换后
if (b[i] > a[n] || a[i] > b[n])
flag = 1;
}
}
}
if (flag)
cout << "NO" << '\n';
else
cout << "YES" << '\n';
}
return 0;
}
B-Three Sevens
思路:二维数组存不下,建立vector,从后往前推,对于每天没出现的编号挑选一个,若无答案为-1
#include
#include
#include
#include
C. Candy Store
思路:性质:范围内(xi*yi)的最大公约数*范围内yi的最小公倍数=zi,
不断更新范围内最小公倍数,最大公约数 ,当最大公约数%最小公倍数不等于0时,
更换最小公倍数,最大公约数同时ans++
#include
#include
#include
#include
D. Shocking Arrangement
错解:目的出现错误,贪心使之小于Max
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define int long long
const int maxn = 3e5 + 10;
vectorp; //set默认从小到大排列并且去重不采用
queueq;
void solve() {
int n;
cin >> n;
if (n == 1) {
int a;
cin >> a;
cout << "No" << '\n';
return;
}
for (int i = 1; i <= n; i++) {
int a;
cin >> a;
p.push_back(a);
}
sort(p.begin(), p.end()); //
//sort(p.begin(), p.end(), greater()); //从大到小排序
auto Max = *p.rbegin() - *p.begin();
int sum = 0;
while (!p.empty()) {
sum = abs(sum + *p.rbegin());
if (sum <= Max) {
q.push(*p.rbegin());
p.pop_back();
}
else {
sum = abs(sum - *p.rbegin());
sum = abs(sum + *p.begin());
if (sum > Max) {
cout << "No" << '\n';
p.clear();
q = queue(); //队列的清空:赋初值
return;
}
else {
q.push(*p.begin());
p.erase(p.begin());
}
}
}
cout << "Yes" << '\n';
while (!q.empty()) {
cout << q.front() << ' ';
q.pop();
}
cout << '\n';
}
signed main(){
ios::sync_with_stdio(0);
int t;
cin >> t;
while(t--) {
solve();
}
return 0;
}
思路:目的:使得区间内范围相加abs最小,即使之靠近0的这个值
当sum>0时,选择最小,当sum<=0时选择最大,考虑全为0的特殊情况
#inclide
#include
#include
#include
using namespace std;
#define int long long
int mod = 1e7 + 7;
void solve() {
int n;
cin >> n;
int zero = 0;
vectorv;
for (int i = 0; i < n; i++){
int x;
cin >> x;
if (x == 0)
zero++;
else
v.push_back(x);
}
if (zero == n) //全为0的情况
cout << "NO\n";
else{
sort(v.begin(), v.end());
vectorans;
while (zero--)
ans.push_back(0);
int sum = 0;
int l = 0, r = v.size() - 1;
while (l <= r){
if (sum >= 0){
ans.push_back(v[l]);
sum += v[l];
l++;
}
else{
ans.push_back(v[r]);
sum += v[r];
r--;
}
}
cout << "YES\n";
for (auto i : ans)
cout << i << " ";
cout << '\n';
}
}
signed main()
{
int t;
cin >> t;
while (t--){
solve();
}
return 0;
}
第一次测试
A - Super Ryuma
思路:两点重合,0步,直接满足三种条件,1步,满足1,2;2,3;1,3;3,3;两步,否则三步
#include
typedef long long ll;
using namespace std;
int main()
{
ios::sync_with_stdio(false);
ll a, b, c, d;
cin >> a >> b >> c >> d;
if (a == c && b == d)
cout << 0 << '\n';
else if (a + b == c + d || a - b == c - d || abs(a - c) + abs(b - d) <= 3) //直接满足三种条件
cout << 1 << '\n';
else{ //图形
if (abs(a - c) + abs(b - d) <= 6) //两次第三种情况:两步方框距离
cout << 2 << '\n';
else{
int flag = 0;
for (int i = a - 3; i <= a + 3; i++) //第一种与第三种,第二种与第三种
for (int j = b - 3; j <= b + 3; j++) { //找方框里面能否有直接斜线到终点的
if (abs(i - a) + abs(j - b) <= 3 && i + j == c + d || i - j == c - d)
flag = 1;
}
if (flag)
cout << 2 <<'\n';
else { //第一种与第二种
if (((a + b) & 1LL) == ((c + d) & 1LL)) cout << 2 <<'\n'; //看对角线奇偶性
else cout << 3 << '\n';
}
}
}
return 0;
}
简化版
#include
#include
using namespace std;
int main() {
long long a, b, c, d;
cin >> a >> b >> c >> d;
long long p = abs(c - a), q = abs(d - b);
if (p == 0 && q == 0) cout << '0' << '\n';
else if (p == q || p + q <= 3) cout << '1' << '\n';//1,3;2,3曼哈顿距离小于等于3即可
else if ((p + q) % 2 == 0 || p + q <= 6 || abs(p - q) <= 3) cout << '2' << '\n';
else cout << '3' << '\n';
}
F - Third Avenue
思路:bfs,防止时间超限,将传送门用vector
#include
#include
#include
using namespace std;
#define PI pair
const int maxm = 2e3 + 5;
int dir[4][2] = { {1,0},{0,1},{-1,0},{0,-1} };
int mark[maxm][maxm];
char s[maxm][maxm];
vector g[26];
int vis[26];
PI st, ed;
int n, m;
struct node {
int x,y;
};
void bfs() {
queueq;
q.push(st);
mark[st.first][st.second] = 1;
while (!q.empty()) {
int x = q.front().first;
int y = q.front().second;
q.pop();
for (int k = 0; k < 4; k++) {
int xx = x + dir[k][0];
int yy = y + dir[k][1];
if (xx <= 0 || xx > n || yy <= 0 || yy > m|| mark[xx][yy]|| s[xx][yy] == '#')
continue;
mark[xx][yy] = mark[x][y] + 1;
q.push({ xx,yy });
}
if (s[x][y] >= 'a' && s[x][y] <= 'z') {
if (!vis[s[x][y] - 'a']) {//每种字母只能用一次
vis[s[x][y] - 'a'] = 1;
for (auto i : g[s[x][y] - 'a']) {
if (mark[i.first][i.second])continue;
mark[i.first][i.second] = mark[x][y] + 1;
q.push(i);
}
}
}
}
}
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> s[i][j];
if (s[i][j] == 'S') {
st = { i,j };
}
else if (s[i][j] == 'G') {
ed = { i,j };
}
else if (s[i][j] >= 'a' && s[i][j] <= 'z') {
g[s[i][j] - 'a'].push_back({ i,j });
}
}
}
bfs();
cout<
java(4)
在书本对以前网上学到的知识点进行落实,发现书本上有很多网上没有涉及到区域,于是又重温了一遍,感觉对知识点理解加深了,整理出之前遗落的知识点,然后就是在代码进行动手能力加强