数据结构(暴力法)

  1 #include <algorithm>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <string>
  5 #include <queue>
  6 #include <map>
  7 #include <set>
  8 #include <ctime>
  9 #include <cmath>
 10 #include <iostream>
 11 #define pi acos(-1.)
 12 using namespace std;
 13 typedef long long ll;
 14 const int int_inf = 0x3f3f3f3f;
 15 const ll ll_inf = 1ll << 62;
 16 const int INT_INF = (int)((1ll << 31) - 1);
 17 const int mod = 1e6 + 7;
 18 const double double_inf = 1e30;
 19 typedef unsigned long long ul;
 20 #pragma comment(linker, "/STACK:102400000,102400000")
 21 #define max(a, b) ((a) > (b) ? (a) : (b))
 22 #define min(a, b) ((a) < (b) ? (a) : (b))
 23 #define mp make_pair
 24 #define st first
 25 #define nd second
 26 #define keyn (root->ch[1]->ch[0])
 27 #define lson (u << 1)
 28 #define rson (u << 1 | 1)
 29 #define pii pair<int, int>
 30 #define pll pair<ll, ll>
 31 #define pb push_back
 32 #define type(x) __typeof(x.begin())
 33 #define foreach(i, j) for(type(j)i = j.begin(); i != j.end(); i++)
 34 #define FOR(i, s, t) for(int i = s; i <= t; i++)
 35 #define ROF(i, t, s) for(int i = t; i >= s; i--)
 36 #define dbg(x) cout << x << endl
 37 #define dbg2(x, y) cout << x << " " << y << endl
 38 #define clr(x, i) memset(x, (i), sizeof(x))
 39 #define maximize(x, y) x = max((x), (y))
 40 #define minimize(x, y) x = min((x), (y))
 41 inline int readint(){
 42     bool neg = 0; char ch, t[11];
 43     int k = 0;
 44     while((ch = getchar()) == ‘ ‘ || ch == ‘\n‘) ;
 45     neg = ch == ‘-‘;
 46     ch == ‘-‘ ? neg = 1 : t[k++] = ch;
 47     while((ch = getchar()) >= ‘0‘ && ch <= ‘9‘) t[k++] = ch;
 48     int x = 0, y = 1;
 49     while(k) x += (t[--k] - ‘0‘) * y, y *= 10;
 50     return neg ? -x : x;
 51 }
 52
 53 inline int readstr(char *s){
 54     char ch;
 55     int len = 0;
 56     while((ch = getchar()) == ‘ ‘ || ch == ‘\n‘) ;
 57     if(ch == EOF) return 0;
 58     *(s++) = ch, ++len;
 59     while((ch = getchar()) != ‘ ‘ && ch != ‘\n‘ && ch != EOF) *(s++) = ch, ++len;
 60     *s = ‘\0‘;
 61     return len;
 62 }
 63
 64 inline void writestr(const char *s){
 65     while(*s != ‘\0‘) putchar(*(s++));
 66     putchar(‘\n‘);
 67 }
 68
 69 inline void writeint(int num){
 70     if(!num){
 71         putchar(‘0‘), putchar(‘\n‘);
 72         return;
 73     }
 74     if(num < 0) num = -num, putchar(‘-‘);
 75     char buf[15];
 76     int k = 0;
 77     while(num) buf[k++] = num % 10, num /= 10;
 78     ROF(i, k - 1, 0) putchar(buf[i] + ‘0‘);
 79     putchar(‘\n‘);
 80 }
 81 class cmpt{
 82 public:
 83     bool operator () (const int &x, const int &y) const{
 84         return x > y;
 85     }
 86 };
 87 int Rand(int x, int o){
 88     //if o set, return [1, x], else return [0, x - 1]
 89     int tem = (int)((double)rand() / RAND_MAX * x) % x;
 90     return o ? tem + 1 : tem;
 91 }
 92
 93 void data_gen(){
 94     freopen("in.txt", "w", stdout);
 95     int n = 80, y = 4000;
 96     printf("%d %d\n", n, y);
 97     int operand;
 98     srand(time(0));
 99     FOR(i, 1, y){
100         operand = rand() % 2;
101         if(operand){
102             printf("Q ");
103             int num = rand() % 13 + 1;
104             printf("%d ", num);
105             FOR(j, 1, num){
106                 int tem = Rand(n, 0);
107                 printf("%d ", tem);
108             }
109             printf("\n");
110         }else{
111             printf("I ");
112             int ok = rand() % 2;
113             if(ok){
114                 printf("%d %d\n", Rand(n, 0), Rand(1 << 20, 0));
115             }else{
116                 printf("%d %d %d\n", Rand(n, 0), Rand(n, 0), Rand(1 << 20, 0));
117             }
118         }
119     }
120 }
121
122 struct cmpx{
123     bool operator () (int x, int y) { return x > y; }
124 };
125 int debug = 0;
126 int dx[] = {-1, 1, 0, 0};
127 int dy[] = {0, 0, -1, 1};
128 //-------------------------------------------------------------------------
129 const int maxn = 1e4 + 10;
130 pii buf[maxn];
131 int s, r, w, p;
132 set<pii> S;
133 pii pdt[maxn];
134 pair<pii, pii> walls[20];
135
136 ll Cross(pii x, pii y){
137     return (ll)x.st * y.nd - (ll)x.nd * y.st;
138 }
139
140 int Dif(ll x, ll y){
141     return !(x > 0 && y > 0 || x < 0 && y < 0);
142 }
143 pii operator - (pii x, pii y) { return mp(y.st - x.st, y.nd - x.nd); }
144
145 int Intersect(pair<pii, pii> _wall, pii x, pii y){
146     pii a1 = _wall.st, a2 = _wall.nd;
147     pii b1 = x, b2 = y;
148     ll c1 = Cross(a2 - a1, b1 - a1), c2 = Cross(a2 - a1, b2 - a1);
149     ll c3 = Cross(b2 - b1, a1 - b1), c4 = Cross(b2 - b1, a2 - b1);
150     //dbg2(c1, c2), dbg2(c3, c4);
151     return Dif(c1, c2) && Dif(c3, c4);
152 }
153
154 //-------------------------------------------------------------------------
155 int main(){
156     //data_gen(); return 0;
157     debug = 1;
158     ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
159     if(debug) freopen("in.txt", "r", stdin);
160     //freopen("out.txt", "w", stdout);
161     int T = readint();
162     while(T--){
163         s = readint(), r = readint(), w = readint(), p = readint();
164         S.clear();
165         FOR(i, 1, s){
166             int x = readint(), y = readint();
167             S.insert(mp(x, y));
168         }
169         FOR(i, 1, w){
170             int x = readint(), y = readint();
171             walls[i].st = mp(x, y);
172             x = readint(), y = readint();
173             walls[i].nd = mp(x, y);
174         }
175         FOR(i, 1, p){
176             int x = readint(), y = readint();
177             pdt[i] = mp(x, y);
178         }
179         FOR(i, 1, p){
180             int tem = 0;
181             FOR(j, -r, r) FOR(k, -r, r){
182                 int nx = pdt[i].st + j, ny = pdt[i].nd + k;
183                 if(S.find(mp(nx, ny)) == S.end()) continue;
184                 int dist = j * j + k * k;
185                 if(dist > r * r) continue;
186                 int res = 0;
187                 FOR(u, 1, w) if(Intersect(walls[u], mp(nx, ny), pdt[i])) ++res;
188                 if(res > r) continue;
189                 int tmp = r - res;
190                 if(dist > tmp * tmp) continue;
191                 buf[tem++] = mp(nx, ny);
192             }
193             printf("%d", tem);
194             FOR(u, 0, tem - 1) printf(" (%d,%d)", buf[u].st, buf[u].nd);
195             putchar(‘\n‘);
196         }
197     }
198     //////////////////////////////////////////////////////////////////////////////////////////////////////////////
199     return 0;
200 }
201 //https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3919
202 //WA
203 //http://paste.ubuntu.com/18381653/
时间: 2024-08-11 07:30:52

