SD五一联赛(加权并查集)

67 篇文章 0 订阅
40 篇文章 0 订阅

Arny

Time Limit: 3000MS Memory limit: 65536K

题目描述

Bad working coordinator was the everlasting trouble of their spaceship. Arny already had been working under this trouble while his not very attentive and responsible mate just noticed the breakage. Judging by schematics the broken module of coordinator consists of the set of special gears connected with each other in order to transfer the traction from the kinetic generator to the lot of antenna driving engines.
Despite the extraterrestrial origin of these gears, they are connected by usual terrestrial method: the cogs of one gear-wheel get into the slots between cogs of another gear-wheel. So the rotation of the first gear-wheel is transmitted to the second gear-wheel.
After the multiple Arny’s revisions, no unnecessary gears stayed in the coordinator. It means that there is no cycles in gears connection graph. The only problem now is to check that all the gears have right directions and speeds of rotation.

输入

There are many test cases.
First line of input contains the number of gear-wheels in mechanism N (1 ≤ N ≤ 1000). The next Nlines contain the information about the gear-wheels. i-th line contains K (1 ≤ K ≤ 1000) — the number of cogs on the i-th gear-wheel followed by the list of gears, that are connected to the i-th one. Zero ends the list.
The last line of input contains the number of gear-wheel, that is connected to the kinetic-generator and the speed of its rotation V (1 ≤ V ≤ 1000). This gear-wheel rotates in counter-clockwise direction.

输出

Output should contain N lines. In the i-th line there is a speed of rotation of i-th gear-wheel in the form of irreducible fraction. Numerator and denominator of this fraction should be separated by the sign ‘/’. If speed is negative, it is assumed that the gear-wheel rotates in clockwise direction (in this case the minus sign should be displayed before numerator). Otherwise the gear-wheel rotates in counter-clockwise direction. If speed equals zero than numerator should be equal 0 and denominator should be equal to 1. It is guaranteed that neither numerator nor denominator of all speeds will be greater than 10^6.

示例输入

4
10 2 3 0
20 1 0
40 1 4 0
100 3 0
1 6

示例输出

6/1
-3/1
-3/2
3/5


简单DFS


#include<iostream>  
#include<cstdio>  
#include<cstring>  
#include<vector>  
using namespace std;  
const int maxn=1010;  
long long num[maxn],st,sp;  
int n;  
vector<int> grid[maxn];  
int vis[maxn];  
long long fenzi[maxn],fenmu[maxn];  
long long gcd(long long a,long long b)  
{  
    if(b==0)return a;  
    return gcd(b,a%b);  
}  
void dfs(int u,int dir)  
{  
    int len=grid[u].size();  
    for(int i=0;i<len;i++)  
    {  
        int v=grid[u][i];  
        if(vis[v])continue;  
        vis[v]=1;  
        int g=gcd(num[st]*sp,num[v]);  
        if(dir)fenzi[v]=-num[st]*sp/g;  
        else fenzi[v]=num[st]*sp/g;  
        fenmu[v]=num[v]/g;  
        dfs(v,(dir+1)%2);  
    }  
}  
int main()  
{  
    //freopen("in.txt","r",stdin);  
    int x;  
    while(scanf("%d",&n)!=EOF)  
    {  
        for(int i=1;i<=n;i++)grid[i].clear();  
        for(int i=1;i<=n;i++)  
        {  
            scanf("%lld",&num[i]);  
            while(scanf("%d",&x)&&x)  
            {  
                grid[i].push_back(x);  
            }  
        }  
        scanf("%lld%lld",&st,&sp);  
        fenzi[st]=sp;fenmu[st]=1;  
        memset(vis,0,sizeof(vis));  
        vis[st]=1;  
        dfs(st,1);  
        for(int i=1;i<=n;i++)  
        {  
            if(!vis[i])printf("0/1\n");  
            else printf("%lld/%lld\n",fenzi[i],fenmu[i]);  
        }  
    }  
    return 0;  
}  
  

Monitor

Time Limit: 3000MS Memory limit: 65536K

题目描述

Reca company makes monitors, the most popular of their models is AB999 with the screen size a × b centimeters. Because of some production peculiarities a screen parameters are integer numbers. Recently the screen sides ratio x: y became popular with users. That\'s why the company wants to reduce monitor AB999 size so that its screen sides ratio becomes x: y, at the same time they want its total area to be maximal of all possible variants. Your task is to find the screen parameters of the reduced size model, or find out that such a reduction can\'t be performed.

输入

The first line of the input contains 4 integers — a, b, x and y (1 ≤ a, b, x, y ≤ 2·10^9).

输出

