当前位置:网站首页>CF1617B Madoka and the Elegant Gift、CF1654C Alice and the Cake、 CF1696C Fishingprince Plays With Arr
CF1617B Madoka and the Elegant Gift、CF1654C Alice and the Cake、 CF1696C Fishingprince Plays With Arr
2022-07-03 01:32:00 【surocco】
Record some problems you didn't expect to solve at the beginning
cf1617B
Madoka and the Elegant Gift
The main idea of the title is that the largest black rectangular area in the figure is output without crossing yes, Otherwise output no
analysis :
At first, I saw this problem and wanted to use bfs Search for , Find each 1 The leftmost part of the area 、 The most right 、 At the top 、 The lowest position , Then judge whether there is 0, But such words are too complicated , It's not like a 1200 The practice of dividing questions
Then I found that it is to refine the rectangle , Find each 2*2 rectangular , If there is any one 2*2 Rectangle contains 3 individual 1, One 0, It doesn't accord with the meaning of the question , Output no, Otherwise output yes
Relatively simple , No more code
CF1654C
Alice and the Cake
The wrong way to write :
At first, the idea was , First add up all , Find the sum sum, hold a[i] Deposit in set, use map The array records each a[i] Number , Reuse queue save sum, Successive decomposition , Each decomposition is a,b; if a,b yes a[i[ The elements in , Just delete , Otherwise, it will be decomposed ; In the end, if it is decomposed into 1 And set None of them , It means that it is impossible to decompose successfully , On the contrary, it can succeed .
#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <algorithm>
#include <iomanip>
#include <cmath>
#include <map>
#include <stack>
#include <vector>
#include <queue>
#include <set>
#define bug(x) cout<<#x<<"=="<<x<<endl
#define memset(x) memset(x,0,sizeof(x))
#define lowbit(x) x&(-x)
#define INF 0x7fffffff
#define inf 0x3f3f3f3f
#define pi acos(-1)
using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef pair<int, int> PII;
#define int long long
void clear(queue<int>& q) {queue<int> empty;swap(empty, q);}
inline int read()
{
int x = 0, f = 1; char ch = getchar();
while (ch < '0' || ch>'9') { if (ch == '-') f = -1; ch = getchar(); }
while (ch >= '0' && ch <= '9') { x = x * 10 + ch - 48; ch = getchar(); }
return x * f;
}
int T;
set<int>s;
map<int, int>num;
signed main()
{
cin >> T;
while (T--) {
int n;
cin >> n;
int sum = 0;
for (int i = 1; i <= n; i++) {
int x;
cin >> x;
sum += x;
s.insert(x);
num[x]++;
}
queue<int>q;
q.push(sum);
int f = 1;
while (!q.empty()) {
int t = q.front();
q.pop();
if (s.find(t) == s.end()) {
if (t == 1) { f = 0; break; }
int a = ceil(1.0*t / 2);
q.push(a);
int b = floor(1.0*t / 2);
q.push(b);
}
else {
num[t]--;
if (!num[t]) s.erase(t);
}
}
//if(!q.empty())
if (f) cout << "YES\n";
else cout << "NO\n";
num.clear();
s.clear();
}
return 0;
}
But write like this , Hi Ti MLE
Then I read the solution and found a simpler way
The right way :
Still use map The array is stored in each a[i] The number of , Put all the a[i] And sum Deposit in set, Successive decomposition , If it is decomposed to have and a[i] same , Just erase fall , Otherwise, it is directly decomposed and stored set, Note that there is only n Number , So at most n-1 Time , Then you can jump out , Finally, judge set Is it empty , If it can be decomposed successfully , that set All the elements in must be erase It fell off , So you can use set Whether it is empty to output yes or no
void solve()
{
int n; cin >> n;
ll tot = 0;
ll a[n + 1];
map<ll, int> mp;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
tot += a[i];
mp[a[i]]++;
}
multiset<ll> s;
s.insert(tot); // The initial state
for (int i = 0; i < n;)
{
if (s.empty()) break;
ll t = *s.begin();
if (mp[t] == 0)
{
s.erase(s.find(t));
s.insert(t / 2);
s.insert(t - t / 2);
i++; // Number of splits ++
}
else
{
s.erase(s.find(t));
mp[t]--;
}
}
cout << (s.empty() ? "YES\n" : "NO\n");
}
int main()
{
int T;
cin >> T;
while (T--) {
solve();
}
}
Record your changes bug The error of time :set When deleting an element, you delete the value of the corresponding subscript , If you want to delete set The elements in 2, Should be written as s.erase(s.find(2)), instead of s.erase(2) (T▽T)
CF1696C
Fishingprince Plays With Array
The question : Two sequences are given , You can set any number in the sequence x Split into m individual x/m; Or will continue m individual x become m*x;
The minimum split of two sequences must be the same , use queue Save the minimum split , Because there may be a large number after splitting , So use pair{x,cnt} Store the numbers and corresponding quantities after disassembly , If the smallest number is the same as that of the previous one , Just merge with the last one .
int T;
int main()
{
cin >> T;
while (T--) {
vector<pair<ll,ll> >a, b;
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++) {
int x;
cin >> x;
int cnt = 1;
while (x % m == 0) { cnt *= m; x /= m; }
if (a.empty() || a.back().first != x) a.push_back({ x,cnt });
else a.back().second += cnt;
}
int k;
cin >> k;
for (int i = 1; i <= k; i++) {
int x;
cin >> x;
int cnt = 1;
while (x % m == 0) { cnt *= m; x /= m; }
if (b.empty() || b.back().first != x) b.push_back({ x,cnt });
else b.back().second += cnt;
}
if (a == b) cout << "Yes\n";
else cout << "No\n";
}
return 0;
}
边栏推荐
- 强化学习 Q-learning 实例详解
- Thinkphp+redis realizes simple lottery
- Why is it not recommended to use BeanUtils in production?
- 看完这篇 教你玩转渗透测试靶机Vulnhub——DriftingBlues-9
- Concise analysis of redis source code 11 - Main IO threads and redis 6.0 multi IO threads
- Is there a handling charge for spot gold investment
- Strongly connected components of digraph
- Button wizard play strange learning - automatic return to the city route judgment
- 2022 Jiangxi Provincial Safety Officer B certificate reexamination examination and Jiangxi Provincial Safety Officer B certificate simulation examination question bank
- 数学知识:能被整除的数—容斥原理
猜你喜欢
Basic concept and implementation of overcoming hash
电信客户流失预测挑战赛
MySQL basic usage 02
Dotconnect for PostgreSQL data provider
leetcode 6103 — 从树中删除边的最小分数
How is the mask effect achieved in the LPL ban/pick selection stage?
【C语言】指针与数组笔试题详解
Leetcode 2097 - Legal rearrangement of pairs
MySQL
High-Resolution Network (篇一):原理刨析
随机推荐
dotConnect for PostgreSQL数据提供程序
[untitled]
Vim 9.0正式发布!新版脚本执行速度最高提升100倍
Mathematical Knowledge: Steps - Nim Games - Game Theory
Wireshark data analysis and forensics a.pacapng
Button wizard play strange learning - automatic return to the city route judgment
Concise analysis of redis source code 11 - Main IO threads and redis 6.0 multi IO threads
After reading this article, I will teach you to play with the penetration test target vulnhub - drivetingblues-9
Arduino DY-SV17F自动语音播报
[system analyst's road] Chapter V double disk software engineering (development model development method)
2022 cable crane driver examination registration and cable crane driver certificate examination
Swiftui component Encyclopedia: using scenekit and swiftui to build interactive 3D pie charts (tutorial with source code)
【FPGA教程案例6】基于vivado核的双口RAM设计与实现
一比特苦逼程序員的找工作經曆
leetcode 2097 — 合法重新排列数对
MySQL --- 数据库查询 - 基本查询
MySQL - database query - basic query
并发编程的三大核心问题 -《深入理解高并发编程》
Androd Gradle 对其使用模块依赖的替换
ThinkPHP+Redis实现简单抽奖