数据结构(暴力法)的相关文章

暴力法解凸包

给定平面上一系列的点,用暴力法求解它们的凸包,此算法比普通的暴力法要优化,用新找到的极点去寻找下一个极点.此算法不能用于任何两个点在一直线上的情况. 输入 ConvexHull.txt 7,810,1714,1415,2316,1217,322,1724,426,18 C代码 1 /*brute force solution to convex hull problem */ 2 /*only limited to there is no point on the same line with

[数据结构暴力] zoj 3749 Chameleon

题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3749 Chameleon Time Limit: 6 Seconds      Memory Limit: 65536 KB Given n groups of integers(all the integers are distinct). You should answer Q queries in this problem. Each query con

暴力法求最小生成树

暴力法求最小生成树 5 71 2 22 5 21 3 41 4 73 4 12 3 13 5 6 我们采用的是dfs的回溯暴力,所以对于如下图,只能搜索到3条路,就是那种dfs的路. 思路: 暴力求最小生成树求这个图的最小生成树我就要看有多少个点被选进去了,vis数组就好,并且用个n来表示已经被选的点的个数 然后记录所以已经选了的路径和 1 #include <bits/stdc++.h> 2 #define INFINITE 0x3fffffff 3 using namespace std;

数据结构—头插法逆转单链表——空间复杂度为O(1)

#if 1 #include<stdio.h> #include<stdlib.h> #include<iostream> using namespace std; struct Node { int data; Node *next; }; //初始化 Node *init() { Node *head=new Node; head->next=NULL; return head; } //头插法创建节点 void insetList(Node *head,in

大话数据结构02-算法

1.开场白 算法是解决特定问题求解步骤的描述, 在计算机中表现为指令的有限序列, 并且 每条指令表示一个或多个操作.也就是数据结构与算法分析的过程. 现在我要求你写一个求 1+2+3+--+100 结果的程序,大部分人这么写: 这相当于另外一种求等差数列的算法,等差数列是指从第二项起,每一项与它的前一项的差等于同一个常数的一种数列,常用A.P表示.这个常数叫做等差数列的公差,公差常用字母d表示:例如:1,3,5,7,9--2n-1.通项公式为:an=a1+(n-1)*d.首项a1=1,公差d=2

数据结构-冒泡排序法

冒泡排序法又称为交换排序法,是由观察水中冒泡变化构思而成,气泡随着水深压力而改变.气泡在水底时,压力最大,气泡最小:当慢慢浮上水面时,发现气泡由小渐渐变大. 冒泡排序法的比较方式由第一个元素开始,比较相邻元素大小,若大小顺序有误,则对调后再进行下一个元素的比较.如此扫描过一次之后就可确保最后一个元素是位于正确的顺序.接着再逐步进行第二次扫描,直到完成所有元素的排序关系为止. 演算过程 由此可知 5 个元素的冒泡排序法必须执行 5-1 次扫描,第一次扫描需比较 5-1 次,共比较 4+3+2+1

数据结构——头插法

数据结构 顺推法 学习笔记

斐波拉契数列 #include <stdio.h> #define NUM 13 int main() { int  i; long fib[NUM] = {1,1}; for(i=2 ; i<NUM ;i++)  { fib[i] = fib[i-1] + fib[i-2]; } for( i =0 ;i<NUM ; i++) { printf("%d月兔子总数:%d\n", i ,fib[i]); } getch(); return 0; }

数据结构 贪婪法 学习笔记

自动计算找零的张数 #include<stdio.h> #define MAXN 9 int parvalue[MAXN] = {10000,5000,1000,500,200,100,50,20,10}; int num[MAXN] = {0}; int exchange(int n) { int i,j; for(i=0;i<MAXN;i++) if(n>parvalue[i]) break; while(n>0 && i<MAXN) { if(n&

【背包问题】【暴力法】使用暴力法解决背包问题的思路和代码实现

//将所有的组合穷尽排列出来,更新某一个特征值 /* 例题: 有n件物品,每件物品的重量为w[i],价值为c[i]. 现在需要选出若干件物品放入一个容量为V的背包中, 使得在选入背包的物品重量和不超过容量V的前提下, 让背包中物品的价值之和最大,求最大价值.(1≤n≤30) */ #include<iostream> using namespace std; #define maxn 30 int n,v; int maxvalue=0; int w[maxn], c[maxn]; /* 函数