7454 Parentheses

A bracket is a punctuation mark, which is used in matched pairs, usually used within articles or programs. Brackets include round brackets, square brackets, curly brackets, angle brackets, and various other pairs of symbols. Let’s focus on the round brackets, also called parentheses. A sequence of parentheses is said to be well-formed if the parentheses are properly nested. For example, A = a1a2 . . . a18 = “(()())()()()(())()” is well-formed, but B = b1b2 . . . b18 = “(()())))(((((())((” is not. (See Figure 1.) More formally, a sequence of parentheses P = p1p2 . . . pn is well-formed

if (1) when scanning it from p1 to pn, the number of right parentheses does not exceed the number of left parentheses at any state, and

(2) the numbers of left and right parentheses are equal.

Figure 1. Two sequences of parentheses.AutoText is a company, which is developing a text editor for programmers.  The new editor willprovide many powerful functions to automatically correct typing errors. On a keyboard, the left andright parentheses are adjacent. Thus, it is often that “)” is mistyped as “(” or vice versa. And therefore,one of the functions AutoText wants to provide is to automatically convert a sequence of parenthesesP(that may not be well-formed) into a wellformed sequenceP′. In the conversion, the only allowedoperation is to reverse a parenthesis (i.e., either to replace a “(” with a “)” or to replace a “)” witha “(”). For example, in Figure 1, we can convertBinto the well-formed sequenceAby performing 4reverse operations onb7,b10,b12,b18. Of course, there may be several ways to convert a sequence intoa well-formed sequence. A conversion is optimal if it uses the minimum number of reverse operations.Please write a program to compute the minimum number of reverse operations that make a givensequence of parenthesesP=p1p2:::pnwell-formed.InputThe first line contains an integerT10indicating the number of test cases. The first line of each testcase contains an even integern,2n100, indicating the length ofP. Next, the second line givesthe sequenceP.

Output

For each test case, output the minimum number of reverse operations that makePwell-formed.

Sample Input

3

18

(()())))(((((())((

2

()

8

(()))()(

Sample Output

4

0

2

题目意思:对于给出的一系列括号,设计一个AutoText,使得能够自动完成括号匹配,问最少需要转变多少个括号,左括号和右括号可以相互转变。

解题思路:之前经常做这种括号匹配的题目,使用栈先将能够匹配的处理掉,之后栈内不能匹配的两个处理,使其能够匹配即可。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stack>
using namespace std;
stack<char>s;
int main()
{
int t,n,ans;
int x,y;
char c;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
getchar();
ans=;
while(!s.empty())///清空栈
{
s.pop();
}
while(n--)
{
scanf("%c",&c);
if(c=='(')
{
s.push(c);
}
else if(c==')')
{
if(!s.empty()&&s.top()=='(')///已经匹配出栈
{
s.pop();
}
else
{
s.push(c);
}
}
}
x=;
y=;
ans=;
while(!s.empty())
{
if(s.top()=='(')
{
x++;
}
else if(s.top()==')')
{
y++;
}
if(x==&&y==)///出现')('的情况
{
x--;
y--;
ans=ans+;///两个同时翻转
}
else if(x==)///出现'(('的情况
{
x=x-;
ans++;///翻转一个
}
else if(y==)///出现'))'的情况
{
y=y-;
ans++;///翻转一个
}
s.pop();
}
printf("%d\n",ans);
}
return ;
}

7457Discrete Logarithm Problem

题目意思:求一个x使得 a^x%p = b 。

解题思路:开始一看这种表达式,第一反应是快速幂算法,但后来发现数据的规模并不是很大,所以可以直接暴力枚举,同时发现而取模的结果一定在p次以内出现循环,所以直接从0~p枚举x即可。

#include<cstdio>
#include<cstring>
#define ll long long int
#define maxs 102400
using namespace std;
/*ll fast_pow(ll a,ll x,ll p)
{
ll ans=1;
a=a%p;
while(x!=0)
{
if(x&1)
{
ans=(ans*a)%p;
}
x>>=1;
a=(a*a)%p;
}
return ans%p;
}*/
//快速幂
int main()
{
ll a,b,p,ans,i;
int flag;
scanf("%lld",&p);
while(scanf("%lld",&a)!=EOF)
{
if(a==)
{
break;
}
scanf("%lld",&b);
ans=a;
flag=;
for(i=;i<p;i++)
{
ans=(ans*a)%p;
if(ans==b)
{
flag=;
break;
}
}
if(flag)
{
printf("%d\n",i);
}
else
{
printf("0\n");
}
}
return ;
}

7464Robots

Write a program to collect data from robots. We are given two sets of robotsX=fX1;:::;Xmg,Y=fY1;:::;Yng, and a baseB. Each robot has a data and we would like to compute the sum of datafrom all robots and deliver it to the base. In order to do so a robot can send its data to another robotor the base with the following constraints.

•A robot can only send its data to one destination (a robot or the base) at a time.

•A robot (or the base) can receive data from one robot at a time.

•The base can not send data to anyone.

•A robot inXcan complete sending its data in x seconds.A robot in Y can complete sending its data in y seconds.

The robots and the base can perform addition, so we can collect the final sum at the base. Thatis, we assume that after receiving a data, a robot or the base can perform an addition with zero time.Now let us illustrate this concept by an example. Let us consider a system with one robotX1inXand two robotsY1andY2inY. We also assume thatxis 1 andyis 10. At the beginningY1can sendits data toY2andX1can send its data to the base. After 1 second the base will know the data ofX1.However, only after 10 secondsY2will have the data ofY1, add its own data, and send the sum to thebase. After 20 seconds the base receives the sum of data fromY1andY2, adds the data fromX1, andhas the final sum. The entire summation will take 20 seconds.Now let us try a different schedule. At beginningY1sends data to the base, andY2sends data toX1, and both can complete after 10 seconds. Finally X1 starts sending the sum of data from Y2 anditself to the base after 10 seconds, and the entire summation can finish in 11 seconds.Now givenm,n(the numbers of robots in X and Y),x, andy, please determine the minimumnumber of seconds to finish the summation.Constraints•1x<y1000.•0m<1200.•0n<500.

Input

The input consists of multiple test cases. First line contains a single integertindicating the number oftest cases to follow. Each of the nexttlines contains four integers —x,y,m,n.

Output

For each test case, output on a single line the minimum number of seconds to sum up all numbers fromall robots.

Sample Input

11  10  1  2
Sample Output

11

题目意思:有x,y两种类型的机器人各n个和m个,还有一个基地B,x型的机器人将其货物运到基地或其他机器人上需要x秒,y型的机器人将其货物运到基地或其他机器人上需要y秒,同一时刻每个机器人只能发送给一个对象,也只能接受一个对象的货物,基地在同一时刻也只能接受一个机器人的货物,问你如何搭配才能花费最短的时间将所有的货物运送到基地。

解题思路:我刚开始按照那个样例一直想模拟整个运送过程,但发现还是太麻烦,因为里面有很多种情况的判断,那么我们可以从整体的角度出发,需要最少的时间肯定不能是一个个机器人排队去送货物到基地,必然是将一些耗时大的机器人身上的货物送到耗时少的身上,通过哪些耗时少的来运送。这其实就是第一步,将耗时多的机器人身上的货物尽可能的向耗时少的机器人身上送,这个时间段中可以选择一个耗时多的机器人送货到基地,之后耗时多的机器人不管有没有剩余,和耗时少的机器人只要在条件限定内组合时间就像是左手倒右手,时间是一定的,也就是说我们只能省出一个耗时长的机器人的时间。

Live Archive 训练题 2019/3/9的更多相关文章

  1. Live Archive 训练题

    7091 Height Ordering Mrs. Chambers always has her class line up in height order (shortest at the fro ...

  2. X-NUCA 2017 web专题赛训练题 阳光总在风雨后和default wp

     0X0.前言 X-NUCA 2017来了,想起2016 web专题赛,题目都打不开,希望这次主办方能够搞好点吧!还没开赛,依照惯例会有赛前指导,放一些训练题让CTFer们好感受一下题目. 题目有一大 ...

  3. PAT乙级真题及训练题 1025. 反转链表 (25)

    PAT乙级真题及训练题 1025. 反转链表 (25) 感觉几个世纪没打代码了,真是坏习惯,调了两小时把反转链表调出来了,心情舒畅. 这道题的步骤 数据输入,数组纪录下一结点及储存值 创建链表并储存上 ...

  4. 日常 java+雅思+训练题1

    今天主要学了一些类似c中的一些语句,java也是一样类似的,只有一些点需要稍微注意一下,一些语句是新增的需要知道. 完完全全新学的知识就是class和instance的区别.如何创建实例.数据的封装. ...

  5. 中南大学2019年ACM寒假集训前期训练题集(基础题)

    先写一部分,持续到更新完. A: 寒衣调 Description 男从戎,女守家.一夜,狼烟四起,男战死沙场.从此一道黄泉,两地离别.最后,女终于在等待中老去逝去.逝去的最后是换尽一生等到的相逢和团圆 ...

  6. 中南大学2019年ACM寒假集训前期训练题集(入门题)

    A: 漫无止境的八月 Description 又双叒叕开始漫无止境的八月了,阿虚突然问起长门在这些循环中团长哪几次扎起了马尾,他有多少次抓住了蝉等等问题,长门一共回复n个自然数,每个数均不超过1500 ...

  7. java基础集合经典训练题

    第一题:要求产生10个随机的字符串,每一个字符串互相不重复,每一个字符串中组成的字符(a-zA-Z0-9)也不相同,每个字符串长度为10; 分析:*1.看到这个题目,或许你脑海中会想到很多方法,比如判 ...

  8. 2016huas暑假集训训练题 G-Who's in the Middle

    题目链接:http://acm.hust.edu.cn/vjudge/contest/121192#problem/G 此题大意是给定一个数n 然后有n个数 要求求出其中位数  刚开始以为是按数学中的 ...

  9. 2016HUAS暑假集训训练题 G - Oil Deposits

    Description The GeoSurvComp geologic survey company is responsible for detecting underground oil dep ...

随机推荐

  1. Request和Response中文乱码问题的解决方案和区分

    首先,在刚接触这个中文乱码问题的时候,还是比较混乱的,因为针对request和response各自都有自己的解决方案,而且思路相似,方法也很相似,又针对get和post两种提交方式,分两种解决中文乱码 ...

  2. ORACLE->SQL*Loader[20180712]

    https://docs.oracle.com/cd/B28359_01/server.111/b28319/ldr_concepts.htm#g1013706       SQL*Loader将外部 ...

  3. vue---class和style的基本用法

    不多BB了 直接上代码了 通俗移动易懂总结了5种常用改变样式 的形式 <style> .actived2{ color:red; } </style> </head> ...

  4. [示例] 用代码设置 ListView 颜色 (只适用 Win 平台,无需修改官方源码)

    如果可以使用代码随意设置 ListView 的颜色,而不用加载额外的 Style 及修改官方的源码,那该有多好?! 其实 Style 提供了很强了扩充性及可塑性,可以很容易的去操作它. 下面以 Lis ...

  5. STM32 HAL库学习系列第6篇---定时器TIM 级联配置

    应用情景 使用定时器配置编码器模式,发现STM32只有两个定时器是32位,16位的测量值不够用,发现是可以使用两个16位定时器级联为32位的. 我是在使用编码器计数电机转速时使用,但是最终实现的效果不 ...

  6. 【原创】frozenset集合函数入门及实例

    函数作用 frozenset() 返回一个冻结的集合,冻结后集合不能再添加或删除任何元素.与之对应的是set函数,set无序排序且不重复,是可变的,有add(),remove()等方法. 函数原型 f ...

  7. Python学习:14.Python面向对象(一)

    一.面向对象简介 Python设计之初,就是一门面向对象的语言,在Python中一切皆对象,而且在Python中创建一个对象也很简单,今天我们就来学习一下Python的面向对象的知识. 二.两种编程方 ...

  8. 深入浅出MFC学习笔记 第三章 MFC六大关键技术之仿真

    0:MFC类层次结构 1:MFC程序的初始化过程CWinApp::InitApplication()CMyWinApp::InitInstance()CMyFrameWnd::CMyFrameWnd( ...

  9. 最近最少使用算法(LRU)——页面置换

    原创 上一篇博客写了先进先出算法(FIFO)——页面置换:http://www.cnblogs.com/chiweiming/p/9058438.html 此篇介绍最近最少使用算法(LRU)——页面置 ...

  10. week14课上测试

    说明 本次测试老师将所有课下测试的易错题全部重新考察了一遍,虽然是第二次做,还提前复习过,还是错了很多,回到寝室发现老师还没有结束测试,43分的我又忍不住再做了一遍. 做第二遍发现了有几个题目是蓝墨云 ...