Codeforces Round #346 (Div. 2) (659a,659b,659c,659d (geometric fork Multiply), 659E (and set))

Source: Internet
Author: User

Round House Topic Links:

Http://codeforces.com/problemset/problem/659/A

Problem Solving Ideas:

the answer for the problem are calculated with a formula < Span class= "Tex-span" style= "font-size:17.5px; Font-family: ' Times New Roman ', Sans-serif; White-space:nowrap "> (( a ?-? 1?+? b )    n  +  n )    n  + 1 .

Such solution has complexity O(1).

There is also a solution with iterations, modelling every of | b|' s Vasya ' s moves by one entrance one through desired direction, allowed to pass all the tests.

This solution ' s complexity is O(| b|).


AC Code:

#include <bits/stdc++.h>using namespace Std;int main () {    int n,a,b;    while (~SCANF ("%d%d%d", &n,&a,&b)) {        int ans = (a+b)%n;        if (ans < 0)            ans + = n;        if (ans = = 0)            ans = n;        printf ("%d\n", ans);    }    return 0;}
Qualifying Contest Topic Links:

Http://codeforces.com/problemset/problem/659/B

Problem Solving Ideas:

Let's consider the participants from every region separately. So for every, we just need to sort all of its participants by their score in non-increasing order. The answer for a region was inconsistent if and only if the score of the second and the third participant in this order is Equal, otherwise the answer is the first and the second participant in this order.

The solution complexity is.


AC Code:

#include <bits/stdc++.h>using namespace std;struct node{string name;    int score;        Node (string _name,int _score) {name = _name;    Score = _score; }};int vis[10005];vector<node> No[10005];bool CMP (node A,node b) {return a.score > b.score;}    int main () {int n,m;        while (~SCANF ("%d%d", &n,&m)) {for (int i = 0; I <= m; i++) no[i].clear ();        String name;        int Region,score;            for (int i = 0; i < n; i++) {cin>>name>>region>>score;        No[region].push_back (Node (name,score));            } for (int i = 1; I <= m; i++) {sort (No[i].begin (), No[i].end (), CMP);            int len = No[i].size ();            if (Len > 2 && no[i][1].score = = No[i][2].score) puts ("?");        else cout<<no[i][0].name<< "" <<no[i][1].name<<endl; }} return 0;}
Tanya and Toys Topic Links:

Http://codeforces.com/problemset/problem/659/C

Problem Solving Ideas:

