Codeforces Round #492 (Div. 2) [Thanks, uDebug!]

A. Hit the Lottery
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Allen has a LOT of money. He has nn dollars in the bank. For security reasons, he wants to withdraw it in cash (we will not disclose the reasons here). The denominations for dollar bills are 11, 55, 1010, 2020, 100100. What is the minimum number of bills Allen could receive after withdrawing his entire balance?

Input

The first and only line of input contains a single integer nn (1n1091≤n≤109).

Output

Output the minimum number of bills that Allen could receive.

Examples
input
Copy
125
output
Copy
3
input
Copy
43
output
Copy
5
input
Copy
1000000000
output
Copy
10000000
Note

In the first sample case, Allen can withdraw this with a 100100 dollar bill, a 2020 dollar bill, and a 55 dollar bill. There is no way for Allen to receive 125125 dollars in one or two bills.

In the second sample case, Allen can withdraw two 2020 dollar bills and three 11 dollar bills.

In the third sample case, Allen can withdraw 100000000100000000 (ten million!) 100100 dollar bills.

A题:题意:n=a1*100+a2*20+a3*10+a4*5+a5;求 a1+a2+a3+a4+a5的最小值

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
    ll n;cin>>n;
    ll num1=0,num2=0,num3=0,num4=0,num5=0;
    num1=n/100;n%=100;
    num2=n/20;n%=20;
    num3=n/10;n%=10;
    num4=n/5;n%=5;
    num5=n;
    cout<<num1+num2+num3+num4+num5<<endl;
    return 0;
} 
View Code
B. World Cup
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Allen wants to enter a fan zone that occupies a round square and has nn entrances.

There already is a queue of aiai people in front of the ii-th entrance. Each entrance allows one person from its queue to enter the fan zone in one minute.

Allen uses the following strategy to enter the fan zone:

  • Initially he stands in the end of the queue in front of the first entrance.
  • Each minute, if he is not allowed into the fan zone during the minute (meaning he is not the first in the queue), he leaves the current queue and stands in the end of the queue of the next entrance (or the first entrance if he leaves the last entrance).

Determine the entrance through which Allen will finally enter the fan zone.

Input

The first line contains a single integer nn (2n1052≤n≤105) — the number of entrances.

The second line contains nn integers a1,a2,,ana1,a2,…,an (0ai1090≤ai≤109) — the number of people in queues. These numbers do not include Allen.

Output

Print a single integer — the number of entrance that Allen will use.

Examples
input
Copy
4
2 3 2 0
output
Copy
3
input
Copy
2
10 10
output
Copy
1
input
Copy
6
5 2 6 5 7 4
output
Copy
6
Note

In the first example the number of people (not including Allen) changes as follows: [2,3,2,0][1,2,1,0][0,1,0,0][2,3,2,0]→[1,2,1,0]→[0,1,0,0]. The number in bold is the queue Alles stands in. We see that he will enter the fan zone through the third entrance.

In the second example the number of people (not including Allen) changes as follows:[10,10][9,9][8,8][7,7][6,6][5,5][4,4][3,3][2,2][1,1][0,0][10,10]→[9,9]→[8,8]→[7,7]→[6,6]→[5,5]→[4,4]→[3,3]→[2,2]→[1,1]→[0,0].

In the third example the number of people (not including Allen) changes as follows:[5,2,6,5,7,4][4,1,5,4,6,3][3,0,4,3,5,2][2,0,3,2,4,1][1,0,2,1,3,0][0,0,1,0,2,0]

 

B题:题意 >>加全局变量lazy标记即可>>
收获:即1-n变成0-n-1>>更方便的应用了t%n

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
int a[N];
int main(){
    std::ios::sync_with_stdio(false);
    int n;cin>>n;int min_=1e9+10;
    for(int i=0;i<n;i++) cin>>a[i],min_=min(min_,a[i]);
    int lazy=min_;
    int t=min_;t%=n;
    while(1){
        if(a[t]-lazy<=0){ cout<<t+1<<endl;return 0;}
        else{lazy++;}
        t++;t%=n;
    }
    return 0;
}
View Code
C. Tesla
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Allen dreams of one day owning a enormous fleet of electric cars, the car of the future! He knows that this will give him a big status boost. As Allen is planning out all of the different types of cars he will own and how he will arrange them, he realizes that he has a problem.

