15 day 1代碼
第一题
用堆维护。
- #include <cstdio>
- #include <algorithm>
- #include <queue>
- int n,i,f[400000],g[2][200000],j=0,k[400000];
- int l,r;
- bool cho;
- struct pn{
- int l,r,n;
- };
- bool operator<(pn a,pn b){
- return a.n>b.n;
- }
- std::priority_queue<pn> q;
- pn as,as1,as2;
- int main(){
- freopen("minval.in","r",stdin);
- freopen("minval.out","w",stdout);
- scanf("%d",&n);
- for(i=0;i<n;++i) scanf("%d",&g[0][i]);
- for(i=0;i<n;++i) scanf("%d",&g[1][i]);
- std::sort(g[0],g[0]+n);
- std::sort(g[1],g[1]+n);
- as.l=0;
- as.r=0;
- as.n=g[0][0]+g[1][0];
- q.push(as);
- for(i=0;i<n;++i){
- as=q.top();
- q.pop();
- as1.l=as.l+1;
- as1.r=as.r;
- as1.n=g[0][as1.l]+g[1][as1.r];
- as2.l=as.l;
- as2.r=as.r+1;
- as2.n=g[0][as2.l]+g[1][as2.r];
- printf("%d ",as.n);
- q.push(as1);
- q.push(as2);
- }
- return 0;
- }
第二题
连通性判定+二分图
- #include <cstdio>
- #include <cstring>
- int f[200000],d,i,j,m,n,s,t,tt,a,b,pl,h[200000];
- int q[200000],qh,qt;
- bool f2;
- struct edge{
- int t,n;
- } edges[2000000];
- inline void addedge(int f,int t){
- edges[++pl].n=h[f];
- edges[ pl ].t= t ;
- h[f]=pl;
- }
- int main(){
- freopen("catch.in","r",stdin);
- freopen("catch.out","w",stdout);
- scanf("%d",&tt);
- while(tt--){
- ++j;
- memset(f,0,sizeof f);
- memset(h,0,sizeof h);
- pl=0;
- qh=qt=0;
- scanf("%d%d%d",&n,&m,&s);
- for(i=0;i<m;++i){
- scanf("%d%d",&a,&b);
- addedge(a,b);
- addedge(b,a);
- }
- q[qt++]=s;
- f2=false;
- f[s]=1;
- --n;
- while(qh!=qt){
- i=q[qh++];
- for(d=h[i];d;d=edges[d].n){
- t=edges[d].t;
- if(!f[t]){
- f[t]=3-f[i];
- q[qt++]=t;
- --n;
- }else{
- if(f[i]==f[t]) f2=true;//not bicolorable
- }
- }
- }
- if(!f2){
- printf("Case %d: NO\n",j);
- }else{
- if(n) printf("Case %d: NO\n",j); else printf("Case %d: YES\n",j);
- }
- }
- return 0;
- }
第三題:二分答案加判定
- #include <cstdio>
- #include <cstring>
- struct edge{
- int t,n,w;
- } e[300000];
- int h[20000],pl;
- inline void addedge(int f,int t,int w){
- e[++pl].t=t;
- e[pl].n=h[f];
- e[pl].w=w;
- h[f]=pl;
- }
- int n,m,u,v,s,i,j,k;
- int left,right,mid,ans;
- int cost[20000];
- int q[100000],qh,qt;
- bool iq[20000];
- int f[20000];
- int ka,kb,ww;
- int v0,v1,e0;
- void spfa(int mma){
- memset(f,-1,sizeof f);
- memset(iq,0,sizeof iq);
- qh=qt=0;
- q[qt++]=u;
- f[u]=0;
- while(qh!=qt){
- v0=q[qh++];
- iq[v0]=false;
- for(e0=h[v0];e0;e0=e[e0].n){
- v1=e[e0].t;
- if(cost[v1]>mma) continue;
- if(f[v1]==-1 || f[v1] > f[v0] + e[e0].w){
- f[v1] = f[v0] + e[e0].w;
- if(!iq[v1]){
- iq[v1]=true;
- q[qt++]=v1;
- }
- }
- }
- }
- }
- int main(){
- freopen("cost.in","r",stdin);
- freopen("cost.out","w",stdout);
- scanf("%d%d%d%d%d",&n,&m,&u,&v,&s);
- left=0x7fffffff;
- for(i=1;i<=n;++i){
- scanf("%d",cost+i);
- if(cost[i]<left) left=cost[i];
- if(cost[i]>right) right=cost[i];
- }
- for(i=1;i<=m;++i){
- scanf("%d%d%d",&ka,&kb,&ww);
- addedge(ka,kb,ww);
- addedge(kb,ka,ww);
- }
- spfa(right);
- if(f[v]==-1 || f[v] > s){
- printf("-1\n");
- return 0;
- }
- while(left<=right){
- mid=(left+right)/2;
- spfa(mid);
- if( f[v] == -1 || f[v] > s ){
- left=mid+1;
- }else{
- right=mid-1;
- ans=mid;
- }
- }
- printf("%d\n",ans);
- return 0;
- }
更新:修復了exceed 32bit int的bug
- #include <cstdio>
- #include <cstring>
- struct edge{
- int t,n,w;
- } e[300000];
- long long h[20000],pl;
- inline void addedge(int f,int t,int w){
- e[++pl].t=t;
- e[pl].n=h[f];
- e[pl].w=w;
- h[f]=pl;
- }
- long long n,m,u,v,s,i,j,k;
- long long left,right,mid,ans;
- long long cost[20000];
- long long q[100000],qh,qt;
- bool iq[20000];
- long long f[20000];
- long long ka,kb,ww;
- long long v0,v1,e0;
- void spfa(int mma){
- memset(f,-1,sizeof f);
- memset(iq,0,sizeof iq);
- qh=qt=0;
- q[qt++]=u;
- f[u]=0;
- while(qh!=qt){
- v0=q[qh++];
- iq[v0]=false;
- for(e0=h[v0];e0;e0=e[e0].n){
- v1=e[e0].t;
- if(cost[v1]>mma) continue;
- if(f[v1]==-1 || f[v1] > f[v0] + e[e0].w){
- f[v1] = f[v0] + e[e0].w;
- if(!iq[v1]){
- iq[v1]=true;
- q[qt++]=v1;
- }
- }
- }
- }
- }
- int main(){
- freopen("cost.in","r",stdin);
- freopen("cost.out","w",stdout);
- scanf("%lld%lld%lld%lld%lld",&n,&m,&u,&v,&s);
- left=0x7fffffff;
- for(i=1;i<=n;++i){
- scanf("%lld",cost+i);
- if(cost[i]<left) left=cost[i];
- if(cost[i]>right) right=cost[i];
- }
- for(i=1;i<=m;++i){
- scanf("%lld%lld%lld",&ka,&kb,&ww);
- addedge(ka,kb,ww);
- addedge(kb,ka,ww);
- }
- spfa(right);
- if(f[v]==-1 || f[v] > s){
- printf("-1\n");
- return 0;
- }
- while(left<=right){
- mid=(left+right)/2;
- spfa(mid);
- if( f[v] == -1 || f[v] > s ){
- left=mid+1;
- }else{
- right=mid-1;
- ans=mid;
- }
- }
- printf("%lld\n",ans);
- return 0;
- }
15 day 1代碼的更多相关文章
- 配置editplus,讓其支持代碼自動格式化功能.
使用editplus已經好多年了,累積了不少的東西,想換IDE比較麻煩,所以就研究了一下用editplus搭配gofmt.exe配置go語言代碼自動格式化的功能.還好功夫不負有心人,終於被我搞懂了,不 ...
- 我用了13行代碼開發出来的PHP框架
我只用13行代碼開發的PHP框架,如果您對框架不理解,不知道框架究竟幫您做了什麽事,可以下載此框架看一下, 另外如果您想開發自己的框架也可以由這個框架的思路進行擴展. 源碼下載地址:http://do ...
- 关于ios 8 7 下的模态窗口大小的控制 代碼+場景(mainstoryboard)( Resizing UIModalPresentationFormSheet )
1 代碼 UIViewController* modalController = [[UIViewController alloc]init];modalController.modalTransit ...
- phper談談最近重構代碼的感受(1)
作爲一個工作時間並不算長的phper,卻參與了兩家公司的代碼重構.下面談談我的一些感受. 在mjm公司,當時我負責日常的需求開發和2.0的重構.當初的重構更多的是clean codes和一些代碼規範上 ...
- ruby簡單的代碼行統計工具
看代码 # encoding: utf-8 class CodeLineStat attr_reader :code_lines def initialize @code_lines = 0 end ...
- Mybatis逆向生成代碼
Idea 单模块 1.在pom.xml中添加依赖 <build> <plugins> <plugin> <groupId>org.mybatis.gen ...
- 華氏溫度轉化為攝氏溫度的簡單JavaScript代碼
今天,跟著W3School學到了"JavaScript函數",代碼都挺簡單的,在運算符調用函數的地方寫了一個小程序.原碼程序是這樣的: <!DOCTYPE html> ...
- IO流簡單代碼
今天測試了一下,在博客園裏HTML源碼編譯器裏寫CSS内部樣式,更新編譯后,内部樣式可用,但是會將寫的内部樣式代碼强制加上代碼注釋進行編譯,有點類似于强制注入.編譯后的效果就是在前面加入了一個空的p標 ...
- [個人紀錄] WindowsLiveWriter 插入代碼跳出錯誤
跳出找不到設定檔Can’t load configruaration fromC:\Users\…\AppData\Roaming\Windows Live Writer\WindowsLiveWri ...
随机推荐
- 视频播放实时记录日志并生成XML文件
需求描述: 在JWPlayer视频播放过程中,要求实时记录视频观看者播放.暂停的时间,并记录从暂停到下一次播放时所经过的时间.将所有记录保存为XML文件,以方便数据库的后续使用. 实现过程: 尝试1: ...
- Docker-2 的创建、启动、终止、删除、迁移等
学习博客地址:http://www.dwhd.org/20151115_140935.html
- JMeter工具的使用-ForEach
1,Add Thread group this detail information about this panel as below link http://jmeter.apache.org/u ...
- grunt使用watch和livereload的Gruntfile.js的配置
周末在家看angularJS, 用grunt的livereload的自动刷新, 搞了大半天, 现在把配置贴出来, 免得以后忘记了, 只要按照配置一步步弄是没有问题的; 开始的准备的环境安装是: (1) ...
- 【CodeForces 626E】Simple Skewness
题意 给出n个数的集合,求一个 (平均数-中位数)最大 (偏度最大)的子集,输出子集元素个数和各个元素(任意顺序). 分析 因为是子集,所以不一定是连续的序列.然后我们有下面几个结论. 1.最大偏度一 ...
- 【CodeForces 471A】MUH and Sticks
题 题意 给你六根木棍的长度,熊需要头比身体短,大象需要头和身体一样,四肢要一样长,否则就是外星人.请你判断能组成哪一个. 分析 暴力,循环看一下每根有几根相同的,然后如果有四根都是有四根相同的&am ...
- Yii2修改默认布局
public $layout = 'layout';//在类中定义一个变量,名为$layout的php文件 <?php echo $content; ?>
- 常用sql,在做项目时用mysqlWorkBeach里面自动生成的
-- 修改表中的字段的长度ALTER TABLE `sfkbbs`.`sfk_father_module` CHANGE ) NULL DEFAULT NULL COMMENT '父板块名字' ; 在 ...
- jauery加入项目中,但是在页面中显示没有找到这个文件--springMVC框架
遇到一件很不爽的事情,自己明明已经把jquery的文件放在了项目中,但是在页面中总是看不到效果,开发者模式提示没有找到文件,当时都要郁闷疯了,后来无意间看到了Eclipse中报的错,怎么与Spring ...
- c中动态使用数组
#include <iostream> #include <fstream> #include<stdlib.h> #define MAXNUM 200 int I ...