If the answer exists, output 2 positive integers — screen parameters of the reduced size model. Output 0 0 otherwise.

示例输入

800 600 4 3

示例输出

800 600

提示

Input
1920 1200 16 9
Output
1920 1080
Input
1 1 1 2
Output
0 0

刚开始想复杂了。。。

只要去最小的乘就好了


#include<cstdio>  
#include<iostream>  
#include<cstring>  
#include<cmath>  
using namespace std;  
long long gcd(long long a,long long b)  
{  
    return b==0?a:gcd(b,a%b);  
}  
int main()  
{  
    long long cnt;  
    long long x,y,a,b;  
    while(scanf("%lld%lld%lld%lld",&a,&b,&x,&y)!=EOF)  
    {  
        long long m;  
        m=gcd(x,y);  
        x=x/m;  
        y=y/m;
        cnt=min(a/x,b/y);  
        printf("%lld %lld\n",cnt*x,cnt*y);  
    }  
    return 0;  
} 

Mineral Water

Time Limit: 1000MS Memory limit: 65536K

题目描述

Laoshan mineral water is one of famous well-known mineral water, Tyrant(means”Tu Hao”) Chierush liked to drink this mineral water very much. He said that it is such a natural taste, but this is not the focus. One day Chierush went to the campus supermarket to buy n bottles of Laoshan mineral water, the boss see that he is an acquaintance, so he gave him a copy of preferential policies - he can change every m empty bottles into one bottle mineral water. Your task is to help Chierush calculate how many bottles of mineral water he can drink?

输入

The input consists of multiple test cases. The first line contains an integer T means the number of test cases. Each test case consists of one line with two numbers represent n and m. (T<=100, 2<=n,m<=2*10^9)

输出

For each test case, output one line, including one number that represents the answer.

示例输入

2
2 2
5 4

示例输出

3
6


题意:多少个空瓶子可以含多少瓶水的样子

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long LL;
LL n,m;
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%lld%lld",&n,&m);
        LL ans=n;
        while(n>=m)
        {
            ans+=n/m;
            n=n-n/m*m+n/m;
        }
        printf("%lld\n",ans);
    }
    return 0;
}

Bit Problem

Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^

题目描述

As everyone knows, any non-zero integer’s binary representation, must have at least one bit of the data 1, XiaoMing process whom wants to know if a binary representation of the rightmost 1 can change to 0, How much is this number will reduce.
But now, XiaoMing, busy job, no time to YY this question, so as ACMer your duty bound to help him solve this problem.
 

输入

Input consists of several cases, For each case, the first input is an integer N, then with N lines thar each integer is non zero integer. N=0 indicates the end of the input.
 

输出

For each set of case, we need to output N data corresponding to each row of data representation, the corresponding data in the most the 1 change 0, again resulting from the difference between the results with the original data. Each data finally output a blank line.
 

示例输入

2
9
3

5
19999
456
10000000
1
8

0

示例输出

Answer to case1:
1
1

Answer to case2:
1
8
128
1
8
找出最靠右的1

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long LL;
LL n,x;
int main()
{
    int cas=1;
    while(scanf("%lld",&n)!=EOF&&n)
    {
        printf("Answer to case%d:\n",cas++);
        while(n--)
        {
            scanf("%lld",&x);
            for(int i=0;i<64;i++)
            {
                if(x&(1<<i))
                {
                    printf("%lld\n",(1LL<<i));
                    break;
                }
            }
        }
        printf("\n");
    }
    return 0;
}

Fence Repair

Time Limit: 3000MS Memory limit: 65536K

题目描述

Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needs N (1 ≤ N ≤ 20,000) planks of wood, each having some integer length Li (1 ≤ Li ≤ 50,000) units. He then purchases a single long board just long enough to saw into the N planks (i.e., whose length is the sum of the lengths Li). FJ is ignoring the "kerf", the extra length lost to sawdust when a sawcut is made; you should ignore it, too.
FJ sadly realizes that he doesn\'t own a saw with which to cut the wood, so he mosies over to Farmer Don\'s Farm with this long board and politely asks if he may borrow a saw.
Farmer Don, a closet capitalist, doesn\'t lend FJ a saw but instead offers to charge Farmer John for each of the N-1 cuts in the plank. The charge to cut a piece of wood is exactly equal to its length. Cutting a plank of length 21 costs 21 cents.
Farmer Don then lets Farmer John decide the order and locations to cut the plank. Help Farmer John determine the minimum amount of money he can spend to create the N planks. FJ knows that he can cut the board in various different orders which will result in different charges since the resulting intermediate planks are of different lengths.

输入

