【老李的模拟赛】【#5】【2014-08-11】

来源:互联网 发布:文档网络是什么 编辑:程序博客网 时间:2024/06/05 05:36

今天换成usaco了……结果写了3h的T3,……没时间写其他题了QAQ

数据+题目:http://pan.baidu.com/s/1pJ2p4Pt

Problem 1: Cow Lineup
题目大意:给出一串长度为N数列,和一个正数K。你可以删除K种数字,问最长连续相同的数字长度最长是多少。1<=K<=N<=100000      ID<=1000000000


Farmer John's N cows (1 <= N <= 100,000) are lined up in a row.  Each cow is
identified by an integer "breed ID" in the range 0...1,000,000,000; the
breed ID of the ith cow in the lineup is B(i).  Multiple cows can share the
same breed ID.


FJ thinks that his line of cows will look much more impressive if there is
a large contiguous block of cows that all have the same breed ID.  In order
to create such a block, FJ chooses up to K breed IDs and removes from his
lineup all the cows having those IDs.  Please help FJ figure out
the length of the largest consecutive block of cows with the same breed ID
that he can create by doing this.


PROBLEM NAME: lineup


INPUT FORMAT:


* Line 1: Two space-separated integers: N and K.


* Lines 2..1+N: Line i+1 contains the breed ID B(i).


SAMPLE INPUT (file lineup.in):


9 1
2
7
3
7
7
3
7
5
7


INPUT DETAILS:


There are 9 cows in the lineup, with breed IDs 2, 7, 3, 7, 7, 3, 7, 5, 7. 
FJ would like to remove up to 1 breed ID from this lineup.


OUTPUT FORMAT:


* Line 1: The largest size of a contiguous block of cows with
        identical breed IDs that FJ can create.


SAMPLE OUTPUT (file lineup.out):


4


OUTPUT DETAILS:


By removing all cows with breed ID 3, the lineup reduces to 2, 7, 7, 7, 7,
5, 7.  In this new lineup, there is a contiguous block of 4 cows with the
same breed ID (7).




Problem 2: Island Travels 
有一个R行C列的字符矩阵,‘.’表示不能通行的障碍物,‘S’表示可以通行的水池,‘X’表示可以通行的陆地。所有相互连通的‘X’就组成一个岛屿。FJ现在可以任意选择矩阵的某个格子作为出发点,FJ要每个岛屿都至少访问一次,要求FJ经过的水池的总次数最少。岛屿总个数不超过15。0<=R<=50


Farmer John has taken the cows to a vacation out on the ocean! The cows are
living on N (1 <= N <= 15) islands, which are located on an R x C grid (1
<= R, C <= 50). An island is a maximal connected group of squares on the
grid that are marked as 'X', where two 'X's are connected if they share a
side. (Thus, two 'X's sharing a corner are not necessarily connected.)


Bessie, however, is arriving late, so she is coming in with FJ by
helicopter. Thus, she can first land on any of the islands she chooses. She
wants to visit all the cows at least once, so she will travel between
islands until she has visited all N of the islands at least once.


FJ's helicopter doesn't have much fuel left, so he doesn't want to use it
until the cows decide to go home. Fortunately, some of the squares in the
grid are shallow water, which is denoted by 'S'. Bessie can swim through
these squares in the four cardinal directions (north, east, south, west) in
order to travel between the islands. She can also travel (in the four
cardinal directions) between an island and shallow water, and vice versa.


Find the minimum distance Bessie will have to swim in order to visit all of
the islands. (The distance Bessie will have to swim is the number of
distinct times she is on a square marked 'S'.) After looking at a map of
the area, Bessie knows this will be possible.


PROBLEM NAME: island


INPUT FORMAT:


* Line 1: Two space-separated integers: R and C.


* Lines 2..R+1: Line i+1 contains C characters giving row i of the
        grid. Deep water squares are marked as '.', island squares are
        marked as 'X', and shallow water squares are marked as 'S'.


SAMPLE INPUT (file island.in):


5 4
XX.S
.S..
SXSS
S.SX
..SX


INPUT DETAILS:


There are three islands with shallow water paths connecting some of them.


OUTPUT FORMAT:


* Line 1: A single integer representing the minimum distance Bessie
        has to swim to visit all islands.


SAMPLE OUTPUT (file island.out):


3


OUTPUT DETAILS:


Bessie can travel from the island in the top left to the one in the middle,
swimming 1 unit, and then travel from the middle island to the one in the
bottom right, swimming 2 units, for a total of 3 units.


Problem 3: Seating 
题目大意:有N个座位M个操作。1 <= N <= 500000, 1 <= M <= 300000。
    操作一:形式是‘A’ 、 P,从最前面的空位要插入连续P个数,若不能插入则Ans++。
    操作二:形式是‘L’、 a、b,表示把a~b这一段清空。
    输出ans