Allen's future parking lot can be represented as a rectangle with 44 rows and nn (n50n≤50) columns of rectangular spaces, each of which can contain at most one car at any time. He imagines having kk (k2nk≤2n) cars in the grid, and all the cars are initially in the second and third rows. Each of the cars also has a different designated parking space in the first or fourth row. Allen has to put the cars into corresponding parking places.

Illustration to the first example.

However, since Allen would never entrust his cars to anyone else, only one car can be moved at a time. He can drive a car from a space in any of the four cardinal directions to a neighboring empty space. Furthermore, Allen can only move one of his cars into a space on the first or fourth rows if it is the car's designated parking space.

Allen knows he will be a very busy man, and will only have time to move cars at most 2000020000 times before he realizes that moving cars is not worth his time. Help Allen determine if he should bother parking his cars or leave it to someone less important.

Input

The first line of the input contains two space-separated integers nn and kk (1n501≤n≤50, 1k2n1≤k≤2n), representing the number of columns and the number of cars, respectively.

The next four lines will contain nn integers each between 00 and kk inclusive, representing the initial state of the parking lot. The rows are numbered 11 to 44 from top to bottom and the columns are numbered 11 to nn from left to right.

In the first and last line, an integer 1xk1≤x≤k represents a parking spot assigned to car xx (you can only move this car to this place), while the integer 00 represents a empty space (you can't move any car to this place).

In the second and third line, an integer 1xk1≤x≤k represents initial position of car xx, while the integer 00 represents an empty space (you can move any car to this place).

Each xx between 11 and kk appears exactly once in the second and third line, and exactly once in the first and fourth line.

Output

If there is a sequence of moves that brings all of the cars to their parking spaces, with at most 2000020000 car moves, then print mm, the number of moves, on the first line. On the following mm lines, print the moves (one move per line) in the format ircc, which corresponds to Allen moving car ii to the neighboring space at row rr and column cc.

If it is not possible for Allen to move all the cars to the correct spaces with at most 2000020000 car moves, print a single line with the integer 1−1.

Examples
input
Copy
4 5
1 2 0 4
1 2 0 4
5 0 0 3
0 5 0 3
output
Copy
6
1 1 1
2 1 2
4 1 4
3 4 4
5 3 2
5 4 2
input
Copy
1 2
1
2
1
2
output
Copy
-1
input
Copy
1 2
1
1
2
2
output
Copy
2
1 1 1
2 4 1
Note

In the first sample test case, all cars are in front of their spots except car 55, which is in front of the parking spot adjacent. The example shows the shortest possible sequence of moves, but any sequence of length at most 2000020000 will be accepted.

In the second sample test case, there is only one column, and the cars are in the wrong order, so no cars can move and the task is impossible.

c题:怎末说呢>>说这个题难??还是不难??总之就是说这些题的思路都不难就看你是否想不想的到>>如果想不到的话试图更暴力一点
暴力呀暴力>>暴力也会出现相通的地方的>>>哎 >>赛后补提>>也不想补>>连续两天晚上cf+球赛,两个四点睡觉>>整个人都傻了?
对最坏情况的判断>>都失误>>难受>>
把2.3行看成一个圆>>每次都旋转一格>>然后check一遍>>看能不能插进去>>

#include<bits/stdc++.h>
using namespace std;
using ll = long long int;
using ull = unsigned long long int;
using dd = double;
using ldd = long double;
#define debug(x) cout << #x << " : " << x << '\n'
#define all(x) x.begin(), x.end()
int arr[4][100];int n, k;
queue<pair<pair<int, int>, int>> ans;
bool check()
{
    bool fuh = 0;
    for (int i = 0; i < n; ++i){
        if (arr[0][i] == arr[1][i] && arr[0][i] != 0)
        {
            fuh = 1;
            ans.push({{arr[0][i], 1}, i+1});
            arr[1][i] = 0;
        }
        if (arr[2][i] == arr[3][i] && arr[2][i] != 0)
        {
            fuh = 1;
            ans.push({{arr[2][i], 4}, i+1});
            arr[2][i]=0;
        }
    }
    return fuh;
}
int x, y;
bool circle()
{
    bool ch = 0;
    for (int i = 0; i < 2 * n-1; ++i)
    {
        if (x == 1)
        {
            if (y < n - 1)
            {
                if (arr[x][y+1] == 0)
                {
                    ++y;
                    continue;
                }
                ans.push({{arr[x][y+1], x+1}, y+1});
                arr[x][y] = arr[x][y+1];
                arr[x][y+1]=0;
                ++y;
                ch = 1;
            }
            else
            {
                if (arr[x+1][y] == 0)
                {
                    x++;
                    continue;
                }
                ans.push({{arr[x+1][y], x+1}, y+1});
                arr[x][y] = arr[x+1][y];
                arr[x+1][y] = 0;
                ++x;
                ch = 1;
            }
        }
        else
        {
            if (y > 0)
            {
                if (arr[x][y-1] == 0)
                {
                    --y;
                    continue;
                }
                ans.push({{arr[x][y-1], x+1}, y+1});
                arr[x][y] = arr[x][y-1];
                arr[x][y-1] = 0;
                --y;
                ch = 1;
            }
            else
            {
                if (arr[x-1][y] == 0)
                {
                    --x;
                    continue;
                }
                ans.push({{arr[x-1][y], x+1}, y+1});
                arr[x][y] = arr[x-1][y];
                arr[x-1][y]=0;
                --x;
                ch = 1;
            }
        }
    }
    return ch;
}
int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);
    cin >> n >> k;
    for (int i = 0; i < 4; ++i)
        for (int j = 0; j < n; ++j)
            cin >> arr[i][j];
    if (k == 2 * n)
    {
        if (!check())
        {
            cout << "-1";
            return 0;
        }
    }
    x = y = 0;
    for (int i = 0; i < n; ++i)
    {
        if (arr[1][i] == 0)
        {
            x = 1;
            y = i;
            break;
        }
        if (arr[2][i] == 0)
        {
            x = 2;
            y = i;
            break;
        }
    }
    for (int i = 0; i < 2 * n; ++i)
    {
        check();
        if (circle())
        {
            continue;
        }
        break;
    }
    cout << ans.size() << '\n';
    while (ans.size())
    {
        cout << ans.front().first.first << " " << ans.front().first.second << " " << ans.front().second << '\n';
        ans.pop();
    }
}
View Code
D. Suit and Tie
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Allen is hosting a formal dinner party. 2n2n people come to the event in nn pairs (couples). After a night of fun, Allen wants to line everyone up for a final picture. The 2n2n people line up, but Allen doesn't like the ordering. Allen prefers if each pair occupies adjacent positions in the line, as this makes the picture more aesthetic.