Line 1: One integer N, the number of planks
Lines 2..N+1: Each line contains a single integer describing the length of a needed plank

输出

Line 1: One integer: the minimum amount of money he must spend to make N-1 cuts

示例输入

3
8
5
8

示例输出

34


哈夫曼树,优先队列

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<cmath>
using namespace std;
int main()
{
    int N;
    while(scanf("%d",&N)!=EOF)
    {
        priority_queue <int,vector<int>,greater<int> > q;
        long long i,t,a,b,s=0;
        for(i=0; i<N; i++)
        {
            scanf("%d",&t);
            q.push(t);
        }
        while(q.size()>1)
        {
            a=q.top();
            q.pop();
            b=q.top();
            q.pop();
            s+=a+b;
            q.push(a+b);
        }
        cout<<s<<endl;
    }
    return 0;
}


Best string Orz~

Time Limit: 3000MS Memory limit: 65536K

题目描述

If a string S is made up by n1 'O',n2 'r', n3 'z' and n4 '~',(n1>=n2>=n3>=n4),and for all of its prefix substring C,if in C the number of 'O' is larger or equal than the number of 'r',the number of 'r' is larger or equal than the number of 'z' and the number of 'z' is larger or equal than the number of '~',then we call it best string.(For example,the prefix substring of "Orz~" is "O","Or","Orz" and "Orz~").
Now give you the n1,n2,n3,n4,I wonder how many best strings are there with only n1 'O',n2 'r',n3 'z',n4 '~';

输入

There are more than one input.
Each input is four number n1,n2,n3,n4,end by four zeros,which shouldn\'t be processed.(0<=n1,n2,n3,n4<10)

输出

For each input,print the number of best string.

示例输入

1 1 1 1
0 0 0 0

示例输出

1


dp四维,递推

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
int check(int a,int b,int c,int d)
{
    if(a<0||b<0||c<0||d<0)
        return 0;
    if(a<=b&&b<=c&&c<=d)
        return 1;
    return 0;
}
long long dp[15][15][15][15];
int main()
{
    freopen("in.txt","r",stdin);
    int a,b,c,d;
    while(scanf("%d%d%d%d",&a,&b,&c,&d)!=EOF)
    {
        if(!a&&!b&&!c&&!d) break;
        memset(dp,0,sizeof(dp));
        dp[0][0][0][1]=1;
        for(int i=0;i<=d;i++)
            for(int j=0;j<=c;j++)
                for(int k=0;k<=b;k++)
                    for(int p=0;p<=a;p++)
                        if(check(i,j,k,p))
                        {
                            if(check(i,j,k,p-1))
                                dp[i][j][k][p]+=dp[i][j][k][p-1];
                            if(check(i,j,k-1,p))
                                dp[i][j][k][p]+=dp[i][j][k-1][p];
                            if(check(i,j-1,k,p))
                                dp[i][j][k][p]+=dp[i][j-1][k][p];
                            if(check(i-1,j,k,p))
                                dp[i][j][k][p]+=dp[i-1][j][k][p];
                        }
        printf("%lld\n",dp[d][c][b][a]);
    }
     return 0;
}



Parity

Time Limit: 3000ms   Memory limit: 65536K  有疑问?点这里^_^

题目描述

Now and then you play the following game with your friend. Your friend writes down a sequence consisting of zeroes and ones. You choose a continuous subsequence (for example the subsequence from the third to the fifth digit inclusively) and ask him, whether this subsequence contains even or odd number of ones. Your friend answers your question and you can ask him about another subsequence and so on.
Your task is to guess the entire sequence of numbers. You suspect some of your friend\'s answers may not be correct and you want to convict him of falsehood. Thus you have decided to write a program to help you in this matter. The program will receive a series of your questions together with the answers you have received from your friend. The aim of this program is to find the first answer which is provably wrong, i.e. that there exists a sequence satisfying answers to all the previous questions, but no such sequence satisfies this answer.

输入

Input contains a series of tests. The first line of each test contains one number, which is the length of the sequence of zeroes and ones. This length is less or equal to 10^9. In the second line, there is one non-negative integer which is the number of questions asked and answers to them. The number of questions and answers is less or equal to 5 000. The remaining lines specify questions and answers. Each line contains one question and the answer to this question: two integers (the position of the first and last digit in the chosen subsequence) and one word which is either “even” or “odd” (the answer, i.e. the parity of the number of ones in the chosen subsequence, where “even” means an even number of ones and “odd” means an odd number). The input is ended with a line containing −1.

输出

Each line of output containing one integer X. Number X says that there exists a sequence of zeroes and ones satisfying first X parity conditions, but there exists none satisfying X + 1 conditions. If there exists a sequence of zeroes and ones satisfying all the given conditions, then number X should be the number of all the questions asked.

