UPC第46场部分题解

这场简单题比较多,基本看懂题意就能A

A: 小凯的疑惑(数论)

A与B互质时,A与B最大不能表达出的数是多少?

打表找规律。。。结论为:
ans=a∗b−a−bans=a*b-a-b ans=a∗b−a−b

B: Irreducible Polynomial(数论)

In mathematics, a polynomial is an expression consisting of variables (also called indeterminate) and coefficients, that involves only the operations of addition, subtraction, multiplication, and non-negative integer exponents of variables. For example, x2 + 4x + 7.

A polynomial is said to be irreducible if it cannot be factored into two or more non-trivial polynomials with real coefficients.
For example, x2+1 is irreducible, but x2-2x+1 is not (since x2-2x+1=(x-1)(x-1)).

Given a polynomial of degree with integer coefficients: anxn+an−1xn−1+…+a1x+a0, you need to check whether it is irreducible or not.

给你一个多项式,问你能不能将其因式分解,可以分解成实数。

这其实也是一个结论题,实数域上,不可拆分只有两种:一次多项式和二次多项式(△ < 0)。

code:code:code:

#include<stdio.h>
#include<stdlib.h>
#include <bits/stdc++.h>
#define ll long long
using namespace std;
// typedef struct Node *List;
ll a[10101];
int main()
{int n,t;cin >> t;while(t--){cin >> n;for(int i=1;i<=n+1;i++){cin >> a[i];}if(n>2) printf("No\n");else if(n==1) printf("Yes\n");else{ll t=a[2]*a[2]-4*a[1]*a[3];if(t<0){printf("Yes\n");}else{printf("No\n");}}}return 0;
}

C: Distant Pastures(最短路)

Farmer John’s farm is made up of an N x N grid of pastures, where each pasture contains one of two different types of grass. To specify these two types of grass, we use the characters ( and ), so for example FJ’s farm might look like the following grid:
(())
)()(
)(((
))))

When Bessie the cow travels around the farm, it takes her A units of time to move from a pasture to an adjacent pasture (one step north, south, east, or west) with the same grass type, or B units of time to move to an adjacent pasture with a different grass type. Whenever Bessie travels from one pasture to a distant pasture, she always uses a sequence of steps that takes the minimum amount of time. Please compute the greatest amount of time Bessie will ever need to take while traveling between some pair of pastures on the farm.

给定一个 n× n的一个网格,每个格子有一个字符,要么是’(‘,要么是’)’。每个格子和它的上下左右的四个格子相邻,对于相邻的两个格子 xy,从 x走到 y的过程中,如果 xy中的字符相同,消耗 A单位时间,如果 xy中字符不同,消耗 B单位时间。定义点 S到点 T的时间为 D( S, T),现在想请你求出网格中最大的 D( S, T)。

老路子了,把a和b作为不同情况点的权值,之后无脑跑spfa就好,枚举起点和终点跑n*n次spfa,每一次记得都要求dis数组的最大值,不断更新就好。

code:code:code:

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=5500,mod = 998244353;
inline int read()
{int X=0,w=0; char ch=0;while(!isdigit(ch)) {w|=ch=='-';ch=getchar();}while(isdigit(ch)) X=(X<<3)+(X<<1)+(ch^48),ch=getchar();return w?-X:X;
}
inline void write(int x)
{if(x<0) putchar('-'),x=-x;if(x>9) write(x/10);putchar(x%10+'0'+' ');
}struct node
{int x,y;
};
ll ans,dis[102][102];
bool mp[102][102];
int n,a,b;
char s[102][110];
int dx[4]={0,0,-1,1};
int dy[4]={-1,1,0,0};
void spfa(int x,int y)
{queue<node>q;memset(dis,0x3f3f3f3f3f,sizeof(dis));memset(mp,0,sizeof(mp));mp[x][y]=1;q.push((node){x,y});dis[x][y]=0;while(!q.empty()){node d=q.front();q.pop();mp[d.x][d.y]=0;for(int i=0;i<4;i++){int sx=d.x+dx[i];int sy=d.y+dy[i];if(sx>0&&sx<=n&&sy>0&&sy<=n){int t1;if(s[d.x][d.y]==s[sx][sy]){t1=a;}else{t1=b;}if(dis[sx][sy]>dis[d.x][d.y]+t1){dis[sx][sy]=dis[d.x][d.y]+t1;if(mp[sx][sy]==0){mp[sx][sy]=1;q.push((node){sx,sy});}}}}}for(int i=1;i<=n;i++){for(int j=1;j<=n;j++){ans=max(ans,dis[i][j]);}}
}
int main()
{cin >> n >> a >> b;for(int i=1;i<=n;i++){for(int j=1;j<=n;j++){cin >> s[i][j];}}for(int i=1;i<=n;i++){for(int j=1;j<=n;j++){spfa(i,j);}}cout << ans;return 0;
}