Help Allen find the minimum number of swaps of adjacent positions he must perform to make it so that each couple occupies adjacent positions in the line.

Input

The first line contains a single integer nn (1n1001≤n≤100), the number of pairs of people.

The second line contains 2n2n integers a1,a2,,a2na1,a2,…,a2n. For each ii with 1in1≤i≤n, ii appears exactly twice. If aj=ak=iaj=ak=i, that means that the jj-th and kk-th people in the line form a couple.

Output

Output a single integer, representing the minimum number of adjacent swaps needed to line the people up so that each pair occupies adjacent positions.

Examples
input
Copy
4
1 1 2 3 3 2 4 4
output
Copy
2
input
Copy
3
1 1 2 2 3 3
output
Copy
0
input
Copy
3
3 1 2 3 1 2
output
Copy
3
Note

In the first sample case, we can transform 11233244112323441122334411233244→11232344→11223344 in two steps. Note that the sequence 11233244113232441133224411233244→11323244→11332244 also works in the same number of steps.

The second sample case already satisfies the constraints; therefore we need 00 swaps.

D题:贪心>>每次都先把队首的那个元素找到匹配的元素>>统计下来就是答案>>
凭直觉>>过的

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int n,tmp;vector<int>a;
int main(){
    std::ios::sync_with_stdio(false);
    cin>>n;for(int i=1;i<=2*n;i++){cin>>tmp;a.push_back(tmp);}
    ll ans=0;
    while(a.size()>0){
        int x=a[0];int pos;
        for(int i=1;i<a.size();i++){
            if(a[i]==x){ans+=(i-1);pos=i;break;}
        }
        auto it=a.begin();a.erase(it+pos);
        it=a.begin();a.erase(it);
    }
    cout<<ans<<endl;
    return 0;
}
View Code

  

