Codeforces Round #756 (Div. 3)

Codeforces Round #756 (Div. 3)

A. Make Even

思路:如果末尾是偶数,不需要操作;如果开头是偶数,一次操作,即全翻转;如果开头和末尾都是 奇数,判断里面是否有偶数,如果没有,无法操作,如果有,需要两次操作。

#include <bits/stdc++.h>
#define int long long
#define PII pair<int, int>
#define endl "\n"
#define fast ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define fer(i, n) for (int i = 0; i < n; i++)
#define ff first
#define ss second
using namespace std;
const int N = 3e5 + 11;
string str;
int a[N];
void solve()
{cin>>str;int len = str.length();int ff = -1;//无法操作int kk = 0;for (int i=0;i<len;i++){a[i] = str[i]-'0';if (a[i]%2==0) kk = 1;}if (a[0]%2==0) ff = 1;//如果开头是偶数if (a[len-1]%2==0) ff = 0;//如果末尾是操作,更优if (ff==-1&&kk) ff = 2;//如果末尾和开头都不是偶数,中间有偶数cout<<ff<<endl;
}
signed main()
{fast;int t;cin >> t;while (t--)solve();
}

B. Team Composition: Programmers and Mathematicians

思路:先看总人数能组成多少队,然后再限制。

#include <bits/stdc++.h>
#define int long long
#define PII pair<int, int>
#define endl "\n"
#define fast ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define fer(i, n) for (int i = 0; i < n; i++)
#define ff first
#define ss second
using namespace std;
const int N = 3e5 + 11;
string str;
int a[N];
void solve()
{int a,b;cin>>a>>b;int cou = (a+b)/4;//最多组成多少队cou = min(cou,min(a,b));//限制判断cout<<cou<<endl;
}
signed main()
{fast;int t;cin >> t;while (t--)solve();
}

C. Polycarp Recovers the Permutation

思路:首先,最大的数应该在最外面,不是最左边就是最右边。其次,如果它在最左边,那就只要一开始它在最右边,就可以控制其他数反向输出,右边同理。
example : 5 3 2 4 1 → 1 4 2 3 5

#include <bits/stdc++.h>
#define int long long
#define PII pair<int, int>
#define endl "\n"
#define fast ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define fer(i) for (int i = 1; i <= n; i++)
#define ff first
#define ss second
using namespace std;
const int N = 3e5 + 11;
int a[N];
void solve()
{int n;cin>>n;for (int i=0;i<n;i++){cin>>a[i];}if (a[0]!=n&&a[n-1]!=n) {cout<<-1<<endl;return;}for (int i=n-1;i>=0;i--){cout<<a[i]<<" ";}cout<<endl;
}
signed main()
{fast;int t;cin >> t;while (t--)solve();
}

D. Weights Assignment For Tree Edges

题目意思:先给出每个数父节点。下一行给出每一位dist的大小排序(我觉得这里有点难读)。
example : [3 1 2 5 4] → 节点三是最小的,其次是一,然后二,五,四。
思路:先判断最小的是不是父节点。然后我们不妨给出dist[i] = i。 然后每一个节点的w就是它的dist-父节点的dist。
千万不要忘了,你是求w[i],不是dist[i]