示例输入

10
5
1 2 even
3 4 odd
5 6 even
1 6 even
7 10 odd
-1

示例输出

3

这题也是用到了偏移向量
一个由0,1组成的数字串~~,现在你问一个人一些问题,第i位到第j位的1的个数为奇数还是偶数。他会告诉你答案, 但是答案可能会自相矛盾,现在就是最多能有前几个回答是不矛盾的。
设r[i]表示第1位到第i位的1个数的奇偶状况,r[i] = 0表示有偶数个1,r[i] = 1表示有奇数个1。
那么要是第i位到第j位为偶数个1时,r[i-1] = 1, r[j] = 1 或r[i-1] = 0, r[j] = 0 所以 r[i-1] ^ r[j] = 0
要是为奇数个1时,r[i-1] = 1, r[j] = 0 或 r[i-1] = 0, r[j] = 1所以r[i-1] ^ r[j] = 1
那么我们可以使用并查集,用一个数组d[i]代表r[i]与其祖先的异或值
如果回答的两个区间的端点以前都出现过,那么就判断异或值是否与以前的那个相等
如果没出现过,就直接合并,更新异或值

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn=50010;
int pra[maxn];
int d[maxn];
int X[maxn];
int a[maxn],b[maxn];
char c[maxn];
int len,n,cnt;
void init()
{
    for(int i=0;i<=maxn;i++)
    {
        pra[i]=i;
        d[i]=0;
    }
}
int find(int x)
{
    if(x==pra[x])return x;
    int fa=find(pra[x]);
    d[x]=d[x]^d[pra[x]];
    pra[x]=fa;
    return fa;
}
bool can(int l,int r,int v)
{
    int fl=find(l);
    int fr=find(r);
    if(fl==fr){return (d[l]^d[r])==v;}
    pra[fl]=fr;
    d[fl]=d[l]^d[r]^v;
    return true;
}
int main()
{
    //freopen("in.txt","r",stdin);
    while(scanf("%d",&len)!=EOF&&len!=-1)
    {
        scanf("%d",&n);
        cnt=0;
        char s[20];
        for(int i=0;i<n;i++)
        {
            scanf("%d%d%s",&a[i],&b[i],s);
            a[i]--;
            X[cnt++]=a[i];
            X[cnt++]=b[i];
            c[i]=s[0];
        }
        sort(X,X+cnt);
        cnt=unique(X,X+cnt)-X;
        init();
        bool flag=false;
        for(int i=0;i<n;i++)
        {
            int l=lower_bound(X,X+cnt,a[i])-X;
            int r=lower_bound(X,X+cnt,b[i])-X;
            if(!can(l,r,(c[i]=='o'?1:0)))
            {
                flag=true;
                printf("%d\n",i);
                break;
            }
        }
        if(!flag)printf("%d\n",n);
    }
    return 0;
}

Remove Trees

题目描述

Shandong University of Science and Technology grove is a place frequented by lovers, there is a small drain fir trees grow particularly close, it\'s a good line of sight blocked. Assuming a row of trees with n trees, the headmaster allow us only cut m trees (except the first and the last tree), your task is to be calculated after cut m trees, the maximum of the shortest distance of remaining trees that is adjacent.

输入

There are multiple test cases.
For each case:
Line 1: Three space-separated integers: n, and m(2<=m,n<=50000,m<=n-2)
Lines 2..N+1: Each line contains a single integer l(0<=l<=1000000000) indicating how far the tree is away from the starting tree. No two rocks share the same position.

输出

Line 1: A single integer that is the maximum of the shortest distance.

示例输入

7 2
0
11
2
14
21
17
25

示例输出

4


最小值最大化,二分+贪心

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=50010;
int m,n;
int a[maxn];
bool can(int mid)
{
    if(a[n]-a[1]<mid)return false;
    int cnt=m;
    int i=2,x=a[1];
    while(i<=n)
    {
        if(a[i]-x<mid)
        {
            if(cnt<=0)return false;
            cnt--;
        }
        else x=a[i];
        i++;
    }
    return cnt>=0;
}
void solve()
{
    int l=0,r=1000000000,mid;
    int ans;
    while(l<r)
    {
        int mid=(l+r)/2;
        if(can(mid)){ans=mid;l=mid+1;}
        else r=mid;
    }
    printf("%d\n",ans);
}
int main()
{
    //freopen("in.txt","r",stdin);
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(int i=1;i<=n;i++)scanf("%d",&a[i]);
        sort(a+1,a+n+1);
        solve();
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值