这个题感觉之前见过,当时大一上册趣味竞赛的一道题,结果放到题库的时候他们把题改了,怎么整也整不出来,做到最后才被告知要用拓扑排序做,当时差点崩溃。
现在学了离散有了一点印象,前几天刚好也看了一点拓扑排序。
用一个数组记录关系数,当它的值为0的时候就从队列弹出,然后和他有关系的都减1,直到弹完整个序列。
 
有N个比赛队(1<=N<=500),编号依次为1,2,3,。。。。,N进行比赛,比赛结束后,裁判委员会要将所有参赛队伍从前往后依次排名,但现在裁判委员会不能直接获得每个队的比赛成绩,只知道每场比赛的结果,即P1赢P2,用P1,P2表示,排名时P1在P2之前。现在请你编程序确定排名。 

Input输入有若干组,每组中的第一行为二个数N(1<=N<=500),M;其中N表示队伍的个数,M表示接着有M行的输入数据。接下来的M行数据中,每行也有两个整数P1,P2表示即P1队赢了P2队。 
Output给出一个符合要求的排名。输出时队伍号之间有空格,最后一名后面没有空格。 

其他说明:符合条件的排名可能不是唯一的,此时要求输出时编号小的队伍在前;输入数据保证是正确的,即输入数据确保一定能有一个符合要求的排名。 
Sample Input

4 3
1 2
2 3
4 3

Sample Output

1 2 4 3
#include <iostream>
#include<string.h>
#include<math.h>
#include<stdio.h>
#include<algorithm>
#include<queue>
using namespace std;
int str[505][505],a[505];
priority_queue<int,vector<int>,greater <int> >que;
queue<int>que2;
int main()
{
   int n,m,i,u,v;
   while(~scanf("%d%d",&n,&m))
   {
       memset(a,0,sizeof(a));
       memset(str,0,sizeof(str));
       for(i=0;i<m;i++)
       {
           cin>>u>>v;
           if(!str[u][v])
           {
           str[u][v]=1;
           a[v]++;
           }
       }
   for(i=1;i<=n;i++)
   {
       if(a[i]==0)
        que.push(i);
   }
       while(!que.empty())
       {
           int t=que.top();
           que.pop();
           que2.push(t);
           for(i=1;i<=n;i++)
           {
               if(str[t][i]==1)
               {
                   a[i]--;
                   if(a[i]==0)
                    que.push(i);
               }
           }
       }
   int kepa=1;
   while(!que2.empty())
   {
       int k=que2.front();
       que2.pop();
      if(kepa)
      {
          cout<<k;
          kepa=0;
      }
      else
      {
          cout<<" "<<k;
      }
   }
   cout<<endl;
   }
   return 0;
}

昨天的比赛打的我胃疼,先说这个最简单的I,用贪心做,分成非平方数和平方数两堆,注意0要变到非平方数要2步,然后比较两堆个数,移二分之差值就可以了。

Ann and Borya have n piles with candies and n is even number. There are ai candies in pile with number i.

Ann likes numbers which are square of some integer and Borya doesn't like numbers which are square of any integer. During one move guys can select some pile with candies and add one candy to it (this candy is new and doesn't belong to any other pile) or remove one candy (if there is at least one candy in this pile).

Find out minimal number of moves that is required to make exactly n / 2 piles contain number of candies that is a square of some integer and exactly n / 2 piles contain number of candies that is not a square of any integer.

Input

First line contains one even integer n (2 ≤ n ≤ 200 000) — number of piles with candies.

Second line contains sequence of integers a1, a2, ..., an (0 ≤ ai ≤ 109) — amounts of candies in each pile.

Output

Output minimal number of steps required to make exactly n / 2 piles contain number of candies that is a square of some integer and exactly n / 2 piles contain number of candies that is not a square of any integer. If condition is already satisfied output 0.

Examples