#include <bits/stdc++.h>
#define int long long
#define PII pair<int, int>
#define endl "\n"
#define fast ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define fer(i) for (int i = 1; i <= n; i++)
#define ff first
#define ss second
using namespace std;
const int N = 3e5 + 11;
int p[N],fa[N],ans[N];
void solve()
{int n;cin>>n;for(int i=1;i<=n;i++) cin>>fa[i];for(int i=1;i<=n;i++) cin>>p[i],ans[i]=0;if(p[1]!=fa[p[1]]){//如果最小的不是父节点cout<<-1<<endl;return;}ans[p[1]]=1;for(int i=2;i<=n;i++){if(!ans[fa[p[i]]]){//如果它的父节点还没有遍历到,说明顺序不对了cout<<-1<<endl;return;}ans[p[i]]=i;}for(int i=1;i<=n;i++){cout<<ans[i]-ans[fa[i]]<<" ";输出w[i];}cout<<endl;
}
signed main()
{fast;int t;cin >> t;while (t--)solve();
}

E1. Escape The Maze (easy version)

题目大意:朋友和你同时走,是否在朋友之前走到出口,即叶节点。
思路:bfs多源问题,因为是同时动,所以可以用bfs找出口,如果有一条路可以在朋友之前走完,就是可行的。注意:处理上要先存朋友的位置,因为朋友和你同时到达的位置也不可以走。

#include <bits/stdc++.h>
#define int long long
#define PII pair<int, int>
#define endl "\n"
#define fast ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define fer(i) for (int i = 1; i <= n; i++)
#define ff first
#define ss second
using namespace std;
const int N = 3e5 + 11;
vector<int> cun[N];//地图
queue<PII> q;
int n,k;
int vis[N]; //判断是否走过
int a[N];//存它的朋友
bool bfs()
{memset(vis, 0, sizeof vis);for (int i = 0; i < k; i++){q.push({a[i], 0});vis[a[i]] = 1;} //0表示是朋友;vis[1] = 1;q.push({1, 1});while (q.size()){PII tmp = q.front();q.pop();int now = tmp.ff;if (now != 1 && cun[now].size() == 1 && tmp.ss == 1)return 1; //找到出口for (auto i : cun[now]){if (vis[i])continue;vis[i] = 1;q.push({i, tmp.ss});}}return 0;
}
void solve()
{int x, y;cin >> n >> k;for (int i = 0; i < k; i++)cin >> a[i];for (int i=1;i<=n;i++) cun[i].clear();for (int i = 1; i < n; i++){cin >> x >> y;cun[x].push_back(y);cun[y].push_back(x);}while (q.size())q.pop();cout << (bfs() ? "YES" : "NO") << endl;
}
signed main()
{fast;int t;cin >> t;while (t--)solve();
}

E2. Escape The Maze (hard version)

题意:就是朋友太多了,他们想用最少的朋友堵住他。如果本身就堵不住,那就输出-1;
思路:和上一题一样,多加一个他走不过去的地方都是什么朋友卡住。然后输出卡住他的朋友的数量

#include <bits/stdc++.h>
#define int long long
#define PII pair<int, int>
#define endl "\n"
#define fast ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define fer(i) for (int i = 1; i <= n; i++)
#define ff first
#define ss second
using namespace std;
const int N = 3e5 + 11;
vector<int> cun[N];//地图
queue<PII> q;
int n,k;
int from[N];//存他从哪个点来
int vis[N]; //判断是否走过
int a[N];//存它的朋友
set<int>st;//存储他碰到哪些朋友走过的路
int bfs()
{st.clear();memset(vis, 0, sizeof vis);for (int i = 0; i < k; i++){q.push({a[i], 0});from[a[i]] = a[i];vis[a[i]] = 1;} //0表示是朋友;vis[1] = 1;q.push({1, 1});from[1] = 1;//從本身來while (q.size()){PII tmp = q.front();q.pop();int now = tmp.ff;//現在在哪個點if (now != 1 && cun[now].size() == 1 && tmp.ss == 1)return -1; //找到出口無法堵住他for (auto i : cun[now]){if (vis[i]){if(from[now] == 1 && from[i] != 1) st.insert(from[i]);  //从根节点来到一个朋友已到过的节点continue;}from[i] = from[now];vis[i] = 1;q.push({i, tmp.ss});}}return st.size();
}
void solve()
{int x, y;cin >> n >> k;for (int i = 0; i < k; i++)cin >> a[i];for (int i=1;i<=n;i++) cun[i].clear();for (int i = 1; i < n; i++){cin >> x >> y;cun[x].push_back(y);cun[y].push_back(x);}while (q.size())q.pop();cout<<bfs()<<endl;
}
signed main()
{fast;int t;cin >> t;while (t--)solve();
}

F. ATM and Students

题意:找一段学生取钱或者存钱;
思路:双指针,固定左边取右边。右边一定大于等于上一次的右边。
证明:如果a[l]是正数,那么去除后,上次的右边结束的值一定小于大于这次,上次小于0,所以这次也小于0,如果是负数,那么过程中肯定始终大于上次过程。

#include <bits/stdc++.h>
#define int long long
#define PII pair<int, int>
#define endl "\n"
#define fast ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define fer(i) for (int i = 1; i <= n; i++)
#define ff first
#define ss second
using namespace std;
const int N = 3e5 + 11;
int a[N];
void solve()
{int n, s;cin >> n >> s;int tt = 0; //开始for (int i = 0; i < n; i++){cin >> a[i];}int ansr, ansl, maxn = 0;while (a[tt] + s < 0)//找到从什么时候开始++tt;int sum = a[tt];for (int l = tt, r = tt; l < n; l++){while (r + 1 < n && s + sum + a[r + 1] >= 0)sum += a[++r];if (s + sum >= 0 && maxn < r - l + 1){maxn = r - l + 1;ansr = r;ansl = l;}sum-=a[l];}if (maxn<=0) cout<<"-1"<<endl;else cout<<ansl+1<<" "<<ansr+1<<endl;
}
signed main()
{fast;int t;cin >> t;while (t--)solve();
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/428725.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

使用MyBatista----上传图像

使用MyBatis上传图像&#xff0c;使用的是Oracle的数据库表&#xff0c;有一个TEACHER表&#xff0c;有7列&#xff0c;有1列是存储图片的&#xff0c;类型用BLOB&#xff0c;最大容量是4G&#xff0c;以二进制的形式写入数据库表。 建立这个表的对应实体类Teacher&#xff0c;…

189A. Cut Ribbon

A. Cut Ribbon:题目地址 题意&#xff1a;一条长为n的彩带切割&#xff0c;切割后每段长度是a或者是b或者是c#include <bits/stdc.h> using namespace std; typedef long long ll; vector<int> a((int)4e5); vector<int> b((int)4e5); int main() {int n,a,…

作业1.3

public class Work{   public static void main(String[] args)   {     System.out.println(" J A V V A");     System.out.println(" J A A V V A A"); //直接输出就好了     System.out.println("J J …

476B. Dreamoon and WiFi

B. Dreamoon and WiFi:题目链接 题意&#xff1a;给你一个正常的走向&#xff0c;在给你一个虚假和不确定的走向&#xff0c;问到达的可能性#include <bits/stdc.h> using namespace std; string str1, str2; double C(double A, double B) {double res 1.0;for (int i…

简易观察者模式

var Event {   on(event,callback){     if(!this.handles){       this.handles {};     }     if(!this.handles[event]){       this.handles[event] [];     }     this.handles[event].push(callback);   },   emit(event){   …

1360E. Polygon

E. Polygon&#xff1a;题目 题意&#xff1a;在一个n*n的方块空间内&#xff0c;上下都有大炮&#xff0c;发射数量和先后由你决定。问能否得到他给的地图。 思路&#xff1a;如果他不是靠下边或者右边&#xff0c;右或者下必有一个炮弹阻挡。所以直接遍历判断就行#include &…

1470A. Strange Birthday Party

A. Strange Birthday Party&#xff1a;题目 题意&#xff1a;有n个朋友&#xff0c;要么给他钱&#xff0c;要么给他买礼物&#xff0c;礼物每样只能买一个 思路&#xff1a;sort&#xff0c;给钱不如给礼物的给礼物&#xff0c;礼物给完了&#xff0c;或者礼物本身太贵不如给…

_INTSIZEOF

在_INTSIZEOF中该有的都有了 1.这其中最小非负剩余和最大正余数例子如下: 设n为4&#xff0c;当r为1时&#xff0c;最小非负剩余就是1&#xff0c;最大非正剩余就是1 - 4 -3&#xff0c;最大正余数为4 - 1 3 2.x nq r推导出qn ((x n - 1) / n) * n的过程如下 1)当x % n等…

Windows下struct和union字节对齐设置以及大小的确定(一 简介和结构体大小的确定)...

在windows下设置字节对齐大小的方式&#xff0c;目前我了解有三种&#xff1a; 1. 在编译程序时候的编译选项 /Zp[n]&#xff0c;如 cl /Zp4 表示对齐大小是4字节&#xff1b; 2. 预处理命令 #pragma pack( [ show ] | [ push | pop ] [, identifier ] , n )&#xff1b; 3…

1285B. Just Eat It

B. Just Eat It!&#xff1a;题目 题意&#xff1a;全部吃是否绝对比吃一部分好 思路&#xff0c;如果一部分总和是0或者负的&#xff0c;就可以通过吃另一部分大于等于全吃。#include <bits/stdc.h> using namespace std; typedef long long ll; vector<int> a((…

小米手机无法连上WIFI网络的解决方案

最近很多朋友提问&#xff0c;为什么自己的小米手机无法连上WIFI网络&#xff0c;这里本人就此问题&#xff0c;教大家如何解决故障&#xff1a;1.首先给大家看看故障现象&#xff1a;故障说明&#xff1a;虽然手机上已经打开WLAN&#xff0c;但是当手机连接无线路由器的时候&a…

1037C. Equalize

C. Equalize&#xff1a;题目 题意&#xff1a;a字符串变成b字符串&#xff0c;有两种方法&#xff0c;一种是选两个换位置&#xff0c;花费为abs[j-i]&#xff0c;二是单独一个变,花费为1。 思路&#xff1a;如果两个需要变并且不一样挨在一起就可以省一点花费。#include <…

bzoj1051[kosaraju算法]求强连通分量

之前一直用的是tarjan第一次学习到这个来试一下。 唔&#xff0c;就是裸的算法&#xff0c;然后如果出度为0的点只有一个&#xff0c;输出这个点的大小。 #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<que…

1555C. Coin Rows

C. Coin Rows&#xff1a;题目 题意&#xff1a;仿佛是一道难的dp&#xff0c;但是因为Alice先走&#xff0c;走完bob想要最大化收益。 Alice走完后Bob要么一直往右走&#xff0c;要么一开始就下去然后往右走。#include <bits/stdc.h> using namespace std; #define int…

常见sql技巧

一、一些常见的SQL实践 &#xff08;1&#xff09;负向条件查询不能使用索引 select * from order where status!0 and stauts!1 not in/not exists都不是好习惯 可以优化为in查询&#xff1a; select * from order where status in(2,3) &#xff08;2&#xff09;前导模糊查询…

1466C. Canine poetry

C. Canine poetry&#xff1a;题目 题意&#xff1a;可以用任何小写英文字母替换其中的&#xff0c;使其没有回文子串 思路&#xff1a;如果两个相同就换&#xff0c;如果三个是回文也换&#xff0c;先看两个再看三个#include <bits/stdc.h> using namespace std; typed…

BZOJ 2768 [JLOI2010]冠军调查

还说还剩十分钟A一道水题&#xff0c;然后发现和善意的投票一模一样粘个代码过去直接A。。。 装作自己又写了一道题。 题面 //Twenty #include<cstdio> #include<cstdlib> #include<iostream> #include<algorithm> #include<cmath> #include<…

1420C1. Pokémon Army (easy version)

C1. Pokmon Army (easy version)&#xff1a;题目 题意&#xff1a;选择其中一部分&#xff0c;按照-依此计算&#xff0c;求总和 思路&#xff1a;找到局部最大值&#xff0c;然后减去局部最小值&#xff0c;依此找。#include <bits/stdc.h> using namespace std; type…

python学习笔记(五)缩进

python学习笔记&#xff08;五&#xff09;缩进 原作&#xff1a;http://www.cnblogs.com/vamei/archive/2012/05/29/2524706.html 笔记&#xff1a; #!/usr/bin/env python i 1 #把1赋值给i变量 x 2 #把2赋值给x变量 if i > 0: #如果i大于1x x 1#执行x1 print (x)#输出…

1443B. Saving the City

B. Saving the City&#xff1a;题目 题意&#xff1a;1是炸弹&#xff0c;引爆的同时引爆i-1&#xff0c;i1&#xff0c;埋一个炸弹的成本为b,引爆的成本为a 思路&#xff1a;首先如果有炸弹&#xff0c;必须引爆一次&#xff0c;然后往后遍历&#xff0c;看是引爆还是接上的…