codeforces160A

来源:互联网 发布:usleep函数源码 编辑:程序博客网 时间:2024/06/05 10:25

Imagine that you have a twin brother or sister. Having another person that looks exactly like you seems very unusual. It’s hard to say if having something of an alter ego is good or bad. And if you do have a twin, then you very well know what it’s like.

Now let’s imagine a typical morning in your family. You haven’t woken up yet, and Mom is already going to work. She has been so hasty that she has nearly forgotten to leave the two of her darling children some money to buy lunches in the school cafeteria. She fished in the purse and found some number of coins, or to be exact, n coins of arbitrary values a1, a2, …, an. But as Mom was running out of time, she didn’t split the coins for you two. So she scribbled a note asking you to split the money equally.

As you woke up, you found Mom’s coins and read her note. “But why split the money equally?” — you thought. After all, your twin is sleeping and he won’t know anything. So you decided to act like that: pick for yourself some subset of coins so that the sum of values of your coins is strictly larger than the sum of values of the remaining coins that your twin will have. However, you correctly thought that if you take too many coins, the twin will suspect the deception. So, you’ve decided to stick to the following strategy to avoid suspicions: you take the minimum number of coins, whose sum of values is strictly more than the sum of values of the remaining coins. On this basis, determine what minimum number of coins you need to take to divide them in the described manner.

Input
The first line contains integer n (1 ≤ n ≤ 100) — the number of coins. The second line contains a sequence of n integers a1, a2, …, an (1 ≤ ai ≤ 100) — the coins’ values. All numbers are separated with spaces.

Output
In the single line print the single number — the minimum needed number of coins.

Example
Input
2
3 3
Output
2
Input
3
2 1 2
Output
2
Note
In the first sample you will have to take 2 coins (you and your twin have sums equal to 6, 0 correspondingly). If you take 1 coin, you get sums 3, 3. If you take 0 coins, you get sums 0, 6. Those variants do not satisfy you as your sum should be strictly more that your twins’ sum.

In the second sample one coin isn’t enough for us, too. You can pick coins with values 1, 2 or 2, 2. In any case, the minimum number of coins equals 2.
这道题就是直接排序后贪到大于一半就可以了。

#include<bits/stdc++.h>using namespace std;#define N 1001int a[N];int n;int sum;int it;int main(){    cin>>n;    for(int i=1;i<=n;i++)cin>>a[i];    sort(a+1,a+n+1);    for(int i=1;i<=n;i++) sum+=a[i];    it=sum/2+1;    int hh=0;    int cnt=0;    for(int i=n;i>=1;i--)    {        cnt++;        hh+=a[i];        if(hh>=it)         {            cout<<cnt<<endl;break;        }    }}

Each of you probably has your personal experience of riding public transportation and buying tickets. After a person buys a ticket (which traditionally has an even number of digits), he usually checks whether the ticket is lucky. Let us remind you that a ticket is lucky if the sum of digits in its first half matches the sum of digits in its second half.

But of course, not every ticket can be lucky. Far from it! Moreover, sometimes one look at a ticket can be enough to say right away that the ticket is not lucky. So, let’s consider the following unluckiness criterion that can definitely determine an unlucky ticket. We’ll say that a ticket is definitely unlucky if each digit from the first half corresponds to some digit from the second half so that each digit from the first half is strictly less than the corresponding digit from the second one or each digit from the first half is strictly more than the corresponding digit from the second one. Each digit should be used exactly once in the comparisons. In other words, there is such bijective correspondence between the digits of the first and the second half of the ticket, that either each digit of the first half turns out strictly less than the corresponding digit of the second half or each digit of the first half turns out strictly more than the corresponding digit from the second half.

For example, ticket 2421 meets the following unluckiness criterion and will not be considered lucky (the sought correspondence is 2 > 1 and 4 > 2), ticket 0135 also meets the criterion (the sought correspondence is 0 < 3 and 1 < 5), and ticket 3754 does not meet the criterion.

You have a ticket in your hands, it contains 2n digits. Your task is to check whether it meets the unluckiness criterion.

Input
The first line contains an integer n (1 ≤ n ≤ 100). The second line contains a string that consists of 2n digits and defines your ticket.

Output
In the first line print “YES” if the ticket meets the unluckiness criterion. Otherwise, print “NO” (without the quotes).

