算法竞赛
2015
codeforces #519 A A. Multiplication Table (暴力)
A. Multiplication Table
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Let’s consider a table consisting of n rows and n columns. The cell located at the intersection of i-th row and j-th column contains number i × j. The rows and columns are numbered starting from 1.
vimrc备份
1map <F9> :call SaveInputData()<CR> 2func! SaveInputData() 3 exec "tabnew" 4 exec 'normal "+gP' 5 exec "w! code/in.txt" 6endfunc 7 8 9 10"colorscheme torte 11" colorscheme murphy 12colorscheme elflord 13" colorscheme molokai 14"colorscheme elisex 15"colorscheme colorer 16"colorscheme blacklight 17"colorscheme blue 18"colorscheme darkblue 19"colorscheme evening 20"colorscheme shine 21 22 23 24 25"set fencs=utf-8,ucs-bom,shift-jis,gb18030,gbk,gb2312,cp936 26"set termencoding=utf-8 27"set encoding=utf-8 28"set fileencodings=ucs-bom,utf-8,cp936 29"set fileencoding=utf-8 30 31"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" 32" 显示相关 33"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" 34"set shortmess=atI " 启动的时候不显示那个援助乌干达儿童的提示 35"winpos 5 5 " 设定窗口位置 36"set lines=40 columns=155 " 设定窗口大小 37set go= " 不要图形按钮 38"color asmanian2 " 设置背景主题 39"set guifont=Courier_New:h10:cANSI " 设置字体 40syntax on " 语法高亮 41autocmd InsertLeave * se nocul " 用浅色高亮当前行 42autocmd InsertEnter * se cul " 用浅色高亮当前行 43"set ruler " 显示标尺 44set showcmd " 输入的命令显示出来,看的清楚些 45"set cmdheight=1 " 命令行(在状态行下)的高度,设置为1 46"set whichwrap+=<,>,h,l " 允许backspace和光标键跨越行边界(不建议) 47"set scrolloff=3 " 光标移动到buffer的顶部和底部时保持3行距离 48set novisualbell " 不要闪烁(不明白) 49set statusline=%F%m%r%h%w\ [FORMAT=%{&ff}]\ [TYPE=%Y]\ [POS=%l,%v][%p%%]\ %{strftime(\"%d/%m/%y\ -\ %H:%M\")} "状态行显示的内容 50set laststatus=1 " 启动显示状态行(1),总是显示状态行(2) 51"set foldenable " 允许折叠 52set foldmethod=manual " 手动折叠 53"set background=dark "背景使用黑色 54set nocompatible "去掉讨厌的有关vi一致性模式,避免以前版本的一些bug和局限 55" 显示中文帮助 56if version >= 603 57 set helplang=cn 58 set encoding=utf-8 59endif 60" 设置配色方案 61"colorscheme murphy 62"字体 63"if (has("gui_running")) 64" set guifont=Bitstream\ Vera\ Sans\ Mono\ 10 65"endif 66""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" 67"""""新文件标题 68"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" 69"新建.c,.h,.sh,.java文件,自动插入文件头 70autocmd BufNewFile *.cpp,*.[ch],*.sh,*.java exec ":call SetTitle()" 71""定义函数SetTitle,自动插入文件头 72"map <F4> :call SetTitle()<CR> 73func SetTitle() 74 "如果文件类型为.sh文件 75 if &filetype == 'sh' 76 call setline(1,"\#########################################################################") 77 call append(line("."), "\# File Name: ".expand("%")) 78 call append(line(".")+1, "\# Author: 111qqz") 79 call append(line(".")+2, "\# mail: rkz2013@126.com") 80 call append(line(".")+3, "\# Created Time: ".strftime("%c")) 81 call append(line(".")+4, "\#########################################################################") 82 call append(line(".")+5, "\#!/bin/bash") 83 call append(line(".")+6, "") 84 else 85 let l = 0 86 let l = l + 1 | call setline(l,'/* ***********************************************') 87 let l = l + 1 | call setline(l,'Author :111qqz') 88 let l = l + 1 | call setline(l,'Created Time :'.strftime('%c')) 89 let l = l + 1 | call setline(l,'File Name :'.expand('%')) 90 let l = l + 1 | call setline(l,'************************************************ */') 91 let l = l + 1 | call setline(l,'') 92 endif 93 if &filetype == 'cpp' 94 let l = l + 1 | call setline(l,'#include <cstdio>') 95 let l = l + 1 | call setline(l,'#include <cstring>') 96 let l = l + 1 | call setline(l,'#include <iostream>') 97 let l = l + 1 | call setline(l,'#include <algorithm>') 98 let l = l + 1 | call setline(l,'#include <vector>') 99 let l = l + 1 | call setline(l,'#include <queue>') 100 let l = l + 1 | call setline(l,'#include <set>') 101 let l = l + 1 | call setline(l,'#include <map>') 102 let l = l + 1 | call setline(l,'#include <string>') 103 let l = l + 1 | call setline(l,'#include <cmath>') 104 let l = l + 1 | call setline(l,'#include <cstdlib>') 105 let l = l + 1 | call setline(l,'#include <ctime>') 106 let l = l + 1 | call setline(l,'#define fst first') 107 let l = l + 1 | call setline(l,'#define sec second') 108 let l = l + 1 | call setline(l,'#define lson l,m,rt<<1') 109 let l = l + 1 | call setline(l,'#define rson m+1,r,rt<<1|1') 110 let l = l + 1 | call setline(l,'#define ms(a,x) memset(a,x,sizeof(a))') 111 let l = l + 1 | call setline(l,'typedef long long LL;') 112 let l = l + 1 | call setline(l,'#define pi pair < int ,int >') 113 let l = l + 1 | call setline(l,'#define MP make_pair') 114 let l = l + 1 | call setline(l,'') 115 let l = l + 1 | call setline(l,'using namespace std;') 116 let l = l + 1 | call setline(l,'const double eps = 1E-8;') 117 let l = l + 1 | call setline(l,'const int dx4[4]={1,0,0,-1};') 118 let l = l + 1 | call setline(l,'const int dy4[4]={0,-1,1,0};') 119 let l = l + 1 | call setline(l,'const int inf = 0x3f3f3f3f;') 120 let l = l + 1 | call setline(l,'int main()') 121 let l = l + 1 | call setline(l,'{') 122 let l = l + 1 | call setline(l,' #ifndef ONLINE_JUDGE ') 123 let l = l + 1 | call setline(l,' freopen("code/in.txt","r",stdin);') 124 let l = l + 1 | call setline(l,' #endif') 125 let l = l + 1 | call setline(l,'') 126 let l = l + 1 | call setline(l,' #ifndef ONLINE_JUDGE ') 127 let l = l + 1 | call setline(l,' fclose(stdin);') 128 let l = l + 1 | call setline(l,' #endif') 129 let l = l + 1 | call setline(l,' return 0;') 130 let l = l + 1 | call setline(l,'}') 131 132 133 endif 134 if &filetype == 'c' 135 call append(line(".")+6, "#include<stdio.h>") 136 call append(line(".")+7, "") 137 endif 138 if &filetype == 'java' 139 call append(line(".")+6,"public class ".expand("%")) 140 call append(line(".")+7,"") 141 endif 142 "新建文件后,自动定位到文件末尾 143 autocmd BufNewFile * normal G23 144endfunc 145"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" 146"键盘命令 147""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""v"""""""""""""""""""""""""""""""""""""""" 148 149nmap <leader>w :w!<cr> 150nmap <leader>f :find<cr> 151 152nmap <F8> :TagbarToggle<CR> 153 154 155" 映射全选+复制 ctrl+a 156map <C-A> ggvG"+Y 157map! <C-A> <Esc>ggVGY 158map <F12> gg=G 159" 选中状态下 Ctrl+c 复制 160vmap <C-c> "+y 161"去空行 162nnoremap <F2> :g/^\s*$/d<CR> 163"比较文件 164nnoremap <C-F2> :vert diffsplit 165"新建标签 166map <M-F2> :tabnew<CR> 167"列出当前目录文件 168map <F3> :tabnew .<CR> 169"打开树状文件目录 170map <C-F3> \be 171"C,C++ 按F5编译运行 172map <F5> :call CompileRunGcc()<CR> 173 174let g:tagbar_usearrows = 1 175nnoremap <leader>l :TagbarToggle<CR> 176func! CompileRunGcc() 177 exec "w" 178 if &filetype == 'c' 179 exec "!g++ % -o %<" 180 exec "! ./%<" 181 elseif &filetype == 'cpp' 182 exec "!g++ % -o %<" 183 exec "! ./%<" 184 elseif &filetype == 'java' 185 exec "!javac %" 186 exec "!java %<" 187 elseif &filetype == 'sh' 188 :!./% 189 elseif &filetype == 'py' 190 exec "!python %" 191 exec "!python %<" 192 endif 193endfunc 194"C,C++的调试 195"map <F8> :call Rungdb()<CR> 196func! Rungdb() 197 exec "w" 198 exec "!g++ % -g -o %<" 199 exec "!gdb ./%<" 200endfunc 201 202 203 204"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" 205""实用设置 206""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" 207" 设置当文件被改动时自动载入 208set autoread 209" quickfix模式 210autocmd FileType c,cpp map <buffer> <leader><space> :w<cr>:make<cr> 211"代码补全 212set completeopt=preview,menu 213"允许插件 214filetype plugin on 215"共享剪贴板 216set clipboard+=unnamed 217"从不备份 218set nobackup 219"make 运行 220:set makeprg=g++\ -Wall\ \ % 221"自动保存 222set autowrite 223set ruler " 打开状态栏标尺 224set cursorline " 突出显示当前行 225set magic " 设置魔术 226set guioptions-=T " 隐藏工具栏 227set guioptions-=m " 隐藏菜单栏 228"set statusline=\ %<%F[%1*%M%*%n%R%H]%=\ %y\ %0(%{&fileformat}\ %{&encoding}\ %c:%l/%L%)\ 229" 设置在状态行显示的信息 230set foldcolumn=0 231set foldmethod=indent 232set foldlevel=3 233set foldenable " 开始折叠 234" 不要使用vi的键盘模式,而是vim自己的 235set nocompatible 236" 语法高亮 237set syntax=on 238" 去掉输入错误的提示声音 239set noeb 240" 在处理未保存或只读文件的时候,弹出确认 241set confirm 242" 自动缩进 243set autoindent 244set clipboard+=unnamed 245set cindent 246" Tab键的宽度 247set tabstop=8 248" 统一缩进为8 249set softtabstop=4 250set shiftwidth=4 251" 不要用空格代替制表符 252set noexpandtab 253" 在行和段开始处使用制表符 254set smarttab 255" 显示行号 256set number 257" 历史记录数 258set history=1000 259"禁止生成临时文件 260set nobackup 261set noswapfile 262"搜索忽略大小写 263set ignorecase 264"搜索逐字符高亮 265set hlsearch 266set incsearch 267"行内替换 268set gdefault 269"编码设置 270set enc=utf-8 271set fencs=utf-8,ucs-bom,shift-jis,gb18030,gbk,gb2312,cp936 272"语言设置 273set langmenu=zh_CN.UTF-8 274set helplang=cn 275" 我的状态行显示的内容(包括文件类型和解码) 276"set statusline=%F%m%r%h%w\ [FORMAT=%{&ff}]\ [TYPE=%Y]\ [POS=%l,%v][%p%%]\ %{strftime(\"%d/%m/%y\ -\ %H:%M\")} 277"set statusline=[%F]%y%r%m%*%=[Line:%l/%L,Column:%c][%p%%] 278" 总是显示状态行 279set laststatus=2 280" 命令行(在状态行下)的高度,默认为1,这里是2 281set cmdheight=2 282" 侦测文件类型 283filetype on 284" 载入文件类型插件 285filetype plugin on 286" 为特定文件类型载入相关缩进文件 287filetype indent on 288" 保存全局变量 289set viminfo+=! 290" 带有如下符号的单词不要被换行分割 291set iskeyword+=_,$,@,%,#,- 292" 字符间插入的像素行数目 293set linespace=0 294" 增强模式中的命令行自动完成操作 295set wildmenu 296" 使回格键(backspace)正常处理indent, eol, start等 297set backspace=2 298" 允许backspace和光标键跨越行边界 299set whichwrap+=<,>,h,l 300" 可以在buffer的任何地方使用鼠标(类似office中在工作区双击鼠标定位) 301set mouse=a 302set selection=exclusive 303set selectmode=mouse,key 304" 通过使用: commands命令,告诉我们文件的哪一行被改变过 305set report=0 306" 在被分割的窗口间显示空白,便于阅读 307set fillchars=vert:\ ,stl:\ ,stlnc:\ 308" 高亮显示匹配的括号 309set showmatch 310" 匹配括号高亮的时间(单位是十分之一秒) 311set matchtime=1 312" 光标移动到buffer的顶部和底部时保持3行距离 313set scrolloff=3 314" 为C程序提供自动缩进 315set smartindent 316" 高亮显示普通txt文件(需要txt.vim脚本) 317set cursorline 318hi CursorLine cterm=bold ctermbg=blue ctermfg=yellow 319au BufRead,BufNewFile * setfiletype txt 320"自动补全 321":inoremap ( ()<ESC>i 322":inoremap ) <c-r>=ClosePair(')')<CR> 323:inoremap { {<CR>}<ESC>O 324:inoremap } <c-r>=ClosePair('}')<CR> 325":inoremap [ []<ESC>i 326":inoremap ] <c-r>=ClosePair(']')<CR> 327":inoremap " ""<ESC>i 328":inoremap ' ''<ESC>i 329function! ClosePair(char) 330 if getline('.')[col('.') - 1] == a:char 331 return "\<Right>" 332 else 333 return a:char 334 endif 335endfunction 336filetype plugin indent on 337"打开文件类型检测, 加了这句才可以用智能补全 338set completeopt=longest,menu 339""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" 340 341 342 343 344 345 346"NERDtee设定 347let NERDChristmasTree=1 348let NERDTreeAutoCenter=1 349let NERDTreeBookmarksFile=$VIM.'\Data\NerdBookmarks.txt' 350let NERDTreeMouseMode=2 351let NERDTreeShowBookmarks=1 352let NERDTreeShowFiles=1 353let NERDTreeShowHidden=1 354let NERDTreeShowLineNumbers=1 355let NERDTreeWinPos='left' 356let NERDTreeWinSize=31 357nnoremap f :NERDTreeToggle 358map <F7> :NERDTree<CR> 359 360"""""""""""""""""Taglist设置""""""""""""""""" 361""let Tlist_Auto_Open = 1 362let Tlist_Ctags_Cmd = '/usr/bin/ctags' 363let Tlist_Show_One_File = 1 364let Tlist_Exit_OnlyWindow = 1 365 366 367 368"""""""""""""""""vundle设置""""""""""""""""" 369set nocompatible 370"filetype off 371set rtp+=~/.vim/bundle/vundle/ 372call vundle#rc() 373Bundle 'gmarik/vundle' 374" vim-scripts repos 375Bundle 'bash-support.vim' 376Bundle 'perl-support.vim' 377Bundle 'majutsushi/tagbar' 378Bundle 'ZoomWin' 379Bundle "scrooloose/syntastic" 380" set the runtime path to include Vundle and initialize 381set rtp+=~/.vim/bundle/vundle/ 382call vundle#rc() 383" alternatively, pass a path where Vundle should install plugins 384"let path = '~/some/path/here' 385"call vundle#rc(path) 386 387" let Vundle manage Vundle, required 388Plugin 'gmarik/vundle' 389Bundle "scrooloose/syntastic" 390filetype plugin indent on " required 391Bundle 'Valloric/YouCompleteMe' 392 393 394 395"""""""""""""""""Tagbar设置""""""""""""""""" 396let g:tagbar_autofocus = 1 397let g:tagbar_sort = 0 398let g:tagbar_compact = 1 399let g:tagbar_indent = 1 400let g:tagbar_autoshowtag = 1 401 402 403 404"""""""""""""""""youcompleteme设置""""""""""""""""" 405 406 407"let g:ycm_autoclose_preview_window_after_completion=1 408"nnoremap <leader>g :YcmCompleter GoToDefinitionElseDeclaration<CR> 409 410" YouCompleteMe 功能 411" 补全功能在注释中同样有效 412"let g:ycm_global_ycm_extra_conf = '~/.vim/bundle/YouCompleteMe/cpp/ycm/.ycm_extra_conf.py' 413"let g:ycm_complete_in_comments=1 414" 允许 vim 加载 .ycm_extra_conf.py 文件,不再提示 415"let g:ycm_confirm_extra_conf=0 416 417let g:ycm_error_symbol = '>>' 418let g:ycm_warning_symbol = '>' 419"nnoremap <leader>gl :YcmCompleter GoToDeclaration<CR> 420"nnoremap <leader>gf :YcmCompleter GoToDefinition<CR> 421"nnoremap <leader>gg :YcmCompleter GoToDefinitionElseDeclaration<CR> 422"nmap <F4> :YcmDiags<CR> 423 424 425 426 427"""""""""""""""""youcompleteme设置 by wyz""""""""""""""""" 428let g:cpp_class_scope_highlight=1 429let g:cpp_experimental_template_highlight=1 430let g:ycm_show_diagnostics_ui=0 431let g:ycm_enable_diagnostic_signs=0 432let g:ycm_enable_diagnostic_highlighting = 0 433let g:ycm_echo_current_diagnostic = 0 434let g:ycm_collect_identifiers_from_tags_files = 1 435let g:ycm_key_invoke_completion = '<C-Q>' 436let g:ycm_seed_identifiers_with_syntax = 1 437let g:ycm_global_ycm_extra_conf = '~/.ycm_extra_conf.py' 438let g:ycm_collect_identifiers_from_tags_files = 1 439let g:ycm_confirm_extra_conf = 0 440let g:ycm_autoclose_preview_window_after_completion = 1 441let g:ycm_autoclose_preview_window_after_insertion = 1 442 443 444"""""""""""""""""php是最好的语言23333"""""""""""""""" 445"只有在是PHP文件时,才启用PHP补全 446au FileType php call AddPHPFuncList() 447function AddPHPFuncList() 448 set dictionary-=/home/feiyan/tools/vim/funclist.txt dictionary+=/home/feiyan/tools/vim/funclist.txt 449 set complete-=k complete+=k 450endfunction 451 452"根据文件类型设置缩进格式 453 454au FileType html,python,vim,javascript setl shiftwidth=2 455au FileType html,python,vim,javascript setl tabstop=2 456au FileType java,php setl shiftwidth=4 457au FileType java,php setl tabstop=4 458 459set ai "自动对齐 460 461set foldlevel=100 " 禁止自动折叠 462 463filetype plugin on 464autocmd FileType python set omnifunc=pythoncomplete#Complete 465 466let g:pydiction_location='~/.vim/tools/pydiction/complete-dict'
acm输出输出技巧(提交oj不需要改变)
1int main() 2 { 3 int a,b; 4 #ifndef ONLINE_JUDGE 5 freopen("in.txt","r",stdin); 6 #endif 7 // int a,b; 8 while (scanf("%d%d",&a,&b)!=EOF){ 9 cout<<a+b<<endl; 10 } 11 #ifndef ONLINE_JUDGE 12 fclose(stdin); 13 #endif 14 return 0; 15 } 这样写比较爽
0x3f3f3f3f...编程中无穷大常量的设置技巧
如果问题中各数据的范围明确,那么无穷大的设定不是问题,在不明确的情况下,很多程序员都取0x7fffffff作为无穷大,因为这是32-bit int的最大值。如果这个无穷大只用于一般的比较(比如求最小值时min变量的初值),那么0x7fffffff确实是一个完美的选择,但是在更多的情况下,0x7fffffff并不是一个好的选择。
【2-SAT问题】(转自kuangbin的博客)
【2-SAT问题】 现有一个由N个布尔值组成的序列A,给出一些限制关系,比如A[x] AND A[y]=0、A[x] OR A[y] OR A[z]=1等,要确定A[0..N-1]的值,使得其满足所有限制关系。这个称为SAT问题,特别的,若每种限制关系中最多只对两个元素进行限制,则称为2-SAT问题。
cf 570 C. Replacement (暴力)
C. Replacement
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Daniel has a string s, consisting of lowercase English letters and period signs (characters ‘.’). Let’s define the operation of replacement as the following sequence of steps: find a substring “..” (two consecutive periods) in string s, of all occurrences of the substring let’s choose the first one, and replace this substring with string “.”. In other words, during the replacement operation, the first two consecutive periods are replaced by one. If string s contains no two consecutive periods, then nothing happens.