Input
4
12 14 30 4
Output
2
Input
6
0 0 0 0 0 0
Output
6
Input
6
120 110 23 34 25 45
Output
3
Input
10
121 56 78 81 45 100 1 0 54 78
Output
0
#include <cstdio>
#include <iostream>
#include <cmath>
#include <algorithm>
#define INF 1e-8
using namespace std;
int main()
{
    int n,m,t=0,w=0;
    int a[200005],b[200005];
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>m;
        int ss=sqrt(m*1.0);
        if(m!=ss*ss){
            ss=min(m-ss*ss,(ss+1)*(ss+1)-m);
            a[t++]=ss;
        }
        else {
            if(m==0) b[w++]=2;
            else b[w++]=1;
        }
    }
    sort(a,a+t);
    sort(b,b+w);
    long long sum=0;
    if(w==t) sum=0;
    else if(w>t){
        int q=w-t;
        for(int i=0;i<q/2;i++) sum+=b[i];
    }
    else{
        int q=t-w;
        for(int i=0;i<q/2;i++) sum+=a[i];
    }
    cout<<sum<<endl;
}

然后是G,当时看这道题觉得好简单呀,肯定能很快做出来,结果这是这里面最可怕的一道题。

我和pzr刚开始拿到题都觉得能很快把它做出来,结果我做了一个小时没搞出来,pzr做了2个小时改了3次代码终于把样例过了结果模拟还是超时了。

这道题应该用优先队列刚,设置一个前缀数组和后缀数组,难点就是删除元素之后可能前面和后面刚好相等,那这个就要提出来判断,把二者压入队列,然后再设一个优先队列来存这种情况相同的元素,判断的时候两个队列同时弹出元素,并不计入操作数

Vasya has an array of integers of length n.

Vasya performs the following operations on the array: on each step he finds the longest segment of consecutive equal integers (the leftmost, if there are several such segments) and removes it. For example, if Vasya's array is [13, 13, 7, 7, 7, 2, 2, 2], then after one operation it becomes [13, 13, 2, 2, 2].

Compute the number of operations Vasya should make until the array becomes empty, i.e. Vasya removes all elements from it.

Input

The first line contains a single integer n (1 ≤ n ≤ 200 000) — the length of the array.

The second line contains a sequence a1, a2, ..., an (1 ≤ ai ≤ 109) — Vasya's array.

Output

Print the number of operations Vasya should make to remove all elements from the array.

Examples

Input
4
2 5 5 2
Output
2
Input
5
6 3 4 1 5
Output
5
Input
8
4 4 4 2 2 100 100 100
Output
3
Input
6
10 10 50 10 50 50
Output
4
#include<iostream>
#include<string.h>
#include<stdio.h>
#include<math.h>
#include<queue>
#include<algorithm>
using namespace std;
long long fa[300000],next[300000],kepa[300000],seqing[300000];
struct node
{
    long long x;
    long long y;
    node() {};
    node(long long c,long long d)
    {
        x=c;
        y=d;
    }
};
bool operator < (node a,node b)
{
    if(a.x==b.x)
        return a.y>b.y;
    return a.x<b.x;
}
priority_queue<node>que,q;
int main()
{
    long long n,i,x;
    cin>>n;
    long long cnt=0;
    for(i=1; i<=n; i++)
    {
        cin>>x;
        if(kepa[cnt]==x)
            seqing[cnt]++;
        else
        {
            kepa[++cnt]=x;
            seqing[cnt]=1;
        }
    }
    for(i=1; i<=cnt; i++)
    {
        fa[i]=i-1;
        next[i]=i+1;
        que.push(node(seqing[i],i));
    }
    long long ans=0;
    for(i=1; i<=cnt; i++)
    {
        while(!q.empty()&&q.top().x==que.top().x&&q.top().y==que.top().y)
        {
            q.pop();
            que.pop();
        }
        long long k=que.top().y;
        que.pop();
        long long t1=fa[k],t2=next[k];
        next[t1]=t2;
        fa[t2]=t1;
        if(t1&&kepa[t1]==kepa[t2])
        {
            q.push(node(seqing[t1],t1));
            q.push(node(seqing[t2],t2));
            seqing[t1]+=seqing[t2];
            next[t1]=next[t2];
            fa[next[t2]]=t1;
            que.push(node(seqing[t1],t1));
            i++;
        }
        ans++;
    }
    cout<<ans<<endl;
    return 0;
}