Example
Input
2
2421
Output
YES
Input
2
0135
Output
YES
Input
2
3754
Output
NO

细节题,每次尝试找双射就可以了

#include<cstdio>#include<iostream>#include<algorithm>#include<cstring>using namespace std;#define N 201int a[11];int b[11];int n;int aa[11];int bb[11];char s[N+5];int l;bool flag1=0,flag2=0;int main(){    cin>>n;    scanf("%s",s+1);    l=strlen(s+1);    for(int i=1;i<=n;i++)    a[s[i]-'0']++;    for(int i=n+1;i<=l;i++)    b[s[i]-'0']++;//  for(int i=0;i<=9;i++) cout<<a[i]<<" ";cout<<endl;//  for(int i=0;i<=9;i++) cout<<b[i]<<" ";    if(a[9]&&b[9]){puts("NO");return 0;}    if(a[0]&&b[0]){puts("NO");return 0;}    for(int i=0;i<=9;i++){aa[i]=a[i];bb[i]=b[i];}    int cnt=1;    while(!bb[cnt])cnt++;    if(!b[0]&&!a[9])    for(int i=0;i<=8;i++)    {        if(!a[i])continue;        for(int j=1;j<=a[i];j++)        {            if(cnt<=i){flag1=1;break;}            if(bb[cnt])bb[cnt]--;            while(!bb[cnt])cnt++;        }    }    else flag1=1;    if(!b[9]&&!a[0])    {        cnt=1;        while(!aa[cnt]) cnt++;        for(int i=0;i<=8;i++)        {            if(!b[i])continue;            for(int j=1;j<=b[i];j++)            {                if(cnt<=i){flag2=1;break;}                if(aa[cnt]) aa[cnt]--;                while(!aa[cnt])cnt++;            }        }    }    else flag2=1;    if(!flag1||!flag2)    {        puts("YES");    }    else puts("NO");}

You’ve got another problem dealing with arrays. Let’s consider an arbitrary sequence containing n (not necessarily different) integers a1, a2, …, an. We are interested in all possible pairs of numbers (ai, aj), (1 ≤ i, j ≤ n). In other words, let’s consider all n2 pairs of numbers, picked from the given array.

For example, in sequence a = {3, 1, 5} are 9 pairs of numbers: (3, 3), (3, 1), (3, 5), (1, 3), (1, 1), (1, 5), (5, 3), (5, 1), (5, 5).

Let’s sort all resulting pairs lexicographically by non-decreasing. Let us remind you that pair (p1, q1) is lexicographically less than pair (p2, q2) only if either p1 < p2, or p1 = p2 and q1 < q2.

Then the sequence, mentioned above, will be sorted like that: (1, 1), (1, 3), (1, 5), (3, 1), (3, 3), (3, 5), (5, 1), (5, 3), (5, 5)

Let’s number all the pair in the sorted list from 1 to n2. Your task is formulated like this: you should find the k-th pair in the ordered list of all possible pairs of the array you’ve been given.

Input
The first line contains two integers n and k (1 ≤ n ≤ 105, 1 ≤ k ≤ n2). The second line contains the array containing n integers a1, a2, …, an ( - 109 ≤ ai ≤ 109). The numbers in the array can coincide. All numbers are separated with spaces.

Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use cin, cout, streams or the %I64d specificator instead.

Output
In the single line print two numbers — the sought k-th pair.

Example
Input
2 4
2 1
Output
2 2
Input
3 2
3 1 5
Output
1 3
Note
In the first sample the sorted sequence for the given array looks as: (1, 1), (1, 2), (2, 1), (2, 2). The 4-th of them is pair (2, 2).

The sorted sequence for the array from the second sample is given in the statement. The 2-nd pair there is (1, 3).

在这题上卡了好久,我没有发现这题的坑,其实上我搜到那个点后,没有重新一遍遍跑过来,导致错误,

4 4
1 1 2 3
应该输出 1 1但我没有,所以改了就对了