E. Leaving the Bar
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

For a vector v=(x,y)v→=(x,y), define |v|=x2+y2|v|=x2+y2.

Allen had a bit too much to drink at the bar, which is at the origin. There are nn vectors v1,v2,,vnv1→,v2→,⋯,vn→. Allen will make nn moves. As Allen's sense of direction is impaired, during the ii-th move he will either move in the direction vivi→ or vi−vi→. In other words, if his position is currently p=(x,y)p=(x,y), he will either move to p+vip+vi→ or pvip−vi→.

Allen doesn't want to wander too far from home (which happens to also be the bar). You need to help him figure out a sequence of moves (a sequence of signs for the vectors) such that his final position pp satisfies |p|1.5106|p|≤1.5⋅106 so that he can stay safe.

Input

The first line contains a single integer nn (1n1051≤n≤105) — the number of moves.

Each of the following lines contains two space-separated integers xixi and yiyi, meaning that vi=(xi,yi)vi→=(xi,yi). We have that |vi|106|vi|≤106 for all ii.

Output

Output a single line containing nn integers c1,c2,,cnc1,c2,⋯,cn, each of which is either 11 or 1−1. Your solution is correct if the value of p=ni=1civip=∑i=1ncivi→, satisfies |p|1.5106|p|≤1.5⋅106.

It can be shown that a solution always exists under the given constraints.

Examples
input
Copy
3
999999 0
0 999999
999999 0
output
Copy
1 1 -1 
input
Copy
1
-824590 246031
output
Copy
1 
input
Copy
8
-67761 603277
640586 -396671
46147 -122580
569609 -2112
400 914208
131792 309779
-850150 -486293
5272 721899
output
Copy
1 1 1 1 1 1 1 -1 



E题:
听他们说怎末瞎搞都能过>>>难受>>正解时说>>任意三个向量中的两个总可以合并成1个向量,并且长度不超过限制>>证明方法很巧妙
大佬们说这个题>>很难卡>>特别佩服杜教的random_shuffle>>>太强了>>收集了两个解法>>正解以后再补

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10; 
struct node{int x,y,v,id;}a[N];
typedef long long ll;
bool cmp(node a,node b){return abs(a.x-a.y)>abs(b.x-b.y);}
ll dis(ll x,ll y){return x*x+y*y;}
bool cmp1(node a,node b){return a.id<b.id;};
int main(){
    std::ios::sync_with_stdio(false);
    int n;cin>>n;
    for(int i=1;i<=n;i++) cin>>a[i].x>>a[i].y,a[i].id=i;
    sort(a+1,a+n+1,cmp);
    ll X=0,Y=0;
    for(int i=1;i<=n;i++){
        if(dis(X+a[i].x,Y+a[i].y)<dis(X-a[i].x,Y-a[i].y)){
            X+=a[i].x,Y+=a[i].y;a[i].v=1;
        }
        else{
            X-=a[i].x,Y-=a[i].y;a[i].v=-1;
        }
    }
    sort(a+1,a+n+1,cmp1);
    for(int i=1;i<=n;i++) cout<<a[i].v<<" ";
    cout<<endl;
    return 0;
} 
View Code
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10; 
struct node{int x,y,id;}a[N];
typedef long long ll;
const ll tmp=1e6+5e5;
ll dis(ll x,ll y){return x*x+y*y;}
int ans[N];
int main(){
    std::ios::sync_with_stdio(false);
    int n;cin>>n;
    for(int i=1;i<=n;i++) cin>>a[i].x>>a[i].y,a[i].id=i;
    while(1){
        random_shuffle(a+1,a+n+1);
        ll X=0,Y=0;
        for(int i=1;i<=n;i++){
            if(dis(X+a[i].x,Y+a[i].y)<dis(X-a[i].x,Y-a[i].y)){
                X+=a[i].x,Y+=a[i].y;ans[a[i].id]=1;
            }
            else{
                X-=a[i].x,Y-=a[i].y;ans[a[i].id]=-1;
            }
        }
        if(dis(X,Y)>=tmp*tmp) continue;
        for(int i=1;i<=n;i++) cout<<ans[i]<<" ";
        return 0;
    }
    return 0;
} 
View Code

猜你喜欢

转载自www.cnblogs.com/vainglory/p/9224833.html