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 ...
随机推荐
-
HP新学知识
Oracle的框架中有webservice和portlet....但不是平时所知道的那种webservice
-
【转载】OGRE 内存管理
原文:OGRE 内存管理 Ogre引擎中与内存管理相关的文件大致有以下几个(只列出头文件) OgreAlignedAllocator.h OgreMemoryAllocatedObject.h Ogr ...
-
(UVA 11624)Fire!
题目链接 http://vjudge.net/contest/121377#problem/J Joe works in a maze. Unfortunately, portions of the ...
-
linux ssh 中在window 传文件 下载文件 工具
centos 安装工具 yum install lrzsz
-
ubuntu安装python3.5
ubuntu14.04系统会自带python2.7,请不要卸载它.不同版本的Python可以共存在一个系统上. 卸载之后,桌面系统会被影响. (1)sudo add-apt-repository pp ...
-
PHP基础之 file_get_contents() 函数
定义和用法 file_get_contents() 函数把整个文件读入一个字符串中. 和 file() 一样,不同的是 file_get_contents() 把文件读入一个字符串. file_get ...
-
poj3083走玉米地问题
走玉米地迷宫,一般有两种简单策略,遇到岔路总是优先沿着自己的左手方向,或者右手方向走.给一个迷宫,给出这两种策略的步数,再给出最短路径的长度. ######### #.#.#.#.# S....... ...
-
《程序设计实践》【PDF】下载
<程序设计实践>[PDF]下载链接: https://u253469.ctfile.com/fs/253469-231196319 内容简介 本书从排错.测试.性能.可移植性.设计.界面. ...
-
【jQuery】(7)---jQueryAjax同步异步区别
jQueryAjax同步异步 今天在项目开发过程中,要实现这么一个功能 <!-- 当我点击就业的时候,触发onclick时间,check()方法里通过ajax请求返回数据, 如果该用户已经毕业可 ...
-
IE8 disable 兼容行问题
在chrome 下 如果样式设置为disabled 则不能点击, 但是在IE9 或者IE8 则还是可以点击