#include<bits/stdc++.h>using namespace std;#define N 500001#define LL long longLL n;LL a[N];LL cnt;LL pg[N];LL pre;LL k;long long sz[N];int main(){    cin>>n;    cin>>k;    for(int i=1;i<=n;i++)     cin>>a[i];    sort(a+1,a+n+1);    pre=a[1];    sz[++cnt]=a[1];    pg[1]++;    for(int i=2;i<=n;i++)    {        if(a[i]==pre){pg[cnt]++;}        else        {            pre=a[i];            sz[++cnt]=a[i];            pg[cnt]=1;        }    }    long long res=0;    for(int i=1;i<=cnt;i++)    {        res=pg[i]*n;        if(res>=k)        {            for(int j=1;j<=n;j++)            {                if(k>pg[i]) k-=pg[i];                else                 {                    cout<<sz[i]<<" "<<a[j]<<endl;break;                }            }            break;        }        else k-=res;    }}

You are given a connected weighted undirected graph without any loops and multiple edges.

Let us remind you that a graph’s spanning tree is defined as an acyclic connected subgraph of the given graph that includes all of the graph’s vertexes. The weight of a tree is defined as the sum of weights of the edges that the given tree contains. The minimum spanning tree (MST) of a graph is defined as the graph’s spanning tree having the minimum possible weight. For any connected graph obviously exists the minimum spanning tree, but in the general case, a graph’s minimum spanning tree is not unique.

Your task is to determine the following for each edge of the given graph: whether it is either included in any MST, or included at least in one MST, or not included in any MST.

Input
The first line contains two integers n and m (2 ≤ n ≤ 105, ) — the number of the graph’s vertexes and edges, correspondingly. Then follow m lines, each of them contains three integers — the description of the graph’s edges as “ai bi wi” (1 ≤ ai, bi ≤ n, 1 ≤ wi ≤ 106, ai ≠ bi), where ai and bi are the numbers of vertexes connected by the i-th edge, wi is the edge’s weight. It is guaranteed that the graph is connected and doesn’t contain loops or multiple edges.

Output
Print m lines — the answers for all edges. If the i-th edge is included in any MST, print “any”; if the i-th edge is included at least in one MST, print “at least one”; if the i-th edge isn’t included in any MST, print “none”. Print the answers for the edges in the order in which the edges are specified in the input.

Example
Input
4 5
1 2 101
1 3 100
2 3 2
2 4 2
3 4 1
Output
none
any
at least one
at least one
any
Input
3 3
1 2 1
2 3 1
1 3 2
Output
any
any
none
Input
3 3
1 2 1
2 3 1
1 3 1
Output
at least one
at least one
at least one
Note
In the second sample the MST is unique for the given graph: it contains two first edges.

In the third sample any two edges form the MST for the given graph. That means that each edge is included at least in one MST.

这题是一个经典的求桥问题,也就是说,这道题就是在kruscal的基础上,要是这条边所连的两个点已经联通,那么说明这点一定不优,就是none否则
进行求桥,我们每次把一个连通块缩成一个点,然后对于权值相同的联建两个连通块,求桥,是桥的一定要选为any,否则为at least one,这样子跑一遍最小生成树就可以了。

#include<bits/stdc++.h>using namespace std;#define N 500001#define LL long longLL n;LL a[N];LL cnt;LL pg[N];LL pre;LL k;long long sz[N];int main(){    cin>>n;    cin>>k;    for(int i=1;i<=n;i++)     cin>>a[i];    sort(a+1,a+n+1);    pre=a[1];    sz[++cnt]=a[1];    pg[1]++;    for(int i=2;i<=n;i++)    {        if(a[i]==pre){pg[cnt]++;}        else        {            pre=a[i];            sz[++cnt]=a[i];            pg[cnt]=1;        }    }    long long res=0;    for(int i=1;i<=cnt;i++)    {        res=pg[i]*n;        if(res>=k)        {            for(int j=1;j<=n;j++)            {                if(k>pg[i]) k-=pg[i];                else                 {                    cout<<sz[i]<<" "<<a[j]<<endl;break;                }            }            break;        }        else k-=res;    }}

The main Bertown street is represented by a straight line. There are 109 bus stops located on the line. The stops are numbered with integers from 1 to 109 in the order in which they follow on the road. The city has n buses. Every day the i-th bus drives from stop number si to stop number fi (si < fi), it stops on all intermediate stops and returns only at night. The bus starts driving at time ti and drives so fast that it finishes driving also at time ti. The time ti is different for all buses. The buses have infinite capacity.

