CCT
最近学校又发了n本五三题霸,BBS看到后十分高兴。但是,当他把五三拿到手后才发现,他已经刷过这些书了!他又认真地看了一会儿,发现新发的这些五三是2017版的,而他刷的是2016版的。现在他想找出所有他没有刷过的题来刷。每本五三都有m道题,并且它的特征(即它和去年版本的五三的差距)可以用一个m位二进制数来代表,二进制位上的1代表该题不同,0代表该题相同。比如4(100)就代表题目3和去年的有不同、5(101)就代表题目1和题目3和去年的有不同。而BBS热衷于给自己找麻烦,他要选择连续一段的几本五三一起刷,并且要求,所有选择的五三的特征中的所有k位中每一位出现1的次数都相同。他又想去刷最多的书,请你告诉他,他最多能刷多少本书?
输入格式:
第一行为两个整数 n、m,接下来的n行为 n 个整数,表示每本五三的特征。
输出格式:
一个整数,表示BBS最多能刷几本书。
样例输入 |
样例输出 |
7 3 7 6 7 2 1 4 2 |
4 |
样例解释:
这7本五三的特征分别为111,110,111,010,001,100,010。选择第3本至第6本五三,这些五三的特征中每一位都出现了2次1。当然,选择第4本到第6本也是可以的,这些五三的特征中每一位都出现了1次1。只是这样子BBS刷的书的数量就少了,他就会不高兴。
数据范围:
对于 100%的数据:1<=n<=100000,1<=k<=30。
well the code is the best language
#include <cstdio> #include <set> //------------------------------------------------------------------------------- #define init(a) freopen(a".in","r",stdin);freopen(a".out","w",stdout); #define cint register int #define fr(i,a,b) for(cint i=a;i<=b;i++) //--------------------efficiency and interface optimization---------------------- using namespace std; //-------------------------------read optimization--------------------------------------- #define getchar() (SS==TT&&(TT=(SS=BB)+fread(BB,1,1<<15,stdin),SS==TT)?EOF:*SS++) char BB[1<<15],*SS=BB,*TT=BB; int read(){cint x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while('0'<=ch&&ch<='9'){x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}return x*f;} //-------------------------------------------------------------------------------------- int n,x,ans,sum[100001][31],k; struct sth{int num,dif[30]; bool operator <(const sth &b)const{fr(i,1,k-1)if(dif[i]!=b.dif[i])return dif[i]<b.dif[i];return 0;} }t;//create the data structure as the set's type,and //the operator function is used to initializate the sort_compare that inside set<sth>s;//use sth to make a set set<sth>::iterator it;//make an iterator named it //if you don't know much about iterator,you can search it on the Internet and have a study int max(int a,int b){return a>b?a:b;}//make a find_max_function by hand //this means if a>b,return a ;or return b(just return the bigger one) int main(){init("CCT");n=read(),k=read();//read total_number of books and the given_binary_digit fr(i,1,n){//make a loop from 1 to n x=read();//read each eigenvalue fr(j,1,k){//calculate out the binary of each eigenvalue sum[i][j]=sum[i-1][j];//this use the ideology of prefix sum if(x&(1<<(j-1)))sum[i][j]++;//1<<(j-1)means 2*(j-1),if the solution >0,just renew the sum }}ans=0;t.num=0;//initializate the answer and t fr(i,1,k)t.dif[i]=0;//initializatation t's differente_num s.insert(t);//insert t into s fr(i,1,n){//make a loop from 1 to n t.num=i;//set t's num as i,just initializate t's num fr(j,1,k-1)//make a loop from 1 to k-1 t.dif[j]=sum[i][j]-sum[i][j+1];//calculate out t's different_num //in this way we can ... it=s.find(t);//set the iterator point to t's location if(it==s.end())s.insert(t);//this just means that have not find t,just t is not real in s //just insert t to s else ans=max(ans,i-(*it).num);//or ,just means that this way is OK,just mark it and renew the answer for better }printf("%d",ans);return 0;//out the answer }
MHM
LGL今天一共要上n节课,这n节课由0标号至n。由于过度劳累,除了第0节课和第n节课,LGL还打算睡上m节课,所以他做了一个睡觉计划表。通过小道消息,LGL得知WQ今天会在学校中检查,所以他想少睡k节课。但是由于某些原因,他又想使相邻的两节睡觉的课之间上的课数量的最小值最大。由于他很困,所以他请你来帮他计算这个值。
输入格式:
第一行为三个整数 n、m、k,接下来的m行为m个整数ai,表示睡觉计划表中LGL想要睡觉的课。
输出格式:
一个整数,表示题目所求的值。
样例输入 |
样例输出 |
25 5 2 14 11 17 2 21 |
3 |
样例解释:
选择第2节和第14节不睡觉,这样子相邻的两节睡觉的课之间上的课数量的最小值为3,即第17节和第21节之间和第21节到第25节之间。没有答案更大的删除方案。
数据范围:
对于100%的数据:1<=n<=109,1<=k<=m<=50000,0<ai<n。
well,it's just a two_point_answer's problem
it's really easy in fact,but don't forget to sort it
I'm so kind that I'd like to give a detailed explaination.
#include <cstdio> #include <algorithm> //------------------------------------------------------------------------------- #define init(a) freopen(a".in","r",stdin);freopen(a".out","w",stdout); #define cint register int #define fr(i,a,b) for(cint i=a;i<=b;i++) //--------------------efficiency and interface optimization---------------------- using namespace std; //-------------------------------read optimization--------------------------------------- #define getchar() (SS==TT&&(TT=(SS=BB)+fread(BB,1,1<<15,stdin),SS==TT)?EOF:*SS++) char BB[1<<15],*SS=BB,*TT=BB; int read(){cint x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while('0'<=ch&&ch<='9'){x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}return x*f;} //-------------------------------------------------------------------------------------- int n,m,k,ans,d[50002]; bool check(int x){int i=1,num=0,now=0; fr(i,1,m)if(d[i]-now<=x)//if one distance is shorter than the smallest_num //that means we need to kill a class to keep the smallest_num is still smallest {num++;if(num>k)return 0;}//if the killed number is bigger than requirement //just means that this case is not allowed,just return false else now=d[i];//or ,that means the distance is larger,just means we don't need to cut this class //just means if we go on,the distance is still larger //so we just need to enumerate from this class to find need_killed_class return 1;//if not return before,just means this case is allowed ,just return true } void TPA(){int l=1,r=n,mid;//set three variable while(l<=r){mid=(l+r)>>1;//think the mid as the smallest amount if(check(mid))ans=mid,l=mid+1;else r=mid-1;} //if the mid satisfies the requirement just renew the answer,and then search in the right part //or ,search in the left part //why we do in this way? //because when you find it's a best solution,that means the left part must be worse than it //(if you don't understand ,just read the check_function.) //(if you still understand ,I think you can have a look at the jumping_rock) //then we just go to the right part to search for the better solution //or if you find it's not the best solution,just means that the right part must be not allowed either. //then we just go to the left part to search for an allowed solution }//two_point_answer int main(){init("mhm");n=read(),m=read(),k=read(); //read total_num of class ,total_num of sleepy class and the killed_number of sleepy class fr(i,1,m)d[i]=read();//read each sleepy class //because these classes are not arranged in order,so we need to sort them sort(d+1,d+m+1);//sort the array from 1 to m d[++m]=n;ans=1;//push the final_class into the array and initializate the answer TPA();printf("%d",ans);return 0;//two_point_ans and then out the answer }
AAFA
YYH有n道题要做。每一道题都有一个截止日期t,只要在该日期之前做完,他的父亲LRB就会奖励他w元钱。令人惊讶的是,每一道题他都只需要1秒来做。请问他最多能从父亲那里拿到多少钱?
输入格式:
第一行为一个整数 n,接下来的n行每一行都有两个数ti和wi,分别表示第i题的截止日期和奖励。
输出格式:
一个整数,表示YYH的最大获利。
样例输入 |
样例输出 |
3 2 10 1 5 1 7 |
17 |
样例解释:
第1秒做第3道题,第2秒做第1道题。
数据范围:
对于 100%的数据:1<=n、ti 、wi <=100000。
#include <cstdio> #include <algorithm> #include <queue> //------------------------------------------------------------------------------- #define init(a) freopen(a".in","r",stdin);freopen(a".out","w",stdout); #define cint register int #define fr(i,a,b) for(cint i=a;i<=b;i++) //--------------------efficiency and interface optimization---------------------- using namespace std; //-------------------------------read optimization--------------------------------------- #define getchar() (SS==TT&&(TT=(SS=BB)+fread(BB,1,1<<15,stdin),SS==TT)?EOF:*SS++) char BB[1<<15],*SS=BB,*TT=BB; int read(){cint x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while('0'<=ch&&ch<='9'){x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}return x*f;} //-------------------------------------------------------------------------------------- typedef long long ll; struct work{ll d,p;}w[100001]; //d:killed time //p:reward priority_queue<int,vector<int>,greater<int> >q; int n;ll sz,ans; bool comp(work a,work b){return a.d<b.d;} int main(){init("aafa");n=read();//read the total_num of problems fr(i,1,n)w[i].d=read(),w[i].p=read();//read each killed time and reward sort(w+1,w+1+n,comp);ans=0;//sort the array to make it in order,and initializate the answer fr(i,1,n){sz=q.size();//use sz to mark the size of priority_queue if(sz<w[i].d)q.push(w[i].p);//if has not arrived this killed time, //just arrive it and push the reward into the queue else if(sz==w[i].d&&q.top()<w[i].p)q.pop(),q.push(w[i].p);} //or ,if has arrived this killed time,and can make the smallest one better,just renew the solution while(!q.empty())ans+=q.top(),q.pop();//calculate the answer printf("%I64d",ans);return 0;//out the answer }
ZZI
YYH拿到了父亲给的钱欣喜若狂,把这些钱拿来造了n栋房子。现在他要给这些房子通电。他有两种方法:第一种是在房间里搭核电发电机发电,对于不同的房子,他需要花不同的代价Vi;,第二种是将有电的房子i的电通过电线通到没电的房子j中,这样子他需要花的代价为aij。他现在请你帮他算出他最少要花多少钱才能让所有的房子通上电。
输入格式:
第一行为一个整数 n。接下来的n行为 n 个整数vi,再接下来的n行每行n个数,第i行第j列的数表示aij。
输出格式:
一个整数,表示最小代价。
样例输入 |
样例输出 |
4 4 4 3 |
9 |
样例解释:
在第4栋房子造核电发电机,再将其他三栋房子通过电线连向它。
数据范围:
对于 100%的数据:1<=n<=300,1<=vi,aij<=100000,保证aii=0,aij=aji。
#include <cstdio> #define INF 0x3f3f3f3f //------------------------------------------------------------------------------- #define init(a) freopen(a".in","r",stdin);freopen(a".out","w",stdout); #define cint register int #define fr(i,a,b) for(cint i=a;i<=b;i++) //--------------------efficiency and interface optimization---------------------- using namespace std; //-------------------------------read optimization--------------------------------------- #define getchar() (SS==TT&&(TT=(SS=BB)+fread(BB,1,1<<15,stdin),SS==TT)?EOF:*SS++) char BB[1<<15],*SS=BB,*TT=BB; int read(){cint x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while('0'<=ch&&ch<='9'){x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}return x*f;} //-------------------------------------------------------------------------------------- int n,dist[301],g[301][301]; bool used[301]; int min(int a,int b){return a<b?a:b;} //make a find_min_function by hand //this means if a<b,return a ;or return b(just return the smaller one) int prim(){int next,maxn,res=0;//set three variables while(true){next=-1;maxn=INF;//initializate the next and the maxn fr(i,1,n)//make a loop from 1 to n if((!used[i])&&dist[i]<maxn)maxn=dist[i],next=i;//it this case can make better and it has not been used //just renew the solution ,and make next point to it if(next==-1)break;//this means that all case have been used or all case can not make better,just break res+=dist[next],used[next]=1,dist[next]=0;//or,just renew the answer and mark it has been used //and set the distance(cost) to 0,just means it has added into the ans and can not choose it again fr(i,1,n)//make a loop from 1 to n if(!used[i])//if this case has not been used dist[i]=min(dist[i],g[next][i]);//if the distance can make better(by connecting to the house had electricity) //just improve it }return res;}//minimal spanning tree int main(){init("zzi");n=read();//read the total_num of houses fr(i,1,n)dist[i]=read();//read the make_electricity_by_hand's cost fr(i,1,n)fr(j,1,n)g[i][j]=read();//read the make_electricity_by_connecting's cost printf("%d",prim());return 0;//set up a minimal spanning tree and out the ans }