F: Peaks(图论)

There are N observatories in AtCoder Hill, called Obs. 1, Obs. 2, …, Obs. N. The elevation of Obs. i is Hi. There are also M roads, each connecting two different observatories. Road j connects Obs. Aj and Obs. Bj.Obs. i is said to be good when its elevation is higher than those of all observatories that can be reached from Obs. i using just one road. Note that Obs. i is also good when no observatory can be reached from Obs. i using just one road.
How many good observatories are there?

有n个点,m条无向边,给定这n个点的权值和这m条边连结的点.

求有多少个点比它只经过一条边就能到达的所有点各自的权值都更大.

Constraints
·2≤N≤10^5
·1≤M≤10^5
·1≤Hi≤10^9
·1≤Ai,Bi≤N
·Ai≠Bi
·Multiple roads may connect the same pair of observatories.
·All values in input are integers.

图论基础题,链式前向星建图,之后跑循环判断统计次数就好。

code:code:code:

#include<stdio.h>
#include<stdlib.h>
#include <bits/stdc++.h>
#define ll long long
using namespace std;
// typedef struct Node *List;
int n,m,ans,a[202202],head[202202],cnt,x,y;
struct node
{int to,next;
}e[401101];
void add(int u,int v)
{e[++cnt]=(node){v,head[u]};head[u]=cnt;
}
int main()
{cin >> n >> m;for(int i=1;i<=n;i++){scanf("%d",&a[i]);}for(int i=1;i<=m;i++){scanf("%d %d",&x,&y);add(x,y);add(y,x);}for(int i=1;i<=n;i++){int fl=0;for(int j=head[i];j;j=e[j].next){if(a[i]<=a[e[j].to]){fl=1;break;}}if(!fl) ans++;}cout << ans;return 0;
}

I: This Message Will Self-Destruct in 5s(map)

You are the top spy of AtCoder Kingdom. To prevent the stolen secret from being handed to AlDebaran Kingdom, you have sneaked into the party where the transaction happens.
There are N attendees in the party, and they are given attendee numbers from 1 through N. The height of Attendee i is Ai.
According to an examination beforehand, you know that a pair of attendees satisfying the condition below will make the transaction.
The absolute difference of their attendee numbers is equal to the sum of their heights.There are N(N−1)/2 ways to choose two from the N attendees and make a pair. Among them, how many satisfy the condition above?

给定一个长度为nn的序列AA,求有多少组ii,jj满足:

  • i<j
  • j−i=a[i]+a[j]j-i=a[i]+a[j]j−i=a[i]+a[j]
  • P.S.: We cannot let you know the secret.

Constraints
·All values in input are integers.
·2≤N≤2×10^5
·1≤Ai≤10^9 (1≤i≤N)

转换一下j−a[j]=i+a[i]j-a[j]=i+a[i]j−a[j]=i+a[i],扫一遍map统计即可。

code:code:code:

#include<stdio.h>
#include<stdlib.h>
#include <bits/stdc++.h>
#define ll long long
using namespace std;
// typedef struct Node *List;
map<int,int>mp1;
ll a[201015],ans,n;
int main()
{cin >> n;for(int i=1;i<=n;i++){scanf("%d",&a[i]);}mp1[a[1]+1]++;for(int i=2;i<=n;i++){ll k=i-a[i],k2=a[i]+i;ans+=mp1[k];mp1[k2]++;}cout << ans;return 0;
}

L: Typo(栈)

