CodeForces 839B Game of the Rows

传送门:http://codeforces.com/problemset/problem/839/B

题意:

   一个飞机的每一排的座位排列如下图,图中相邻的座位为{1,2},{3,4},{4,5},{5,6},{7,8},飞机共有n排,现在有k队人需要安排座位,问能否有一种安排,使任何两个不同队伍的人不相邻。

题解:

  这道题真的是从头wa到尾,完全跑暴力还tle了,好菜啊。。。。

  将座位分为两种一种四连坐,一种二连坐。

  将每个队伍先尽可能的按4个人直接安排。可能会多出1,2,3个人统计多出1/2/3个人的数量:c[1],c[2],c[3].

  先处理3个人的情况先尽可能排在4连坐。如果剩余,剩下的将3人拆分成2人和1人,2人直接排在二连坐,剩下的一人加入到c[1]中。

  再处理2人情况,考虑单独1个人的话座位浪费太多,于是尽量先将2人和1人合起来排在4连坐,即3位置为1人56位置两人。这道题最大的坑点是考虑如何使有2人队时尽量坐满,我们可以这样处理,先考虑四连坐,取出两个四连坐,两个3位置分别坐一个2人队的2个人。两个56分别坐两个2人队伍这样两排8个人的位置就可以坐6个人(开始没想到,一直wa到test43)。如果有剩余的话只能单独排位了,因为2连坐没有浪费,所以先排2连坐在排4连坐。

  剩下的只有一个人了,二连坐一排只能坐一个,四连坐一排只能坐2个。

 1 #include <bits/stdc++.h>
 2 #define INF 0x3f3f3f3f
 3 using namespace std;
 4 typedef long long ll;
 5 const int mod=1e9+7;
 6 const int maxn=1e4+100;
 7 const double eps=1e-10;
 8 int n,k,a[105],use,need,all,c[5],mi;
 9 int main() {
10 #ifdef ac
11     freopen("in.txt" , "r" , stdin);
12 //    freopen("out.txt" , "w" , stdout);
13 #endif
14     cin >> n >> k; all=n;use=n*2;
15     for(int i=1;i<=k;++i) cin >> a[i];
16     for(int i=1;i<=k;++i) {
17         while(all&&a[i]>=4)all--,a[i]-=4;
18     }
19     for(int i=1;i<=k;++i) {
20         use-=a[i]/4*2;a[i]%=4;
21         c[a[i]]++;
22     }
23     if(c[3]) {
24         mi=min(c[3],all); all-=mi; c[3]-=mi;
25         mi=min(use,c[3]); use-=mi; c[3]-=mi; c[1]+=mi;
26         if(c[3]) {
27             cout << "NO" << endl;
28             return 0;
29         }
30     }
31     if(c[2]) {
32         mi=min(min(all,c[2]) ,c[1]); all-=mi; c[2]-=mi;c[1]-=mi;
33         mi=min(use,c[2]); c[2]-=mi; use-=mi;
34         mi=min(all/2,c[2]/3); all-=mi*2; c[2]-=mi*3;
35
36         while(c[2]&&all) {
37             all--; c[2]--;
38             if(c[1])c[1]--;
39         }
40         if(c[2]) {
41             cout << "NO" << endl;
42             return 0;
43         }
44     }
45     if(c[1]) {
46         all=all*2+use;
47         if(all<c[1]) {
48             cout << "NO" << endl;
49             return 0;
50         }
51     }
52     cout << "YES" << endl;
53     return 0;
54 }

时间: 2024-10-29 10:45:56

CodeForces 839B Game of the Rows的相关文章

Co-prime Array&amp;&amp;Seating On Bus(两道水题)

Co-prime Array Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 660A Description You are given an array of n elements, you must make it a co-prime array in as few moves as possible. In ea

CodeForces 510 B. Fox And Two Dots(DFS 啊)

题目链接:http://codeforces.com/problemset/problem/510/B Fox Ciel is playing a mobile puzzle game called "Two Dots". The basic levels are played on a board of size n?×?m cells, like this: Each cell contains a dot that has some color. We will use diff

CodeForces 510 A. Fox And Snake(模拟啊 )

题目链接:http://codeforces.com/problemset/problem/510/A Fox Ciel starts to learn programming. The first task is drawing a fox! However, that turns out to be too hard for a beginner, so she decides to draw a snake instead. A snake is a pattern on a n by m

CodeForces 1B. Spreadsheets(模拟)

题目链接:http://codeforces.com/problemset/problem/1/B B. Spreadsheets time limit per test 10 seconds memory limit per test 64 megabytes input standard input output standard output In the popular spreadsheets systems (for example, in Excel) the following

Codeforces Round #368 (Div. 2) Brain&#39;s Photos

Brain's Photos Small, but very brave, mouse Brain was not accepted to summer school of young villains. He was upset and decided to postpone his plans of taking over the world, but to become a photographer instead. As you may know, the coolest photos

Codeforces Round #200 (Div. 2) (ABCDE题解)

比赛链接:http://codeforces.com/contest/344 A. Magnets time limit per test:1 second memory limit per test:256 megabytes Mad scientist Mike entertains himself by arranging rows of dominoes. He doesn't need dominoes, though: he uses rectangular magnets inst

CodeForces - 344A Magnets (模拟题)

CodeForces - 344A Magnets Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submit Status Description Mad scientist Mike entertains himself by arranging rows of dominoes. He doesn't need dominoes, though: he uses rectangula

Codeforces Round #308 (Div. 2)

A. Vanya and Table Vanya has a table consisting of 100 rows, each row contains 100 cells. The rows are numbered by integers from 1 to 100 from bottom to top, the columns are numbered from 1 to 100 from left to right. In this table, Vanya chose n rect

B. Ohana Cleans Up(Codeforces Round #309 (Div. 2))

B. Ohana Cleans Up Ohana Matsumae is trying to clean a room, which is divided up into an n by n grid of squares. Each square is initially either clean or dirty. Ohana can sweep her broom over columns of the grid. Her broom is very strange: if she swe