Bertown has m citizens. Today the i-th person should get from stop number li to stop number ri (li < ri); the i-th citizen comes to his initial stop (li) at time bi. Each person, on the one hand, wants to get to the destination point as quickly as possible, and on the other hand, definitely does not want to change the buses as he rides. More formally: the i-th person chooses bus j, with minimum time tj, such that sj ≤ li, ri ≤ fj and bi ≤ tj.

Your task is to determine for each citizen whether he can ride to the destination point today and if he can, find the number of the bus on which the citizen will ride.

Input
The first line contains two integers n and m (1 ≤ n, m ≤ 105) — the number of buses and the number of people.

Then n lines follow, each of them contains three integers: si, fi, ti (1 ≤ si, fi, ti ≤ 109, si < fi) — the description of the buses. It is guaranteed that all ti-s are different.

Then m lines follow, each of them contains three integers: li, ri, bi (1 ≤ li, ri, bi ≤ 109, li < ri) — the Bertown citizens’ description. Some bi-s could coincide.

Output
In the first line print m space-separated integers: the i-th number should be equal either to -1, if the person number i can’t get to the destination point, or to the number of the bus that will ride the person number i. The buses are numbered with integers from 1 to n in the input order.

Example
Input
4 3
1 10 10
5 6 2
6 7 3
5 7 4
5 7 1
1 2 1
1 10 11
Output
4 1 -1
Input
1 1
1 1000000000 1000000000
1 1000000000 1000000000
Output
1

我们把人和车一起读入,然后如果l比某个人小的,一定不会对其有影响,所以就可以利用这个性质,对于l排序,直接对于时间建线段树,维护r,然后每次优先左搜,搜到解输出即可。
三维偏序对CDQ分治其实是可以的。

%:pragma optimze(4) //一维确定,另两维要求单调时的做法 using namespace std;#include<cstdio>#include<iostream>#include<algorithm>#include<map>#include<queue>#define N 600001#define r(x) scanf("%d",&x)#define F(i,a,b) for(int i=a;i<=b;i++)#define D(i,a,b) for(int i=a;i>=b;i--)#define pk(x) printf("%d ",x)#define pln(x) printf("%d\n",x)#define p(x) printf("%d",x)#define lc no<<1#define rc no<<1|1#define getmid int mid=(L[no]+R[no])>>1#define pushup val[no]=max(val[lc],val[rc])int n,m,sz[N],tot,L[N],R[N],root,id[N],val[N];map<int,int>M;struct dance{int id,l,r,t;}xx[N];bool cmp(dance x,dance y){return (x.l<y.l||(x.l==y.l&&x.id<y.id));}void build(int no,int l,int r){    L[no]=l;R[no]=r;    if(l==r) return;getmid;    build(lc,l,mid);build(rc,mid+1,r);}void add(int no,int num,int c,int pos){    if(L[no]==R[no]){val[no]=c;id[no]=num;return;}getmid;    if(pos>mid) add(rc,num,c,pos);else add(lc,num,c,pos);     pushup;}int query(int no,int num,int l,int r){    if(val[no]<num) return -1;    if(L[no]==R[no]) return id[no];getmid;    if(l>mid) return query(rc,num,l,r);    if(r<=mid) return query(lc,num,l,r);    int lq=query(lc,num,l,mid);    if(lq!=-1) return lq;    return query(rc,num,mid+1,r); }int ans[N];int main(){    r(n);r(m);for(int i=1;i<=n;i++){r(xx[i].l);r(xx[i].r);r(xx[i].t);xx[i].id=i;}for(int i=1;i<=m;i++){r(xx[i+n].l);r(xx[i+n].r);r(xx[i+n].t);xx[i+n].id=i+n;}    sort(xx+1,xx+n+m+1,cmp);    for(int i=1;i<=n+m;i++){if(!M.count(xx[i].t)){sz[++tot]=xx[i].t,M[xx[i].t]=tot;}}    sort(sz+1,sz+tot+1);for(int i=1;i<=tot;i++){M[sz[i]]=i;}    build(1,1,tot);    for(int i=1;i<=n+m;i++)    {        int pos=M[xx[i].t];        if(xx[i].id<=n){add(1,xx[i].id,xx[i].r,pos);}        else        {            ans[xx[i].id-n]=query(1,xx[i].r,pos,tot);        }    }    F(i,1,m) pk(ans[i]);}
原创粉丝点击