码迷,mamicode.com
首页 > 其他好文 > 详细

D. Time to Run【构造】

时间:2020-03-10 01:12:16      阅读:52      评论:0      收藏:0      [点我收藏+]

标签:must   other   pre   cal   dex   graphics   bin   content   example   

技术图片
  1 #include <bits/stdc++.h>
  2 #define dbg(x) cout << #x << "=" << x << endl
  3 #define eps 1e-8
  4 #define pi acos(-1.0)
  5 
  6 using namespace std;
  7 typedef long long LL;
  8 
  9 const int inf = 0x3f3f3f3f;
 10 
 11 template<class T>inline void read(T &res)
 12 {
 13     char c;T flag=1;
 14     while((c=getchar())<0||c>9)if(c==-)flag=-1;res=c-0;
 15     while((c=getchar())>=0&&c<=9)res=res*10+c-0;res*=flag;
 16 }
 17 
 18 namespace _buff {
 19     const size_t BUFF = 1 << 19;
 20     char ibuf[BUFF], *ib = ibuf, *ie = ibuf;
 21     char getc() {
 22         if (ib == ie) {
 23             ib = ibuf;
 24             ie = ibuf + fread(ibuf, 1, BUFF, stdin);
 25         }
 26         return ib == ie ? -1 : *ib++;
 27     }
 28 }
 29 
 30 int qread() {
 31     using namespace _buff;
 32     int ret = 0;
 33     bool pos = true;
 34     char c = getc();
 35     for (; (c < 0 || c > 9) && c != -; c = getc()) {
 36         assert(~c);
 37     }
 38     if (c == -) {
 39         pos = false;
 40         c = getc();
 41     }
 42     for (; c >= 0 && c <= 9; c = getc()) {
 43         ret = (ret << 3) + (ret << 1) + (c ^ 48);
 44     }
 45     return pos ? ret : -ret;
 46 }
 47 
 48 int m, n, k;
 49 
 50 map<string, int> mp;
 51 vector<pair<int, string> > vec;
 52 
 53 void write(string a) {
 54     mp[a] = 0;
 55 }
 56 
 57 int main()
 58 {
 59     scanf("%d %d %d",&n, &m, &k);
 60     if(4 * n * m - 2 * n - 2 * m < k) {
 61         puts("NO");
 62         return 0;
 63     }
 64     else {
 65         puts("YES");
 66         int cnt = 0;
 67         for ( int i = 1; i <= m - 1; ++i ) {
 68             if(cnt < k) {
 69                 ++cnt;
 70                 mp["R"]++;
 71             }
 72             else {
 73                 break;
 74             }
 75         }
 76         if(mp["R"]) {
 77             vec.push_back(make_pair(mp["R"], "R"));
 78             write("R");
 79             
 80         }
 81         for ( int i = 1; i <= m - 1; ++i ) {
 82             if(cnt < k) {
 83                 ++cnt;
 84                 mp["L"]++;
 85             }
 86             else {
 87                 break;
 88             }
 89         }
 90         if(mp["L"]) {
 91             vec.push_back(make_pair(mp["L"], "L"));
 92             write("L");
 93         }
 94         for(int j = 1; j <= n - 1; ++j) {
 95             if(cnt > k)
 96                 break;
 97             for ( int i = 1; i <= m-2; ++i ) {
 98                 if(k - cnt > 3) {
 99                     mp["DRU"]++;
100                     cnt += 3;
101                 }
102             }
103             //dbg(k-cnt);
104             if(k - cnt >= 3 && m > 1) {
105                 cnt += 3;
106                 mp["DRU"]++;
107                 //dbg(mp["DRU"]);
108             }
109             else {
110                 if(m > 1) {
111                     if(cnt < k) {
112                         mp["D"]++;
113                         cnt++;
114                         vec.push_back(make_pair(mp["D"], "D"));
115                         write("D");
116                         
117                     }
118                     if(cnt < k) {
119                         mp["R"]++;
120                         cnt++;
121                         vec.push_back(make_pair(mp["R"], "R"));
122                         write("R");
123                         
124                     }
125                 }
126             }
127             if(mp["DRU"]) {
128                 vec.push_back(make_pair(mp["DRU"], "DRU"));
129                 write("DRU");
130                 
131             }
132             if(cnt < k) {
133                 mp["D"]++;
134                 cnt++;
135                 vec.push_back(make_pair(mp["D"], "D"));
136                 write("D");
137                 
138             }
139             for ( int i = 1; i <= m - 1; ++i ) {
140                 if(cnt < k) {
141                     mp["L"]++;
142                     cnt++;
143                 }
144                 else {
145                     break;
146                 }   
147             }
148             if(mp["L"] != 0) {
149                 vec.push_back(make_pair(mp["L"], "L"));
150                 write("L");
151             }
152         }
153         for ( int i = 1; i <= n-1; ++i ) {
154             if(cnt < k) {
155                 ++cnt;
156                 mp["U"]++;
157             }
158         }
159         if(mp["U"]) {
160             vec.push_back(make_pair(mp["U"], "U"));
161         }
162         int d = vec.size();
163         printf("%d\n",d);
164         for ( int i = 0; i < d; ++i ) {
165             cout << vec[i].first << " " << vec[i].second << endl;
166         }
167     }
168     return 0;
169 }
View Code

 