To earn some extra money, the cows have opened a restaurant in their barn
specializing in milkshakes.  The restaurant has N seats (1 <= N <= 500,000)
in a row. Initially, they are all empty. 


Throughout the day, there are M different events that happen in sequence at
the restaurant (1 <= M <= 300,000).  The two types of events that can
happen are:


1. A party of size p arrives (1 <= p <= N). Bessie wants to seat the party
in a contiguous block of p empty seats. If this is possible, she does so in
the lowest position possible in the list of seats.  If it is impossible,
the party is turned away.


2. A range [a,b] is given (1 <= a <= b <= N), and everybody in that range of
seats leaves.


Please help Bessie count the total number of parties that are turned away
over the course of the day.


PROBLEM NAME: seating


INPUT FORMAT:


* Line 1: Two space-separated integers, N and M.


* Lines 2..M+1: Each line describes a single event.  It is either a
        line of the form "A p" (meaning a party of size p arrives) or
        "L a b" (meaning that all cows in the range [a, b] leave).


SAMPLE INPUT (file seating.in):


10 4
A 6
L 2 4
A 5
A 2


INPUT DETAILS:


There are 10 seats, and 4 events.  First, a party of 6 cows arrives.  Then
all cows in seats 2..4 depart.  Next, a party of 5 arrives, followed by a
party of 2.


OUTPUT FORMAT:


* Line 1: The number of parties that are turned away.


SAMPLE OUTPUT (file seating.out):


1


OUTPUT DETAILS:


Party #3 is turned away.  All other parties are seated.


T3其实就是线段树,但是可以链表水过,不过……我忘打文件名了啊啊啊啊啊啊啊啊啊啊啊啊啊

Code:

#include<set>#include<list>#include<cstdio>#include<cctype>#include<iostream>#include<algorithm>using namespace std;const int maxn=500010;int getint(){int res=0;char ch=getchar();while(!isdigit(ch))ch=getchar();while(isdigit(ch))res=(res<<3)+(res<<1)+ch-'0',ch=getchar();return res;}int n,m,ans=0;struct block{int l,r,used;};list<block>List;void deb(){for(list<block>::iterator it=List.begin();it!=List.end();it++)printf("l:%d r:%d used:%d\t",it->l,it->r,it->used);cout<<endl;}bool Insert(int x){for(list<block>::iterator it=List.begin();it!=List.end();it++){if(!it->used&&it->r-it->l+1>=x){list<block>::iterator i=List.insert(++it,(block){it->l,it->l+x-1,1});it=--i;i++;if(it->l+x-1<it->r)List.insert(++i,(block){it->l+x,it->r,0});List.erase(it);return true;}}return false;}void Erase(int l,int r){for(list<block>::iterator it=List.begin();it!=List.end();){if(it->r-it->l+1<=0){it=List.erase(it);continue;}if(it->r<l||it->l>r){it++;continue;}list<block>::iterator i,nxt,pre;if(it->l<l&&it->r>r){if(it->used){block k=*it;i=List.insert(++it,(block){l,r,0});it--;it--;i=List.erase(it);i=List.insert(i,(block){k.l,l-1,1});i++;i++;it=List.insert(i,(block){r+1,k.r,1});it++;}else{break;}}elseif(it->l<l&&it->r>=l){if(it->used){block k=*it;//deb();i=List.insert(++it,(block){k.l,l-1,1});it--;//deb();List.insert(++i,(block){l,k.r,0});i--;i--;i--;//deb();it=List.erase(i);it++;it++;//deb();}else{it++;continue;}}elseif(it->l<=r&&it->r>r){if(it->used){block k=*it;//deb();i=List.insert(++it,(block){k.l,r,0});it--;//deb();List.insert(++i,(block){r+1,k.r,1});i--;i--;i--;//deb();it=List.erase(i);it++;it++;//deb();}else{it++;continue;}}elseif(it->l>=l&&it->r<=r){if(it->used){i=List.insert(++it,(block){it->l,it->r,0});i--;it=List.erase(i);}else{it++;continue;}}}//deb();for(list<block>::iterator it=List.begin();it!=List.end();){if(it==List.begin())it++;if(List.size()<=1)break;list<block>::iterator i=--it;it++;if(i->used==it->used){int L=i->l,R=it->r,U=i->used;//deb();i=List.erase(i);//deb();i=List.insert(++it,(block){L,R,U});i--;//deb();it=List.erase(i);}else it++;}}int main(){n=getint();m=getint();List.push_back((block){1,n,0});while(m--){char op=getchar();while(op!='A'&&op!='L')op=getchar();//deb();if(op=='A'){int a=getint();ans+=!Insert(a);}else{int a=getint(),b=getint();if(a>b)swap(a,b);Erase(a,b);}//deb();}printf("%d\n",ans);return 0;}




0 0