Practice_Codeforces Round #406 (Div. 2)

来源:互联网 发布:php工程师需要具备 编辑:程序博客网 时间:2024/05/20 20:18

蒟蒻的水题之路

嗨呀,好像又只是做了A+B呀,窝好菜啊

没错,窝只会A+B


A. The Monster
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

A monster is chasing after Rick and Morty on another planet. They're so frightened that sometimes they scream. More accurately, Rick screams at times b, b + a, b + 2a, b + 3a, ... and Morty screams at times d, d + c, d + 2c, d + 3c, ....

The Monster will catch them if at any point they scream at the same time, so it wants to know when it will catch them (the first time they scream at the same time) or that they will never scream at the same time.

Input

The first line of input contains two integers a and b (1 ≤ a, b ≤ 100).

The second line contains two integers c and d (1 ≤ c, d ≤ 100).

Output

Print the first time Rick and Morty will scream at the same time, or  - 1 if they will never scream at the same time.

Examples
input
20 29 19
output
82
input
2 116 12
output
-1
Note

In the first sample testcase, Rick's 5th scream and Morty's 8th time are at time 82.

In the second sample testcase, all Rick's screams will be at odd times and Morty's will be at even times, so they will never scream at the same time.


题目链接:http://codeforces.com/contest/787/problem/A


题意:给定a,b,c,d四个数,问是否存在b+ia = d+jc, i与j为非负数,注意,为最小相等的值,若存在输出其值。

题解:You need to find out if there are non-negative integers like i and j such ai + b = cj + d and i or j (or both) is minimized. It's easy to show that if a, b, c, d ≤ N, and such i and j exist, then i, j ≤ N, so you can iterate over i and check if such j exists.Time complexity: 

代码:

#include <iostream>#define inf 0x3f3f3f3f#define maxn 1000using namespace std;int main(){    ios::sync_with_stdio(false);    cin.tie(0);    int a, b, c, d;    cin >> a >> b >> c >> d;    int ans = inf;    for (int i = 0; i < maxn; i++)        for (int j = 0; j < maxn; j++)            if(b + a * i == d + c * j)                ans = min(ans, b + a * i);    if(ans != inf)        cout << ans << endl;    else        cout << "-1\n";    return 0;}

B. Not Afraid
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Since the giant heads have appeared in the sky all humanity is in danger, so all Ricks and Mortys from all parallel universes are gathering in groups to find a solution to get rid of them.

There are n parallel universes participating in this event (n Ricks and n Mortys). I. e. each of n universes has one Rick and one Morty. They're gathering in m groups. Each person can be in many groups and a group can contain an arbitrary number of members.

Ricks and Mortys have registered online in these groups. So, a person can have joined a group more than once (developer of this website hadn't considered this possibility).

Summer from universe #1 knows that in each parallel universe (including hers) exactly one of Rick and Morty from that universe is a traitor and is loyal, but no one knows which one. She knows that we are doomed if there's a group such that every member in that group is a traitor (they will plan and destroy the world).

Summer knows that if there's a possibility that world ends (there's a group where all members are traitors) she should immediately cancel this event. So she wants to know if she should cancel the event. You have to tell her yes if and only if there's at least one scenario (among all 2n possible scenarios, 2 possible scenarios for who a traitor in each universe) such that in that scenario the world will end.

Input

The first line of input contains two integers n and m (1 ≤ n, m ≤ 104) — number of universes and number of groups respectively.

The next m lines contain the information about the groups. i-th of them first contains an integer k (number of times someone joined i-th group, k > 0) followed by k integers vi, 1, vi, 2, ..., vi, k. If vi, j is negative, it means that Rick from universe number  - vi, j has joined this group and otherwise it means that Morty from universe number vi, j has joined it.

Sum of k for all groups does not exceed 104.

Output

In a single line print the answer to Summer's question. Print "YES" if she should cancel the event and "NO" otherwise.

Examples
input
4 21 -34 -2 3 2 -3
output
YES
input
5 25 3 -2 1 -1 53 -5 2 5
output
NO
input
7 23 -1 6 77 -5 4 2 4 7 -3 4
output
YES

题目链接:http://codeforces.com/contest/787/problem/B


题意:有n个数,m个组,每组k个数,如果每组都存在一个数的正负表示,输出 NO  否则输出YES。

题解:用的暴力,枚举,看每组是否是否存在两个数相加等于0,如果存在,则cnt加一,读入下一组数据继续,如果最后cnt等于组数m,说明每组均存在。

代码:

#include <iostream>using namespace std;const int maxn = 10010;int a[maxn];int main(){    ios::sync_with_stdio(false);    cin.tie(0);    int n, m, k;    cin >> n >> m;    int t = m, cnt = 0;    while (m--)    {        bool f = false;        cin >> k;        for (int i = 0; i < k; i++)            cin >> a[i];        for (int i = 0; i < k; i++)        {            for (int j = i + 1; j < k; j++)                if(a[i] + a[j] == 0)                {                    cnt++;                    f = true;                    break;                }            if(f)                break;        }    }    if (cnt == t)        cout << "NO\n";    else        cout << "YES\n";    return 0;}

学习学习其他人的做法:Codeforces Round #406 (Div. 2) B. Not Afraid


分析 需要同时记录一个数的正负形式,判断它的正负是否出现过,然后想到map的映射。

判断mp[a]和mp[-a]是否都出现过

自己出现的问题 

1.一开始以为map<int,int>和数组一样= = 然后发现用数组的负下标是不能过的(对负下标这方面不了解0.0)

2.输入数据结束前,输出结果(简直作死- -因为这个分都掉光了)。

#include <cstdio>#include <iostream>#include <algorithm>#include<cstring>#include <map>#include <cstdlib>#define T 1001000using namespace std;typedef long long ll;map<int,int>mp;int  c[10010];int main(){   int  n,m,k,f,q;   scanf("%d%d",&n,&m);       q=0;       for(int i=0;i<m;i++)       {           f=0;           scanf("%d",&k);           memset(c,0,sizeof(c));            mp.clear();           for(int j=0;j<k;j++)           {               scanf("%d",&c[j]);                mp[c[j]]=1;               if(mp[c[j]]==1&&mp[-c[j]]==1)                 f=1;           }          if(f==0)            q=1;       }      if(q==1)cout<<"YES"<<endl;      else  cout<<"NO"<<endl;}

不太懂map,得多看看!


The end.

0 0
原创粉丝点击