D. Time to Run
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Bashar was practicing for the national programming contest. Because of sitting too much in front of the computer without doing physical movements and eating a lot Bashar became much fatter. Bashar is going to quit programming after the national contest and he is going to become an actor (just like his father), so he should lose weight.

In order to lose weight, Bashar is going to run for kk kilometers. Bashar is going to run in a place that looks like a grid of nn rows and mm columns. In this grid there are two one-way roads of one-kilometer length between each pair of adjacent by side cells, one road is going from the first cell to the second one, and the other road is going from the second cell to the first one. So, there are exactly (4nm2n2m)(4nm−2n−2m) roads.

Let‘s take, for example, n=3n=3 and m=4m=4. In this case, there are 3434 roads. It is the picture of this case (arrows describe roads):

技术图片

Bashar wants to run by these rules:

  • He starts at the top-left cell in the grid;
  • In one move Bashar may go up (the symbol ‘U‘), down (the symbol ‘D‘), left (the symbol ‘L‘) or right (the symbol ‘R‘). More formally, if he stands in the cell in the row ii and in the column jj, i.e. in the cell (i,j)(i,j) he will move to:
    • in the case ‘U‘ to the cell (i1,j)(i−1,j);
    • in the case ‘D‘ to the cell (i+1,j)(i+1,j);
    • in the case ‘L‘ to the cell (i,j1)(i,j−1);
    • in the case ‘R‘ to the cell (i,j+1)(i,j+1);
  • He wants to run exactly kk kilometers, so he wants to make exactly kk moves;
  • Bashar can finish in any cell of the grid;
  • He can‘t go out of the grid so at any moment of the time he should be on some cell;
  • Bashar doesn‘t want to get bored while running so he must not visit the same road twice. But he can visit the same cell any number of times.

Bashar asks you if it is possible to run by such rules. If it is possible, you should tell him how should he run.

You should give him aa steps to do and since Bashar can‘t remember too many steps, aa should not exceed 30003000. In every step, you should give him an integer ff and a string of moves ss of length at most 44 which means that he should repeat the moves in the string ss for ff times. He will perform the steps in the order you print them.

For example, if the steps are 2RUD, 3UUL then the moves he is going to move are RUD +RUD +UUL +UUL +UUL =RUDRUDUULUULUUL.

Can you help him and give him a correct sequence of moves such that the total distance he will run is equal to kk kilometers or say, that it is impossible?

Input

The only line contains three integers nn, mm and kk (1n,m5001≤n,m≤500, 1k1091≤k≤109), which are the number of rows and the number of columns in the grid and the total distance Bashar wants to run.

Output

If there is no possible way to run kk kilometers, print "NO" (without quotes), otherwise print "YES" (without quotes) in the first line.

If the answer is "YES", on the second line print an integer aa (1a30001≤a≤3000) — the number of steps, then print aa lines describing the steps.

To describe a step, print an integer ff (1f1091≤f≤109) and a string of moves ss of length at most 44. Every character in ss should be ‘U‘, ‘D‘, ‘L‘ or ‘R‘.

Bashar will start from the top-left cell. Make sure to move exactly kk moves without visiting the same road twice and without going outside the grid. He can finish at any cell.

We can show that if it is possible to run exactly kk kilometers, then it is possible to describe the path under such output constraints.

Examples
input
Copy
3 3 4
output
Copy
YES
2
2 R
2 L
input
Copy
3 3 1000000000
output
Copy
NO
input
Copy
3 3 8
output
Copy
YES
3
2 R
2 D
1 LLRR
input
Copy
4 4 9
output
Copy
YES
1
3 RLD
input
Copy
3 4 16
output
Copy
YES
8
3 R
3 L
1 D
3 R
1 D
1 U
3 L
1 D
Note

The moves Bashar is going to move in the first example are: "RRLL".

It is not possible to run 10000000001000000000 kilometers in the second example because the total length of the roads is smaller and Bashar can‘t run the same road twice.

The moves Bashar is going to move in the third example are: "RRDDLLRR".

