Codeforces Round #374 (Div. 2)
A题和B题是一如既往的签到题。
C题是一道拓扑序dp题,题意是给定一个DAG,问你从1号点走到n号点,在长度不超过T的情况下,要求经过的点数最多,换个思维,设dp[i][j]表示到i号点时经过j个点的最小距离,我们按拓扑序转移即可,最后找到一个最大的x,使得dp[n][x]<=T即可,由于还要输出路径,我们就需要记录一下转移。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=;
typedef long long int64;
int n,m,T,tot,now[maxn],stack[maxn],top,fa[maxn][maxn],prep[maxn],son[maxn],val[maxn],head,tail,list[maxn<<][];
int dist[maxn][maxn];
void add(int u,int v,int w){tot++,prep[tot]=now[u],now[u]=tot,son[tot]=v,val[tot]=w;}
int main(){
scanf("%d%d%d",&n,&m,&T);
tot=,memset(now,,sizeof(now));
for (int u,v,w,i=;i<=m;i++){
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
}
memset(dist,,sizeof(dist)); dist[][]=;
head=,tail=,list[][]=,list[][]=; int y,z;
while (head!=tail){
head++; if (head==) head=;
y=list[head][],z=list[head][];
for (int i=now[y],so=son[i];i;i=prep[i],so=son[i]){
if (dist[so][z+]>dist[y][z]+val[i]){
dist[so][z+]=dist[y][z]+val[i];
fa[so][z+]=y;
tail++; if (tail==) tail=;
list[tail][]=so,list[tail][]=z+;
}
}
}
int pos;
for (int i=n;;i--){if (dist[n][i]<=T){pos=i;break;}}
printf("%d\n",pos); y=n; top=;
while (pos){
stack[++top]=y;
y=fa[y][pos],pos--;
}
for (int i=top;i>=;i--) printf("%d ",stack[i]);
puts("");
return ;
}
D题是一个贪心题,题意是给你一个序列,最多进行k次操作,每次操作是将某个位置上的数+x或-x,要求输出若干次操作后这个序列变成什么样子,并且要求这个序列中所有元素的乘积最小。
做法:我们先肯定是要把乘积变为负数,怎么变呢,如果是正数,我们要找到绝对值最小的那个数,如果小于0,就一直+x,直到变成>0,否则就一直-x,直到变成<0.之后我们的目标就是让某些数绝对值变大,因为这样才能尽可能小,还是一样的,每次选择绝对值最小的数,把它绝对值变大,这个过程用个堆高效地维护即可,复杂度nlogn.
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#define PI pair<long long,int>
#define mp(a,b) make_pair(a,b)
using namespace std;
typedef long long int64;
const int maxn=;
int n,k,op;
int64 x,a[maxn],t1,t2;
priority_queue < PI,vector<PI>,greater<PI> > heap;
int main(){
scanf("%d%d%I64d",&n,&k,&x); op=;
for (int i=;i<=n;i++) scanf("%I64d",&a[i]);
for (int i=;i<=n;i++) if (a[i]<) op=-op;
if (op==){
t1=1LL*maxn*maxn;
for (int i=;i<=n;i++) if (abs(a[i])<abs(t1)) t1=a[i],t2=i;
if (t1<){
while (k&&t1<=){
k--,t1+=x;
}
a[t2]=t1;
}else{
while (k&&t1>=){
k--,t1-=x;
}
a[t2]=t1;
}
}
if (k){
while (!heap.empty()) heap.pop();
for (int i=;i<=n;i++) heap.push(mp(abs(a[i]),i));
while (k--){
t1=heap.top().first,t2=heap.top().second; heap.pop();
if (a[t2]<) a[t2]-=x;
else a[t2]+=x;
heap.push(mp(abs(a[t2]),t2));
}
}
for (int i=;i<=n;i++) printf("%I64d ",a[i]);
puts("");
return ;
}
E题是一个dp题,还不会写,待填坑......
Codeforces Round #374 (Div. 2)的更多相关文章
- 拓扑序+dp Codeforces Round #374 (Div. 2) C
http://codeforces.com/contest/721/problem/C 题目大意:给你有向路,每条路都有一个权值t,你从1走到n,最多花费不能超过T,问在T时间内最多能访问多少城市? ...
- Codeforces Round #374 (Div. 2) D. Maxim and Array 贪心
D. Maxim and Array 题目连接: http://codeforces.com/contest/721/problem/D Description Recently Maxim has ...
- Codeforces Round #374 (Div. 2) C. Journey DP
C. Journey 题目连接: http://codeforces.com/contest/721/problem/C Description Recently Irina arrived to o ...
- Codeforces Round #374 (Div. 2) B. Passwords 贪心
B. Passwords 题目连接: http://codeforces.com/contest/721/problem/B Description Vanya is managed to enter ...
- Codeforces Round #374 (Div. 2) A. One-dimensional Japanese Crosswor 水题
A. One-dimensional Japanese Crossword 题目连接: http://codeforces.com/contest/721/problem/A Description ...
- Codeforces Round #374 (Div. 2) D. Maxim and Array —— 贪心
题目链接:http://codeforces.com/problemset/problem/721/D D. Maxim and Array time limit per test 2 seconds ...
- Codeforces Round #374 (Div. 2) C. Journey —— DP
题目链接:http://codeforces.com/contest/721/problem/C C. Journey time limit per test 3 seconds memory lim ...
- Codeforces Round #374 (Div. 2) B. Passwords —— 基础题
题目链接:http://codeforces.com/contest/721/problem/B B. Passwords time limit per test 2 seconds memory l ...
- Codeforces Round #374 (Div. 2) A. One-dimensional Japanese Crossword —— 基础题
题目链接:http://codeforces.com/contest/721/problem/A A. One-dimensional Japanese Crossword time limit pe ...
随机推荐
- Play Framework 完整实现一个APP(八)
创建Tag标签 1.创建Model @Entity @Table(name = "blog_tag") public class Tag extends Model impleme ...
- SQL SERVER出现大量一致性错误的解决方法
如果DBCC CHECKDB发现了比较少的一致性错误,可以使用 DBCC UPDATEUSAGE(DatabaseName,"dbo.ObjectName"); 语句逐个针对表 ...
- Oracle索引梳理系列(五)- Oracle索引种类之表簇索引(cluster index)
版权声明:本文发布于http://www.cnblogs.com/yumiko/,版权由Yumiko_sunny所有,欢迎转载.转载时,请在文章明显位置注明原文链接.若在未经作者同意的情况下,将本文内 ...
- python的进制转换二进制,八进制,十六进制及其原理
#!usr/bin/env python# coding:utf-8def binary(): '''二进制的方法与算法''' Number = 10 Number1 = 20 Nu ...
- 服务器运行环境(LNMP)安装说明
服务器运行环境(LNMP)安装说明 因为公司需要一套流程标准,所以写了如下步骤. 先下载文件environment.tar,将文件上传到服务器. 使用命令解压文件,tar xvf environmen ...
- Ubuntu下Apache+SVN+submin实现WEB管理SVN
为什么需要submin管理SVN? 原来在Ubuntu下,都是直接通过命令行创建SVN仓库并分配权限,但是这有一些问题: 每创建一个SVN仓库,都需要修改httpd.conf 每创建一个帐户,都需要手 ...
- Mysql慢查询和慢查询日志分析
Mysql慢查询和慢查询日志分析 众所周知,大访问量的情况下,可添加节点或改变架构可有效的缓解数据库压力,不过一切的原点,都是从单台mysql开始的.下面总结一些使用过或者研究过的经验,从配置以 ...
- Markdown 完全指南
概述 Markdown 是一种用于网络文本书写的轻量级标记语言,广泛用于个人 blog.github.wiki 中.其实浏览器并不能识别 Markdown 的语法,但许多 blog.wiki 平台以及 ...
- POJ3422 Kaka's Matrix Travels[费用流]
Kaka's Matrix Travels Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9522 Accepted: ...
- LINQ语句中的.AsEnumerable() 和 .AsQueryable()的区别
LINQ语句中的.AsEnumerable() 和 .AsQueryable()的区别 在写LINQ语句的时候,往往会看到.AsEnumerable() 和 .AsQueryable() .例如: s ...