A .Regular Bracket Sequence

题意:给定“((” , “()” ,  “)(”,  “))”四种,问是否可以组成合法括号匹配

思路:设四种是ABCD,B可以不用管,而C在A或者D存在时可以不考虑,然后就是A=D。

#include<bits/stdc++.h>
#define ll long long
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int maxn=;
ll A,B,C,D,ans;
int main()
{
scanf("%lld%lld%lld%lld",&A,&B,&C,&D);
ans=B;
if(A||D) ans+=C; ans+=2LL*min(A,D);
if(ans==A+B+C+D) puts("");
else puts("");
return ;
}

B .Discounts

题意:给定N个物品,Q次询问,每次询问给定P,表示你可以选P个,最便宜的那个不用付款,问最少付款额。

思路:排序,贪心。

#include<bits/stdc++.h>
#define ll long long
#define rep(i,w,v) for(int i=w;i<=v;i++)
using namespace std;
const int maxn=;
ll a[maxn],sum,ans;
int main()
{
int N,M,x; scanf("%d",&N);
rep(i,,N) scanf("%lld",&a[i]),sum+=a[i];
sort(a+,a+N+);
scanf("%d",&M);
rep(i,,M) {
scanf("%d",&x);
printf("%lld ",sum-a[N+-x]);
}
return ;
}

C .Painting the Fence

题意:给定长度为N个点,M条线段,问选择M-2条,最多可以覆盖多少个点(N,M<5000)

思路:首先如果点被覆盖数大于2,不用考虑了,一定被覆盖。所以先求出被1个线段覆盖的,被2覆盖的,然后枚举不选的线段,前缀和更新答案即可。

#include<bits/stdc++.h>
#define ll long long
#define rep(i,w,v) for(int i=w;i<=v;i++)
using namespace std;
const int maxn=;
int sum[maxn],num[][maxn],ans,tot; pair<int,int>s[maxn];
int main()
{
int N,M;
scanf("%d%d",&N,&M);
rep(i,,M){
scanf("%d%d",&s[i].first,&s[i].second);
sum[s[i].first]++; sum[s[i].second+]--;
}
rep(i,,N) sum[i]+=sum[i-];
rep(i,,N){
if(sum[i]) tot++;
num[][i]+=num[][i-];
num[][i]+=num[][i-];
if(sum[i]==) num[][i]++;
if(sum[i]==) num[][i]++;
}
sort(s+,s+M+);
rep(i,,M)
rep(j,i+,M){
if(s[j].first>s[i].second) ans=max(ans,tot-(num[][s[i].second]-num[][s[i].first-])-(num[][s[j].second]-num[][s[j].
first-]));
else if(s[j].second<=s[i].second)
ans=max(ans,tot-(num[][s[j].first-]-num[][s[i].first-])-(num[][s[i].second]-num[][s[j].second])-(num[][s
[j].second]-num[][s[j].first-]));
else
ans=max(ans,tot-(num[][s[j].first-]-num[][s[i].first-])-(num[][s[j].second]-num[][s[i].second])-(num
[][s[i].second]-num[][s[j].first-]));
}
printf("%d\n",ans);
return ;
}

D .Stressful Training

题意:有N台电脑,每台电脑初始电量a[],每单位时间掉电b[],现在给一个充电器,这个充电器同一单位时间只能给一个电脑充电,问每单位时间至少充多少,满足每台电脑都至少坚持K时间。

思路:二分X,二分之后这么写呢,是线性的呢。 求出每个人最晚的一些充电时间,然后总量不大于K,然后就是线性的向前摊平,如果同一时间个数大于1,则false。

这两个限制使得每次二分是线性的。

#include<bits/stdc++.h>
#define ll long long
#define rep(i,w,v) for(int i=w;i<=v;i++)
using namespace std;
const int maxn=;
ll a[maxn],b[maxn],ans=-;int N,K,vis[maxn];
bool check(ll x)
{
int cnt=; rep(i,,K) vis[i]=;
rep(i,,N){
ll L=a[i];
while(L-b[i]*(K-)<){
int t=L/b[i]+;
if(L/b[i]+<=K) vis[L/b[i]+]++;
cnt++; if(cnt==K) return false;
L+=x;
}
}
for(int i=K;i>;i--) {
if(vis[i]) vis[i-]+=vis[i]-;
}
if(vis[]>) return false;
return true;
}
int main()
{
ll L=,R=1e18,Mid;
scanf("%d%d",&N,&K);
rep(i,,N) scanf("%lld",&a[i]);
rep(i,,N) scanf("%lld",&b[i]);
while(L<=R){
Mid=(L+R)/;
if(check(Mid)) R=Mid-,ans=Mid;
else L=Mid+;
}
printf("%lld\n",ans);
return ;
}