The moves Bashar is going to move in the fifth example are: "RRRLLLDRRRDULLLD". It is the picture of his run (the roads on this way are marked with red and numbered in the order of his running):

技术图片

 

 思路

  在棋盘上构造出一条欧拉路径

  如图构造即可,特判一下 m = 1 的情况

技术图片

 

 

 

CODE

 

 

#include <bits/stdc++.h>
#define dbg(x) cout << #x << "=" << x << endl
#define eps 1e-8
#define pi acos(-1.0)

using namespace std;
typedef long long LL;

const int inf = 0x3f3f3f3f;

template<class T>inline void read(T &res)
{
    char c;T flag=1;
    while((c=getchar())<0||c>9)if(c==-)flag=-1;res=c-0;
    while((c=getchar())>=0&&c<=9)res=res*10+c-0;res*=flag;
}

namespace _buff {
    const size_t BUFF = 1 << 19;
    char ibuf[BUFF], *ib = ibuf, *ie = ibuf;
    char getc() {
        if (ib == ie) {
            ib = ibuf;
            ie = ibuf + fread(ibuf, 1, BUFF, stdin);
        }
        return ib == ie ? -1 : *ib++;
    }
}

int qread() {
    using namespace _buff;
    int ret = 0;
    bool pos = true;
    char c = getc();
    for (; (< 0 || c > 9) && c != -; c = getc()) {
        assert(~c);
    }
    if (== -) {
        pos = false;
        c = getc();
    }
    for (; c >= 0 && c <= 9; c = getc()) {
        ret = (ret << 3) + (ret << 1) + (^ 48);
    }
    return pos ? ret : -ret;
}

int m, n, k;

map<string, int> mp;
vector<pair<int, string> > vec;

void write(string a) {
    mp[a] = 0;
}

int main()
{
    scanf("%d %d %d",&n, &m, &k);
    if(4 * n * m - 2 * n - 2 * m < k) {
        puts("NO");
        return 0;
    }
    else {
        puts("YES");
        int cnt = 0;
        for ( int i = 1; i <= m - 1; ++) {
            if(cnt < k) {
                ++cnt;
                mp["R"]++;
            }
            else {
                break;
            }
        }
        if(mp["R"]) {
            vec.push_back(make_pair(mp["R"], "R"));
            write("R");
            
        }
        for ( int i = 1; i <= m - 1; ++) {
            if(cnt < k) {
                ++cnt;
                mp["L"]++;
            }
            else {
                break;
            }
        }
        if(mp["L"]) {
            vec.push_back(make_pair(mp["L"], "L"));
            write("L");
        }
        for(int j = 1; j <= n - 1; ++j) {
            if(cnt > k)
                break;
            for ( int i = 1; i <= m-2; ++) {
                if(- cnt > 3) {
                    mp["DRU"]++;
                    cnt += 3;
                }
            }
            //dbg(k-cnt);
            if(- cnt >= 3 && m > 1) {
                cnt += 3;
                mp["DRU"]++;
                //dbg(mp["DRU"]);
            }
            else {
                if(> 1) {
                    if(cnt < k) {
                        mp["D"]++;
                        cnt++;
                        vec.push_back(make_pair(mp["D"], "D"));
                        write("D");
                        
                    }
                    if(cnt < k) {
                        mp["R"]++;
                        cnt++;
                        vec.push_back(make_pair(mp["R"], "R"));
                        write("R");
                        
                    }
                }
            }
            if(mp["DRU"]) {
                vec.push_back(make_pair(mp["DRU"], "DRU"));
                write("DRU");
                
            }
            if(cnt < k) {
                mp["D"]++;
                cnt++;
                vec.push_back(make_pair(mp["D"], "D"));
                write("D");
                
            }
            for ( int i = 1; i <= m - 1; ++) {
                if(cnt < k) {
                    mp["L"]++;
                    cnt++;
                }
                else {
                    break;
                }   
            }
            if(mp["L"] != 0) {
                vec.push_back(make_pair(mp["L"], "L"));
                write("L");
            }
        }
        for ( int i = 1; i <= n-1; ++) {
            if(cnt < k) {
                ++cnt;
                mp["U"]++;
            }
        }
        if(mp["U"]) {
            vec.push_back(make_pair(mp["U"], "U"));
        }
        int d = vec.size();
        printf("%d\n",d);
        for ( int i = 0; i < d; ++) {
            cout << vec[i].first << " " << vec[i].second << endl;
        }
    }
    return 0;
}

D. Time to Run【构造】

标签:must   other   pre   cal   dex   graphics   bin   content   example   

原文地址:https://www.cnblogs.com/orangeko/p/12452715.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!