Bessie has just purchased a new laptop computer, but she unfortunately finds herself unable to type well, given the size of her large hooves relative to the small keyboard. Bessie has just attempted to type in one of her favorite patterns – a balanced string of parentheses. However, she realizes that she might have mis-typed one character, accidentally replacing ( with ) or vice versa. Please help Bessie compute the number of locations in the string such that reversing the single parenthesis at that location would cause the entire string to become balanced.

There are several ways to define what it means for a string of parentheses to be “balanced”. Perhaps the simplest definition is that there must be the same total number of ('s and )'s, and for any prefix of the string, there must be at least as many ('s as )'s. For example, the following strings are all balanced:
()
(())
()(()())

while these are not:
)(
())(
((())))

写错了一个括号,问改变一个括号使序列合法方案数。

我们使用栈来做一遍括号匹配,看看最后栈中元素是不是剩两个,出栈判断括号类型,如果是左括号,在它的右边去找左括号,如果是右括号,在它的左边去找右括号。

code:code:code:

#include<iostream>
#include<iomanip>
#include<cstring>
#include<cmath>
#include<cstdio>
#include <vector>
#include <set>
#include<algorithm>
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=5500,mod = 998244353;
inline int read()
{int X=0,w=0; char ch=0;while(!isdigit(ch)) {w|=ch=='-';ch=getchar();}while(isdigit(ch)) X=(X<<3)+(X<<1)+(ch^48),ch=getchar();return w?-X:X;
}
inline void write(int x)
{if(x<0) putchar('-'),x=-x;if(x>9) write(x/10);putchar(x%10+'0'+' ');
}
char s[100001];
stack<int>st;
ll ans;
int main()
{scanf("%s",s+1);int n1=strlen(s+1);for(int i=1;i<=n1;i++){if(st.empty()){st.push(i);}else{if(s[i] == '('){st.push(i);}else if(s[i] == ')'){if(s[st.top()] == '(')  st.pop();else st.push(i);}}}if(st.size()==2){if(s[st.top()]==')'){int r=st.top();for(int i=0;i<r;i++){if(s[i]==')'){ans++;}}}else{int r=st.top();for(int i=r;i<=n1;i++){if(s[i]=='('){ans++;}}}}cout << ans;return 0;
}

M: Tower

有N个块,编号为1,2,…,N。对于每个i(1≤我≤N) ,块i的重量为wi,坚固度为si,值为vi。

塔罗决定建造一座塔楼,从N个木块中挑选一些,并按一定顺序垂直堆放。在此,风塔必须满足以下条件:

对于塔架中包含的每个区块i,其上方堆叠区块的重量总和不大于si。

查找塔中包含的块的最大可能值之和。

这个题要排序后DP,怎么排序?显然我们要就是对于i , j ,将他们反过来一定不会更劣。
as−bw>bs−awa_s-b_w>b_s-a_w as​−bw​>bs​−aw​
我们定义DP[I]为剩下多少时的最大值,方程有:

f[min(j-a[i].w,a[i].s)]=max(f[min(j-a[i].w,a[i].s)],f[j]+a[i].v);

code:code:code:

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define LL long long
using namespace std;
LL f[10010];
struct node{int w,s,v;}a[1010];
int n,m;
bool cmp(node a,node b) {return a.s-b.w>b.s-a.w;}
int main()
{scanf("%d",&n);for(int i=1;i<=n;i++) scanf("%d %d %d",&a[i].w,&a[i].s,&a[i].v);sort(a+1,a+n+1,cmp);for(int i=1;i<=n;i++){for(int j=a[i].w;j<=10000;j++) f[min(j-a[i].w,a[i].s)]=max(f[min(j-a[i].w,a[i].s)],f[j]+a[i].v);f[a[i].s]=max(f[a[i].s],(LL)a[i].v);}LL ans=0;for(int i=0;i<=10000;i++) ans=max(ans,f[i]);printf("%lld",ans);
}

UPC第46场部分题解相关推荐

  1. UPC第41场,第42场部分题解

    UPC第41场,第42场部分题解 第41场 A: LR Constraints We have N cards arranged in a row from left to right. We wil ...

  2. UPC第38,39,40场部分题解

    UPC第38,39,40场部分题解 第38场 A. Bovine Alliance Description Bessie and her bovine pals from nearby farms h ...

  3. LeetCode第 227 场周赛题解

    LeetCode第 227 场周赛题解 检查数组是否经排序和轮转得到 原题链接 https://leetcode-cn.com/problems/check-if-array-is-sorted-an ...

  4. 20201015:力扣第210场周赛题解(上)

    力扣第210场周赛题解上 题目 思路与算法 代码实现 写在最后 题目 括号的最大嵌套深度 最大网络秩 思路与算法 第一题典型的栈数据结构求最大栈长的题目 第二题是一个经典的出入度题目,本题更加简单,统 ...

  5. 2021UPC第十四场部分题解(ADFL)

    2021UPC第十四场部分题解(ADFL) 文章目录 2021UPC第十四场部分题解(ADFL) 前言 A 火星人问题 F: Card Game for Three L Daisy Chains D: ...

  6. 2021百度之星初赛第一场部分题解

    写在前面 几个家长要求我写一些2021百度之星初赛第一场的题解. 1003 鸽子 原题链接 https://acm.hdu.edu.cn/showproblem.php?pid=6998 http:/ ...

  7. 2020牛客暑期多校训练营(第八场)题解

    文章目录 A. All-Star Game C. Cinema E. Enigmatic Partition G. Game SET H. hard String Problem I. Interes ...

  8. 2019暑假多校训练第四场 | 部分题解

    有一道题目是原题emmm 朝鲜友人出的题目 可能英文题面会有些小问题 反正这场 我的贡献率≈0 题解链接: https://pan.baidu.com/s/1KRpqHaOdq2EL2-Kgbm2EX ...

  9. 2020ICPC·小米 网络选拔赛第一场 全部题解

    整理的算法模板合集: ACM模板 目录 题目传送门 题目总体情况 A.Intelligent Warehouse B.Intelligent Robot C.Smart Browser D.Route ...

最新文章

  1. SSAN 关系抽取 论文笔记
  2. OpenResty简介及学习笔记
  3. equipment download scenario3
  4. windows下集成maven+eclipse开发环境二:集成maven到eclipse,并使用nexus作为maven仓库...
  5. 实例变量与局部变量的区别 java 1615135277
  6. 基于机器学习的GitHub敏感信息泄露监控
  7. 【To Understand】程序员面试金典——番外篇之洪水
  8. weblogic t3协议配置_WebLogic远程Blind XXE高危漏洞,网御星云提供解决方案
  9. 修复Linux系统内核TCP漏洞,Linux 内核中TCP SACK机制远程Dos漏洞处理方法(CVE-2019-11477) | 聂扬帆博客...
  10. vs2010 c++项目创建简易教程
  11. 【408考研计划】计算机组成原理
  12. 【华为云技术分享】软件工程师的AI模型训练起步
  13. 读书笔记——实时渲染(一)
  14. 计算机专业类ppt背景图片,6种方法,教你做出高大上PPT及背景-ppt背景图片怎么设置...
  15. 程序员电脑(减少辐射)(¥37)
  16. dwg格式转换成jpg图片
  17. python 新闻摘要_每日新闻摘要:随着机器取代工人,黄金一日即将推出
  18. spider pi 智能视觉六足机器人 开箱介绍 0602
  19. python规范化货币_Python货币资金科目分析
  20. 用词误导:无症状指的是无肺炎症状,发烧40度、很疼痛都是无症状

热门文章

  1. 【EMQX 5.0】1.1 MQTT协议介绍
  2. java JNI调用C++代码(给出一个简单java application示例和实际java web项目过程及错误解决)(二)
  3. c语言生日创意代码_C语言 生日快乐
  4. 挂号平台首页开发(静态页面部分)
  5. Java获取并解析服务器端的JSON数据包
  6. 密码工程的创新—区块链
  7. php程序设计ppt,PHP程序设计基础知识.ppt
  8. animate动画库的使用步骤
  9. uniapp 使用animate
  10. 本笔记为阿里云天池龙珠计划SQL训练营的学习内容 TASK 4