Our task was to take largest amount of toys Tanya doesn ' t has yet the the-the-the-the--the sum of their costs doesn ' t exceedm. To does that one can perform greedy Algorithm:let's buy the cheepest toy Tanya doesn ' t has at every step, while the amount Of money left is sufficient to do. The Boolean arrayusedCan is a handle in that, storingtrueValues in indices equal to toy types which Tanya does has at the moment. As soon asTen9 Money are sufficient to buy no more thanTen5 Toys,usedIs enough-be sized2?x?105 (We won ' t buy the toys with types numbered greater). So we just need to iterate over the number of type we want to buy, and if corresponding value inusedis equal tofalse, we should buy it, otherwise we can ' t.

The solution complexity is.

One can use the <> data structure (c + + \texttt{std::set}, for example), for storing the types Tanya have at the Momen T. The complexity is.


AC Code:

#include <bits/stdc++.h>using namespace std;vector<int> v;map<int,int> ma;int main () {    int n,m;    while (~SCANF ("%d%d", &n,&m)) {        ma.clear ();        V.clear ();        int x;        for (int i = 0; i < n; ++i) {            scanf ("%d", &x);            ma[x]++;        }        for (int i = 1; I <= 1000000; ++i) {            if (i > M) break                ;            if (!ma[i]) {                v.push_back (i);                M-= i;            }        }        int len = V.size ();        printf ("%d\n", Len);        if (len) {            for (int i = 0; i < len-1; ++i)                printf ("%d", V[i]);            printf ("%d\n", v[len-1]);        }    }    return 0;}


Bicycle Race

Topic Links:

Http://codeforces.com/problemset/problem/659/D

Problem Solving Ideas:

From the track description follows that Maria moves the the-the-the-the-water always located-the right from her, so she-CO Uld fall into the water only while turning left. To check if the turn are to the left and let's give every Maria ' s moves directions a number:moving to the north-0, moving to the west-1, to the south-2and to the east-3. Then the turn was to the left if and only if the number of direction after performing a turndiris equal to the number before performing a turnOlddirPlus one modulo4.

This solution has complexity O(n).

One can solve this problem in alternative. Let the answer is equal tox(That means, the number of inner corners of theDegrees equalsx, but the number of inner corners of -Degrees to N?-? x ). As soon as the sum of the inner corners ' values of polygon ofNVertices is equal to180?x? (n?-? 2), then x? x?270?+? (n?-? x)? X?90equals to180?x? (n?-? 2). This leads us to, being the answer for the problem calculated in O(1).


AC Code:

#include <bits/stdc++.h>using namespace std;struct point{    int x, y;    Point (int x=0,int y=0): X (x), Y (y) {}//constructor, convenient code writing}po[1005];typedef point Vector;  From the program implementation, the vector is just the alias of point//dot-point = vector vectors operator-(vector a,vector B) {    return vector (A.X-B.X,A.Y-B.Y);} int Cross (Vector a,vector B) {    return a.x*b.y-a.y*b.x;} int main () {    int n;    while (~SCANF ("%d", &n)) {for        (int i = 0; I <= N; ++i)            scanf ("%d%d", &po[i].x,&po[i].y);        int ans = 0;        for (int i = 2; I <= N, ++i) {            if (cross (Po[i]-po[i-1],po[i-1]-po[i-2]) < 0)//counter-clockwise angle                ans++;        }        printf ("%d\n", ans);    }    return 0;}

New Reform Topic Links:

Http://codeforces.com/problemset/problem/659/E

Problem Solving Ideas:

let This connected graph (Of  n  vertices) contain  n ?-? 1  edge (such is called a tree). If one maintain a DFS from all of its vertex, every edge'll be oriented, and each of the them could given to its ending vert Ex, this-on-the-every vertex (except the one we launched DFS from, which is the root) would be a satisfied by an edge. In this case, the answer is equal to 1 .

let ' s then deal with a case when the graph contains more Than&nbs P N ?-? 1  edges. This graph contains at least one cycle. Let's take arbitrary vertex from any of the cycles and launch a DFS (as above) from it. All vertices except chosen would be satisfied and so we is to give a edge to the chosen vertex. As soon as chosen vertex belongs to a cycle, at least one of its edge is not being taken to account for the DFS, so it can B E given to a root. This is the vertices would be satisfied.

Now we is able to solve the task for any connected graph, so we is to divide the graph into a connected Components-thi s can be easily do by DFS or BFS.

The solution complexity is O(n? +? M).


AC Code:

#include <bits/stdc++.h>using namespace Std;const int N = 100005;int fa[n],tot[n],tot[n];int findset (int x) {if (f    A[X] = = x) return fa[x]; return fa[x] = Findset (Fa[x]);}    int main () {int n,m;        while (~SCANF ("%d%d", &n,&m)) {memset (tot,0,sizeof (tot));        memset (tot,0,sizeof (TOT));            for (int i = 1; I <= n; ++i) {fa[i] = i;            Tot[i] = 1;        Tot[i] = 0;        } int u,v;            for (int i = 1; I <= m; ++i) {scanf ("%d%d", &u,&v);            U = findset (u), V = findset (v);            if (U = = v) tot[u]++;                else{Fa[u] = v;                TOT[V] + = Tot[u];                TOT[V] + = Tot[u] + 1;            Tot[u] = Tot[u] = 0;        }} int ans = 0;            for (int i = 1; I <= n; ++i) {findset (i); if (fa[i] = = i) ans + = Tot[i] > tot[i]?        1:0;    } printf ("%d\n", ans); }return 0;}


Codeforces Round #346 (Div. 2) (659a,659b,659c,659d (geometric fork Multiply), 659E (and set))

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.