E .Knapsack

题意:给定8种物体的个数a[],重量分别对应1到8,给定W,问选处的不超过W的最大重量。 (W<1e18,a<1e16)

思路:二进制拆分物体,然后暴力背包,可能会空间爆炸,用ans减去没用的一些空间就够了。

这样的话,物体要从大到小排序,这样才能保证map的空间,以及减枝效果。

(这是比较暴力的做法了,有更优美的解法

#include<bits/stdc++.h>
#define ll long long
using namespace std;
map<ll,bool>mp,tmp;
map<ll,bool>::iterator it;
const int maxn=;
ll ans,W,a[maxn],v[maxn],sum[maxn]; int tot,cnt;
int main()
{
scanf("%lld",&W);
for(int i=;i<=;i++){
scanf("%lld",&a[i]);
if(!a[i]) continue; cnt++; ll t=;
ans=max(ans,min(W/i,a[i])*i);
while(){
tot++; v[tot]=min(t,a[i])*i;
a[i]-=min(t,a[i]); t=t*;
if(!a[i]) break;
}
}
if(cnt==){
printf("%lld\n",ans); return ;
}
sort(v+,v+tot+); reverse(v+,v+tot+);
for(int i=tot;i>=;i--) sum[i]=sum[i+]+v[i];
mp[]=true;
for(int i=;i<=tot;i++){
tmp.clear();
for(it=mp.begin();it!=mp.end();it++){
ll x=it->first;
tmp[x]=true;
if(x+v[i]<=W) tmp[x+v[i]]=true;
}
mp.clear();
for(it=tmp.begin();it!=tmp.end();it++){
if(it->first+sum[i+]<ans) continue;
mp[it->first]=true;
ans=max(it->first,ans);
}
}
printf("%lld\n",ans);
return ;
}

F .Clear the String

题意:给定长度为N的字符串,每次可以删去一段连续的相同字符,求最小删去次数。(N<500)

思路:区间DP,反过来就是“刷墙”这种题:每次可以刷一段,问最少刷的次数。 如果col[L]=col[R],我们可以先刷[L,R],然后去刷[L+1,R-1];如果中间有相同的,还可以分子区间。所以是个需要枚举中间点的O(N^3)区间DP。(比赛的时候想N^2做,WA了。

#include<bits/stdc++.h>
#define ll long long
#define rep(i,w,v) for(int i=w;i<=v;i++)
using namespace std;
const int maxn=;
int dp[maxn][maxn]; char a[maxn];
int main()
{
int N,tot=; scanf("%d%s",&N,a+);
rep(i,,N){
if(a[i]!=a[i-]) a[++tot]=a[i];
}
N=tot;
memset(dp,0x3f,sizeof(dp));
rep(len,,N){
rep(L,,N-len+){
int R=L+len-;
if(len==) dp[L][R]=;
else if(len==) dp[L][R]=(a[L]==a[R]?:);
else {
dp[L][R]=len;
if(a[L]==a[R]) dp[L][R]=min(dp[L+][R],dp[L][R-]);
else{ dp[L][R]=min(dp[L+][R],dp[L][R-])+;
rep(k,L,R) dp[L][R]=min(dp[L][R],dp[L][k]+dp[k][R]-);
}
}
}
}
printf("%d\n",dp[][N]);
return ;
}

CF1132.Educational Codeforces Round 61(简单题解)的更多相关文章

  1. Educational Codeforces Round 64 部分题解

    Educational Codeforces Round 64 部分题解 不更了不更了 CF1156D 0-1-Tree 有一棵树,边权都是0或1.定义点对\(x,y(x\neq y)\)合法当且仅当 ...

  2. Educational Codeforces Round 61

    Educational Codeforces Round 61 今早刚刚说我适合打pikmike出的EDU 然后我就挂了 A 不管 B 不管 C 这道题到快结束了才调出来 大概就是\(n^2\)枚举不 ...

  3. Educational Codeforces Round 64部分题解

    Educational Codeforces Round 64部分题解 A 题目大意:给定三角形(高等于低的等腰),正方形,圆,在满足其高,边长,半径最大(保证在上一个图形的内部)的前提下. 判断交点 ...

  4. Educational Codeforces Round 63部分题解

    Educational Codeforces Round 63 A 题目大意就不写了. 挺简单的,若果字符本来就单调不降,那么就不需要修改 否则找到第一次下降的位置和前面的换就好了. #include ...

  5. Educational Codeforces Round 61 (Rated for Div. 2) D,F题解

    D. Stressful Training 题目链接:https://codeforces.com/contest/1132/problem/D 题意: 有n台电脑,每台电脑都有初始电量ai,也有一个 ...

  6. Educational Codeforces Round 61 F 思维 + 区间dp

    https://codeforces.com/contest/1132/problem/F 思维 + 区间dp 题意 给一个长度为n的字符串(<=500),每次选择消去字符,连续相同的字符可以同 ...

  7. Educational Codeforces Round 61 D 二分 + 线段树

    https://codeforces.com/contest/1132/problem/D 二分 + 线段树(弃用结构体型线段树) 题意 有n台电脑,只有一个充电器,每台电脑一开始有a[i]电量,每秒 ...

  8. Educational Codeforces Round 61 C 枚举 + 差分前缀和

    https://codeforces.com/contest/1132/problem/C 枚举 + 差分前缀和 题意 有一段[1,n]的线段,有q个区间,选择其中q-2个区间,使得覆盖线段上的点最多 ...

  9. Educational Codeforces Round 61 (Rated for Div. 2) E 多重背包优化

    https://codeforces.com/contest/1132/problem/E 题意 有8种物品,重量是1~8,每种数量是\(cnt[i]\)(1e16),问容量为W(1e18)的背包最多 ...

随机推荐

  1. weblogic查看版本号教程

    1.查看软件版本号 cd /weblogic/bea/wlserver_10.3/server/lib java -cp weblogic.jar weblogic.version 说明:版本号后边的 ...

  2. Qt画笔实现曲线

    效果图: void CurvePoint::paintEvent(QPaintEvent *event) { // 曲线上的点 static QList<QPointF> points = ...

  3. LaTeX技巧10:LaTeX数学公式输入初级入门

    LaTeX最强大的功能就是显示美丽的数学公式,下面我们来看这些公式是怎么实现的. 1.数学公式的前后要加上 $ 或 \( 和 \),比如:$f(x) = 3x + 7$ 和 \(f(x) = 3x + ...

  4. spring boot 打jar包

    想必大家经常会出现以下报错信息 java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Fai ...

  5. 你应该这样理解JVM内存管理

    在进行Java程序设计时,一般不涉及内存的分配和内存回收的相关代码,此处引用一句话: Java和C++之间存在一堵由内存动态分配和垃圾收集技术所围成的高墙,墙外的人想进去,墙里面的人想出来 ,个人从这 ...

  6. 【MVC】快速构建一个图片浏览网站

    当抄完MusicStore时,你应该对MVC有一个比较清晰的认识了.接下来就需要做个网站来继续增加自己的知识了.那么,该做个什么网站呢.做个图片浏览网站吧,简单而实用. 简单设计 1.首先,页面中间是 ...

  7. day25-python操作redis一

    1.     Python操作nosql数据库 NoSQL,泛指非关系型的数据库.随着互联网web2.0网站的兴起,传统的关系数据库在应付web2.0网站,特别是超大规模和高并发的SNS类型的web2 ...

  8. python 数据如何保存到excel中--xlwt

    第一步:下载xlwt 首先要下载xlwt,(前提是你已经安装好了Python) 下载地址:  https://pypi.python.org/pypi/xlwt/   下载第二个   第二步:安装xl ...

  9. python nltk 安装及配置说明

    本教程采用pip安装方式,前期需要在本机安装setuptools 及pip 网上铺天盖地的说了很多关于nltk的说明,特别是后期nltk_data 手动下载操作,多数都不好使,这里整理 用pip安装n ...

  10. 非递归实现二叉树的三种遍历操作,C++描述

    body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...