再然后是J,这道题是一个纯模拟题,但是要用到很复杂的容器,我设置一个名叫色情的vector存名字,一个名叫可怕的set数组存每一个编号的电话号码,每一个后缀都设为1,如果它是前面某个电话的后缀,那就要在可怕的数组set中删除这个元素。

Vasya has several phone books, in which he recorded the telephone numbers of his friends. Each of his friends can have one or several phone numbers.

Vasya decided to organize information about the phone numbers of friends. You will be given n strings — all entries from Vasya's phone books. Each entry starts with a friend's name. Then follows the number of phone numbers in the current entry, and then the phone numbers themselves. It is possible that several identical phones are recorded in the same record.

Vasya also believes that if the phone number a is a suffix of the phone number b(that is, the number b ends up with a), and both numbers are written by Vasya as the phone numbers of the same person, then a is recorded without the city code and it should not be taken into account.

The task is to print organized information about the phone numbers of Vasya's friends. It is possible that two different people have the same number. If one person has two numbers x and y, and x is a suffix of y (that is, y ends in x), then you shouldn't print number x. If the number of a friend in the Vasya's phone books is recorded several times in the same format, it is necessary to take it into account exactly once.

Read the examples to understand statement and format of the output better.

Input

First line contains the integer n (1 ≤ n ≤ 20) — number of entries in Vasya's phone books.

The following n lines are followed by descriptions of the records in the format described in statement. Names of Vasya's friends are non-empty strings whose length does not exceed 10. They consists only of lowercase English letters. Number of phone numbers in one entry is not less than 1 is not more than 10. The telephone numbers consist of digits only. If you represent a phone number as a string, then its length will be in range from 1 to 10. Phone numbers can contain leading zeros.

Output

Print out the ordered information about the phone numbers of Vasya's friends. First output m — number of friends that are found in Vasya's phone books.

The following m lines must contain entries in the following format "name number_of_phone_numbers phone_numbers". Phone numbers should be separated by a space. Each record must contain all the phone numbers of current friend.

Entries can be displayed in arbitrary order, phone numbers for one record can also be printed in arbitrary order.

Examples

Input
2
ivan 1 00123
masha 1 00123
Output
2
masha 1 00123
ivan 1 00123
Input
3
karl 2 612 12
petr 1 12
katya 1 612
Output
3
katya 1 612
petr 1 12
karl 1 612
Input
4
ivan 3 123 123 456
ivan 2 456 456
ivan 8 789 3 23 6 56 9 89 2
dasha 2 23 789
Output
2
dasha 2 23 789
ivan 4 789 123 2 456
#include<iostream>
#include<string.h>
#include<stdio.h>
#include<math.h>
#include<queue>
#include<set>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;
set<string>kepa[25];
vector<string>seqing;
map<string,int>haha,phone[25];
int main()
{
    string s1,s2,s3;
    int n,t,i,j;
    cin>>n;
    int m=0;
    while(n--)
    {
        cin>>s1>>t;
        if(!haha[s1])
        {
            haha[s1]=++m;
            seqing.push_back(s1);
        }
        int pos=haha[s1];
        for(i=0; i<t; i++)
        {
            cin>>s2;
            s3=s2;
            reverse(s2.begin(),s2.end());
            if(!phone[pos][s2])
            {
                s2.clear();
                kepa[pos].insert(s3);
                for(j=s3.length()-1; j>=0; j--)
                {
                    s2+=s3[j];
                    if(phone[pos][s2])
                    {
                        reverse(s2.begin(),s2.end());
                        kepa[pos].erase(s2);
                        reverse(s2.begin(),s2.end());
                    }
                    phone[pos][s2]=1;
                }
            }
        }
    }
        cout<<m<<endl;
        for(i=1;i<=m;i++)
        {
            cout<<seqing[i-1];
            cout<<" "<<kepa[i].size();
            set<string>::iterator it;
            for(it=kepa[i].begin();it!=kepa[i].end();it++)
            {
                cout<<" "<<(*it);
            }
            cout<<endl;

        }

    return 0;
}

上帝保佑明天比赛转运!!!

posted on 2018-08-09 18:51  可怕hiahia  阅读(368)  评论(0编辑  收藏  举报